Manisha Singh Sanoo
Manisha Singh Sanoo

Reputation: 929

Getting NaN as answer for a simple Java program

I'm learning Java lately so i started converting my programs in C++ to Java but i'm getting NaN as answer when i'm calculating the distance. Can anyone tell me what is the problem here? Thank you. Here are the codes:

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

public class Labsheet1Number1 {

public static void main(String[] args) {
    double length=0,height=0,pi=3.142;
    double distance=0,angle=0;

    Scanner input = new Scanner(System.in);
    System.out.println("This program will calculate the distance and the angle.");
    System.out.print("Enter the length of the ladder: ");
    length = input.nextInt();
    System.out.print("Enter the height of the wall: ");
    height = input.nextInt();

    distance = Math.sqrt(Math.pow(length, 2) - Math.pow(height, 2));

    angle = (180/pi) * (Math.sin(height/length));

    System.out.println("The distance is " + distance);
    System.out.println("The angle is " + angle);
}

}

Upvotes: 2

Views: 956

Answers (2)

Eran
Eran

Reputation: 394116

If you are calculating the distance between two points, the distance should be :

distance = Math.sqrt(Math.pow(length, 2) + Math.pow(height, 2));

As it is, you might be computing Math.sqrt(x) for a negative number, which would explain the NaN (Not a Number) result.

From the JavaDoc of Math.sqrt() :

If the argument is NaN or less than zero, then the result is NaN

EDIT :

Due to @AnthonyGrist comment, I read the question again. While the equation for distance calculation is dist(p1,p2) = sqrt ( (x1-x2)^2 + (y1-y2)^2 ), based on the inputs requested from the user - Enter the length of the ladder: and Enter the height of the wall: - it seems reasonable to believe that the distance we are meant to compute is the horizontal distance of the bottom of a ladder from the bottom of the wall.

If that is the case, p1 and p2 are the locations of the ladder's edges (lets assume for simplicity the ladder is 1-dimentional), and dist(p1,p2) in the above equation is already known - it's length. Assuming that the ladder reaches the top of the wall, we also know that height = abs(y1-y2).

Therefore, what we want to calculate is actually abs(x1-x2).

If we rearrange the above equation, we get :

distance = abs(x1-x2) = sqrt (dist(p1,p2)^2 - (y1-y2)^2) = sqrt (length^2 - height^2)

Which is exactly the original equation in the question.

However, this equation is correct only if height <= length (since they ladder cannot reach the top of a wall higher than its own length). Therefore, in order to avoid invalid result (NaN), you should validate the input, and make sure height <= length.

Upvotes: 6

Akash Thakare
Akash Thakare

Reputation: 23012

Nan stands for NOT a NUMBER. If a fractional operation has some inputs that causes the operation to make some weird or undefined result.For example square root of a negative number is undefined which is basically performed in your code for Math.sqrt() for length2-height2 < 0.It can be caused by 0.0/0.0 as well but that's not the case here.

Upvotes: 3

Related Questions