Skip to content

Commit b4fbb85

Browse files
committed
fix: add explanation for zero representation in IEEE-754 floating point
1 parent dee4663 commit b4fbb85

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

topics/numbersystems.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,25 @@ Final float binary:
384384
0 10000101 10010001 000000000000000
385385
----
386386

387+
=== Special Case: Zero
388+
389+
The formula always adds an implicit `1` (`1 + mantissa`), so the smallest normal number we can build is `1.0 x 2^(exponent - 127)`. This means there is no combination of bits that gives us exactly `0`, and we also cannot normalise `0`: there is no leading `1` bit to shift to.
390+
391+
IEEE-754 solves this by reserving the exponent value `0000 0000`. When the exponent and the mantissa are all zero, the value is defined to be zero (the implicit `1` is dropped):
392+
393+
[source]
394+
----
395+
0 00000000 00000000000000000000000 -> +0
396+
1 00000000 00000000000000000000000 -> -0
397+
----
398+
399+
Because the sign bit is independent, we even get both `+0` and `-0` (they still compare as equal in Java).
400+
401+
The same trick is used for a few other values that cannot be written in normal form:
402+
403+
* exponent all `1`s, mantissa `0` → ± infinity
404+
* exponent all `1`s, mantissa not `0` → NaN (not a number)
405+
387406
=== Why 0.1 Can't Be Represented Exactly
388407

389408
There are some problems with floating point numbers, one of them is that not every number can be represented correctly.

0 commit comments

Comments
 (0)