This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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: 174

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.