Why does the following code result in Positive Infinity?

public class Test { public static void main(String []args) { double d = -10.0 / -0.0; if (d == Double.POSITIVE_INFINITY) System.out.println("d = Positive Infinity"); if (d == Double.NEGATIVE_INFINITY) System.out.println("d = Negative Infinity"); }

hi, though you divide a neagative number by -0.0 the computation is done for the signs which result in a positive entity. However the numerical result is INFINITY and hence the result shows up. Thanks .....Hope it helps!! Sagar

Dear Terry, The result is POSITIVE_INFINITY because you are trying with both negative numbers, mathematically, if in a division, both the numbers are negative the resutl is always positive. So the result is POSITIVE_INFINITY and nothing different. Try the same with -/+ and +/- and you'll get NEGATIVE_INFINITY.

hope it helps regards, raghav..

Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 175

posted

0

Thanks for the help. I realize that dividing two negative numbers results in a positive. I just don't understand why Java makes a distinction between -0.0 and 0.0.