Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Rules Roundup - answer wrong?

Sharda Vajjhala
Ranch Hand

Joined: Nov 14, 2001
Posts: 57
One of the questions on Rules Roundup was
double x;
x=24.0/0;
what is the value of x? I tested this out and it came to Infinity, although, the answer given was NaN.
Thanks
Sharda
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.

Ang guess what x is 0 in the question....
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 256
Well isn't that 0 promoted to a double before the evaluation? If so wouldn't that result in a NaN since division by zero using double or float doesn't give a ArithmeticException but instead returns Double.NaN or Float.NaN?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Why do you think it should return a NaN rather than an Infinity?
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 256
Originally posted by Ron Newman:
Why do you think it should return a NaN rather than an Infinity?

Because for some reason I have it stuck in my head that division by zero is not a number. However double checking my books I see that division by zero is infinity when dealing with floating point numbers, the sign determined by the original sign of the dividend (numerator if a fraction).
I honestly can't think of where I learned division by zero is not a number to keep me making that mistake.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.

If 0 is always going to be converted to double before division occurs, is it fair to say that NaN is never going to be output of x/0 where x is double.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
No, because 0.0/0.0 should still be a NaN, not an Infinity.
Bharat Keskar
Greenhorn

Joined: Aug 26, 2002
Posts: 11
try this,

System.out.println(0.0/0);//NaN
System.out.println(-0/0.0);//NaN
System.out.println(0.0/0.0);//NaN
System.out.println(-0.0/0.0);//NaN
System.out.println(0.0/-0.0);//NaN
System.out.println(0.0/-0);//NaN
System.out.println(0.0/+0);//NaN
System.out.println(0.0f/0);//NaN
System.out.println(0.0f/-0);//NaN
System.out.println(0.0f/-0.0);//NaN
System.out.println(0.0f/0.0);//NaN
System.out.println(5.0f/0.0);// Infinity
System.out.println(5.0/0.0);// Infinity
System.out.println(-5.0/0.0);// -Infinity
System.out.println(5.0/-0.0);// -Infinity
System.out.println(-5.0/-0.0);// Infinity

I agree. Here's the link: http://aspose.com/file-tools

subject: Rules Roundup - answer wrong?