• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Majji #Q.28

 
Punitha krishna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }

A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.
The correct answer is A.
How come System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0)) is true. I thought it is false because Math.min(-0.0,+0.0) is -0.0 and Math.max(0.0,+0.0)) is 0.0.
Thanx in adv
 
Anbooo Sanygao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope the following Notes from JLS makes it clear
http://java.sun.com/docs/books/jls/html/4.doc.html#95845
4.2.3 Floating-Point Types and Values

Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity. The operations Math.min and Math.max also distinguish positive zero and negative zero.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic