Author Topic:   Math min,max
Mani
ranch hand
posted April 29, 2000 06:29 AM             
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.max0.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.

Ans given is B.
I compiled the code and it is giving the same .

What is happening here?? Math.min(-0.0,+0.0) gives -0.0
Math.max(-0.0,+0.0) gives +0.0

If I'm not wrong -0.0 is differnt from +0.0
So it has to print false for the System.out.println
Whats wrong in this??

[This message has been edited by Mani (edited April 30, 2000).]

maha anna
bartender
posted April 30, 2000 11:58 AM             
Mani,
Please edit your message. The last part. Math.min(-0.0,+0.0) gives +0.0 as Math.max(...)

In Java primitive float type, other than NaN all other float values are ordered. float primitive type has special +inf , -inf ,+0.0 ,-0.0 . In these -0.0 is considered to be less than +0.0 and +0.0 is considered to be greater than -0.0.

But (0.0 == -0.0) IS TRUE. I have my notes which I written down from JLS and It is said in JLS like that. But min(+0.0,-0.0) is
-0.0 and max(+0.0,-0.0) is +0.0.

I will get the exact page from JLS for you as soon as I get it.

regds
maha anna

[This message has been edited by maha anna (edited April 30, 2000).]

Mani
ranch hand
posted April 30, 2000 11:02 PM             
Maha ,

I have edited the message.
Have they given any explantions??

|