System.out.println(Math.min(-0.0,+0.0));
System.out.println(Math.max(-0.0,+0.0));
System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
output:
-0.0
0.0
true
the last value is true ?
how ? -0.0 not equals 0.0 ....is not it ?
still it prints out true ? why ?
Tim Ward - 14 Sep 2006 15:19 GMT
> how ? -0.0 not equals 0.0 ....is not it ?
For even more amusement try comparing NaNs ...
--
Tim Ward
Brett Ward Limited - www.brettward.co.uk
Oliver Wong - 14 Sep 2006 15:21 GMT
> System.out.println(Math.min(-0.0,+0.0));
> System.out.println(Math.max(-0.0,+0.0));
[quoted text clipped - 11 lines]
>
> still it prints out true ? why ?
I believe IEEE's floating point standard defines 0.0 as being equal
to -0.0. Probably because it would generate the least astonishment in the
developer.
- Oliver
Ralf Seitner - 14 Sep 2006 15:21 GMT
gk schrieb:
> System.out.println(Math.min(-0.0,+0.0));
> System.out.println(Math.max(-0.0,+0.0));
[quoted text clipped - 5 lines]
> 0.0
> true
Hi!
What have you expected?
> the last value is true ?
>
> how ? -0.0 not equals 0.0 ....is not it ?
Of course -0.0 is the same as 0.0.
If you try System.out.println(0.0==-0.0); it will print true.
> still it prints out true ? why ?
Because it is true - and mathematics says that there is only one neutral
element for addition and that is 0. And whether you call it 0.0 or -0.0
doesnt matter, because it still is the same.
bye, Ralf
gk - 14 Sep 2006 15:32 GMT
thanks .
nice explanation.
> gk schrieb:
> > System.out.println(Math.min(-0.0,+0.0));
[quoted text clipped - 20 lines]
>
> bye, Ralf
Oliver Wong - 14 Sep 2006 15:37 GMT
> gk schrieb:
>> System.out.println(Math.min(-0.0,+0.0));
[quoted text clipped - 6 lines]
>> 0.0
>> true
[...]
>> still it prints out true ? why ?
> Because it is true - and mathematics says that there is only one neutral
> element for addition and that is 0. And whether you call it 0.0 or -0.0
> doesnt matter, because it still is the same.
Careful: IEEE floating point doesn't always match the mathematical rules
of real or rational number arithmatic. Addition is not always associative in
IEEE, whereas it is always associative "in math".
- Oliver
Patricia Shanahan - 14 Sep 2006 16:15 GMT
> System.out.println(Math.min(-0.0,+0.0));
> System.out.println(Math.max(-0.0,+0.0));
[quoted text clipped - 11 lines]
>
> still it prints out true ? why ?
The core problem here is that the IEEE 754 zeros have two functions:
1. A representation of true zero, the additive identity element. A true
zero is less than all positive numbers, greater than all negative
numbers, and does not itself have a meaningful sign.
2. A way of representing incredibly small numbers. It is useful, in some
algorithms, to preserve the sign even when a number is too small to
represent any part of the magnitude. It makes the difference between 1/x
being positive or negative infinity. It can allow some code to work
without explicit overflow or underflow checking.
However, it does lead to a slightly split personality. To make the true
zero behavior work right, it is essential to treat both zeros as being
equal. To make the sign-preserving underflow behavior work, the min of
the two zeros is the negative one etc.
Patricia
Lew - 29 Sep 2006 03:18 GMT
>> System.out.println(Math.min(-0.0,+0.0));
>> System.out.println(Math.max(-0.0,+0.0));
[quoted text clipped - 5 lines]
>> 0.0
>> true
http://docs-pdf.sun.com/800-7895/800-7895.pdf