Java floating point clarification
floating-point, java
Solution
You can think of floating point as base-2 scientific notation. In floating point, you are limited to a fixed number of bits for the mantissa (a.k.a. significand) and for the exponent. How many depends on whether you are using a `float` (24 bits) or a `double` (53 bits).
It's a little more familiar to think of base-10 scientific notation. Imagine that the mantissa is limited to an integer and is always represented by 3 significant digits. Now consider these two pairs of successive numbers in this representation:
- 100 x 100 and 101 x 100 (100 and 101)
- 100 x 101 and 101 x 101 (1000 and 1010)
Note that the distance (a.k.a. difference) between the numbers in the first pair is 1, while with the second pair it is 10. In both pairs, the mantissas differ by 1, which is the smallest difference there can be between integers, but the difference is scaled by the exponent. That's why larger numbers have bigger steps between them in floating point (your first question).
Regarding the second question, let's look at adding 1 (100 x 10-2) to the number 1000 (100 x 101):
- 100 x 101 + 100 x 10-2 = 1001 x 100
but we are limited to only three significant digits in the mantissa, so the last number gets normalized (after rounding) to:
- 100 x 101
which leaves us back at 1000. To change a floating point value, you need to add at least half the difference between that number and the next number; this minimum difference varies with the scale of the number.
Exactly the same kind of thing is going on with binary floating point. There are more details (e.g., normalization, guard digits, implied radix point, implied bit), which you can read about in the excellent article What Every Computer Scientist Should Know About Floating-Point Arithmetic
Problem
I am reading Java puzzlers by Joshua Bloch. In puzzle 28, I am not able to understand following paragraph- This works because the larger a floating-point value, the larger the distance between the value and its successor. This distribution of floating-point values is a consequence of their representation with a fixed number of significant bits. Adding 1 to a floating-point value that is sufficiently large will not change the value, because it doesn't "bridge the gap" to its successor. - Why do larger floating point values have larger distances between their values and successors? - In case of `Integer`, we add one to get the next `Integer`, but in case of `float`, how do we get next `float` value? If I have float value in IEEE-754 format, do I add 1 to the mantissa part to get next float?