2

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.

Bhavya
  • 43
  • 6

2 Answers2

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
  • 1
    Why not close as duplicate instead of answering with a link to an already existing answer? – Oleg May 07 '19 at 06:06
  • @Oleg Because that question is about printing `-0.0`. – OldCurmudgeon May 07 '19 at 06:44
  • 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