Punitha krishna

Greenhorn

Posts: 16

posted 16 years ago

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

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

posted 16 years ago

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.

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.

It is sorta covered in the JavaRanch Style Guide. |