0

I tried the following code:

System.out.println(0 / 1);
System.out.println(0 / -1);
System.out.println(0.0 / 1.0);
System.out.println(0.0 / -1.0);

And the result is:

0
0
0.0
-0.0

Why does the code evaluate to -0.0 when the divisor is a negative double?How can I get rid of the negative sign?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Gropai
  • 321
  • 1
  • 2
  • 11
  • 2
    you should read http://floating-point-gui.de/ –  Jan 20 '16 at 06:37
  • 3
    http://stackoverflow.com/a/8153449/4341456 – Daniel Jan 20 '16 at 06:38
  • Nothing to do with math 'imprecision' here - the linked guide entry is misleading. Simply, IEEE-754 (the form of representing *floating point values*) has the concept of a *negative zero* (as linked) which is being observed. The difference in *integer division* vs *floating point division* is what changes the result: there is *no -0 integer* value, but there *is a -0.0 floating point* value. – user2864740 Jan 20 '16 at 19:03
  • Also, note that although the negative concept/sign is present (and shown when converted to a string) there is the special property that `0.0 == (0.0 / -1.0)`. – user2864740 Jan 20 '16 at 19:11

0 Answers0