Why does java return a -0.0 when you divide 0.0 by -1. It just seems very strange that the definition of negative numbers is broken when you do the above in Java. A negative number is something that is less than 0. So how can you denote 0.0 as negative.
Asked
Active
Viewed 99 times
2
-
3Please share your code snippet – Hammad Sajid May 07 '19 at 05:28
-
1What you see as 0.0 may not be zero exactly but a very small number. – Thorbjørn Ravn Andersen May 07 '19 at 05:30
-
Actually, in mathematics `-0` and `0` are not equal. `-0 = [x->-infinite] 1/x`, which makes it a negative number. But in java, it shouldn't change anything. – Paul Lemarchand May 07 '19 at 05:35
-
@Paul Lemarchand in Java `0 = -0 = -0.0 = +0` please don't misinterpret things – ShivCK May 07 '19 at 05:37
-
1@ShivCKushawah (that's what I said at the end of my comment) – Paul Lemarchand May 07 '19 at 05:38
-
1@Paul Lemarchand in your first line what is is just contrary that's why I thought that way. Btw good. – ShivCK May 07 '19 at 05:44
2 Answers
3
Floats are calculated according IEEE-754. This standard includes positive and negative zero, NaN(Not a Number) and positive/negative infinity.
You can access these special numbers using constants(like Double.NEGATIVE_ZERO
).

dan1st
- 12,568
- 8
- 34
- 67
0
See here for some details but essentially
-0.0
represents the result of a rounding to zero of numbers in the range between zero and the smallest representable non-zero negative number.
Some interesting background reading here.

OldCurmudgeon
- 64,482
- 16
- 119
- 213
-
1Why not close as duplicate instead of answering with a link to an already existing answer? – Oleg May 07 '19 at 06:06
-
-
This question: "So how can you denote 0.0 as negative". The other question: "How can a primitive float value be -0.0" seems the same to me. Even if it isn't such a long time and high rep user as yourself should be well aware that the answer is what's important. "As long as an existing answer provides the solution to the general problem and can easily be adapted to the OP's specific situation, it is a duplicate." https://meta.stackoverflow.com/a/312160/1398418 – Oleg May 07 '19 at 06:53