Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Positive Infinity

 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}

}
 
Sagar Sharma
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raghvendra Sharma
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic