0

I created a method:

public double Calculouno(double x1,double x2,double y1,double y2)
{
    double ecuacion1;
    ecuacion1= (x2-x1)+(y2-y1);
    ecuacion1= Math.sqrt(ecuacion1);
    return ecuacion1;
}

When my program tries to calculate ecuacion1 using mathematical functions such as pow and sqrt (at least that´s what I suspect), it just stops working without a compiler warning and says "Build succesful". Help please.

When i reach this part (method), the compiler says "Build succesful" and it just ends. My program works great until this part.

This is the entire source code.

    import java.util.Scanner;
import java.lang.Math;

public class Ejercicio12
{
    public static void main(String args[])
    {
        double[] x= new double[3];
        double[] y= new double[3];
        double a,b,c;
        int con=0, con2=0;
        double[] angulo= new double[3];
        Scanner entrada = new Scanner(System.in);
        Calculos cal= new Calculos();

        for(con=0;con<3;con++)
        {
        System.out.println("Ingrese un valor x para el punto "+(con+1)+": ");
        x[con]= entrada.nextDouble();
        System.out.println("Ingrese un valor y para el punto "+(con+1)+": ");
        y[con]= entrada.nextDouble();
        }

        a= cal.Calculouno(x[0],x[1],y[0],y[1]);
        b= cal.Calculouno(x[1],x[2],y[1],y[2]);
        c= cal.Calculouno(x[2],x[0],y[2],y[0]);

        angulo[0]= cal.Angulo(a,b,c);
        angulo[1]= cal.Angulo(c,a,b);
        angulo[2]= cal.Angulo(b,a,c);

        if(angulo[0]>90||angulo[1]>90||angulo[2]>90)
        {
            System.out.println("El triangulo es obtusangulo");
        }
        else
        {
            if(angulo[0]==90||angulo[1]==90||angulo[2]==90)
            {
                System.out.println("El triangulo es rectangulo");
            }
            else
            {
                if(angulo[0]<90&&angulo[1]<90&&angulo[2]<90)
                {
                    System.out.println("El triangulo es acutangulo");
                }
            }

        }
    }


}



 import static java.lang.Math.sqrt;
    import static java.lang.Math.pow;
    import static java.lang.Math.acos;
    public class Calculos
    {
    public double Calculouno(double x1,double x2,double y1,double y2)
        {
            double ecuacion1;
            double dx= (x2-x1);
            double dy= (y2-y1);
            return Math.sqrt(dy+dx);

        }


        public double Angulo(double a1,double b1, double c1)
        {
            double ecuacion2;
            double a11 = pow(a1,2);
            double b11 = pow(b1,2);
            double c11 = pow(c1,1);

            double xx=(b11+c11-a11);
            double zz=(2*b1*c1);

            return Math.acos(xx/zz);
     }

}
3
  • 5
    Define "stops working". Does it crash or Hang? Does it be non-responsive? Also how are you calling this function? What are you doing with the result? Commented Sep 9, 2012 at 1:46
  • 2
    Hi none, Math.sqrt() may be throwing the error; when ecuacion1 is less than zero. Your presentation of the problem is quite lacking BTW, try to be more informative. Commented Sep 9, 2012 at 1:52
  • 1
    @varchar: When the parameter to Math.sqrt() is less than 0, it returns NaN; it doesn't throw any exceptions. Commented Sep 9, 2012 at 1:58

2 Answers 2

1

There is nothing in the code in your snippet that will (directly) cause the program to "stop without warning".

  • There are no syntax errors (etcetera) that would cause a build to fail. (And that matches what you report.)

  • Giving "bad" input to Math.sqrt won't cause it to stop, or even throw an exception. The javadoc says: "[Returns] the positive square root of a. If the argument is NaN or less than zero, the result is NaN." i.e. bad input will give you a NaN value.

  • Bad input wouldn't cause the arithmetic before the sqrt call to throw exceptions. The JLS says (for the floating point + and - operators) "[i]f either operand is NaN, the result is NaN."

So the immediate cause of your application's stopping must be somewhere else in your application.

I expect that what is happening is that some other part of your code is throwing an exception when it gets an unexpected result from this method (maybe a NaN) ... and your application is squashing the exception.


I understand the problem now.

What is happening is that the arithmetic and/or calls to sqrt and pow >>are<< generating NaN values. When you test a NaN value using any of the relational operators, the result of the expression is always false. So that means that your code that none of the println calls is made.

Your code is not actually stopping. Rather it is completing normally without producing any output.

And the underlying reason that the calculations are producing NaN values is ... as @duffymo has pointed out ... that you have implemented the geometric formulae incorrectly.


For the record, NaN values have peculiar behaviour when they are used in a relation expressions. For instance:

    double x = 0.0 / 0.0;  // generate a NaN
    System.out.println(0.0 == x);
    System.out.println(0.0 != x);
    System.out.println(0.0 < x);
    System.out.println(0.0 > x);
    System.out.println(x == x);
    System.out.println(x != x);
    System.out.println(x < x);
    System.out.println(x > x);

All will of the above will print "false". Yes, all of them!

The only way to test for a NaN is to use Double.isNaN(double) or Float.isNaN(float).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the post, i´ll see what i can do to fix the problem. Any tip for the solution would be great, i´m a java beginner.
Tips - 1) read @duffymo's answer and comments carefully, 2) check the formulae you are using. 3) it pays to actually understand the formulae you are trying to implement.
1

Here are two links that I believe describe the problem you want to solve pretty well:

http://mathworld.wolfram.com/AcuteTriangle.html

and

http://mathworld.wolfram.com/ObtuseTriangle.html

Here's how I might write it. I didn't test it exhaustively:

package cruft;

/**
 * Junk description here
 * @author Michael
 * @link
 * @since 9/8/12 10:19 PM
 */

public class Triangle {

    private final Point p1;
    private final Point p2;
    private final Point p3;

    public static void main(String args[]) {
        if (args.length > 5) {
            Point p1 = new Point(Double.valueOf(args[0]), Double.valueOf(args[1]));
            Point p2 = new Point(Double.valueOf(args[2]), Double.valueOf(args[3]));
            Point p3 = new Point(Double.valueOf(args[4]), Double.valueOf(args[5]));
            Triangle triangle = new Triangle(p1, p2, p3);
            double angle = triangle.calculateAngle();
            System.out.println(triangle);
            if (angle > 0.0) {
                System.out.println("obtuse");
            } else if (angle < 0.0) {
                System.out.println("acute");
            } else {
                System.out.println("right triangle");
            }
        } else {
            System.out.println("Usage: Triangle x1 y1 x2 y2 x3 y3");
        }
    }

    public Triangle(Point p1, Point p2, Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public double calculateAngle(){
        double a = Point.distance(this.p1, this.p2);
        double b = Point.distance(this.p2, this.p3);
        double c = Point.distance(this.p3, this.p1);
        return Math.acos(a*a + b*b - c*c)/2.0/a/b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Triangle");
        sb.append("{p1=").append(p1);
        sb.append(", p2=").append(p2);
        sb.append(", p3=").append(p3);
        sb.append('}');
        return sb.toString();
    }
}

class Point {
    public final double x;
    public final double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public static double distance(Point q1, Point q2) {
        double dx = Math.abs(q1.x-q2.x);
        double dy = Math.abs(q1.y-q2.y);
        if (dx > dy) {
            double r = dy/dx;
            return dx*Math.sqrt(1.0+r*r);
        } else {
            double r = dx/dy;
            return dy*Math.sqrt(1.0+r*r);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append('(').append(x);
        sb.append(',').append(y);
        sb.append(')');
        return sb.toString();
    }
}

6 Comments

I made the change. It´s not working, the program just ends and says "Build succesful" when i reach that part. But if i delete all the mathematical functions, the program works. In an additional note, i´m using java.lang.Math;
Post more code. Building and running are two different things, right? The first one can succeed, because all you're doing is generating byte code. Running means actually doing the calculations. Your statements make no sense at all.
Now it's even worse. You're using angles in degrees; you should be thinking radians.
Still nothing, i just cant find a mistake or error in my source code. BUT when i return a value without using sqrt, the program works great. Whats happening :(. Note: without using any math function
@none - Stop hacking, and go back and check the formulae you bare trying to implement. Figure out 1) whether they are correct for your problem and 2) whether you have translated them into Java correctly. The problem is not with the sqrt, acos, but with the way that you are calling them. Basically, you are calling them with input values that do not make sense from a mathematical perspective.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.