Why does Java print a negative sign in front of 0 when 0 is unsigned, equal and more generally accepted as correct?

In java, the output of the following code: 0.0,-0.0,-0.0. what is the reason for these different answers?

System.out.print((0.0 % -1)+","+(-0.0 % 1)+","+ (-0.0 % -1));
+4
source share
4 answers

The modulo operator simply takes the remainder as soon as you divide the number by this.

Divide 0 by -1 and you get 0, so the result is 0.

Floating dots and doubles really know the difference between -0 and +0, although, therefore, when you take the remainder from -0, you get -0, since this is still a valid number from 0 to 1 (or -1).

This is a quirk for working with floating point numbers and special properties 0, as well as for other numbers:

System.out.println((0.0 % -1)+","+(-0.0 % 1)+","+ (-0.0 % -1));

System.out.println((0 % -1)+","+(-0 % 1)+","+ (-0 % -1));

System.out.println((3 % -1)+","+(-3 % 1)+","+ (-3 % -1));

Output:

0.0,-0.0,-0.0 
0,0,0 
0,0,0

Since links were requested:

Floating points are defined in IEEE_754-1985:

http://en.wikipedia.org/wiki/IEEE_754-1985

, :

http://en.wikipedia.org/wiki/Negative_zero

, modulo :

IEEE 754 () , == C Java.

modulo >= 0 < , -0 = = ( -0 == 0), .

+6

float IEEE , .

+1

float . , , .

, , ; "" , . , ToString().

0

IEEE-754 . , "" "" .

0

All Articles