Terry McKee

Ranch Hand

Posts: 175

posted 16 years ago

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");

}

}

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");

}

}

Sagar Sharma

Ranch Hand

Posts: 92

Raghvendra Sharma

Ranch Hand

Posts: 82

posted 16 years ago

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..

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

Posts: 175

posted 16 years ago

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.