Java floating point clarification -
i reading java puzzlers joshua bloch. in puzzle 28, not able understand following paragraph-
this works because larger floating-point value, larger distance between value , successor. distribution of floating-point values consequence of representation fixed number of significant bits. adding 1 floating-point value sufficiently large not change value, because doesn't "bridge gap" successor.
- why larger floating point values have larger distances between values , successors?
- in case of
integer
, add 1 nextinteger
, in case offloat
, how nextfloat
value? if have float value in ieee-754 format, add 1 mantissa part next float?
you can think of floating point base-2 scientific notation. in floating point, limited fixed number of bits mantissa (a.k.a. significand) , exponent. how many depends on whether using float
(24 bits) or double
(53 bits).
it's little more familiar think of base-10 scientific notation. imagine mantissa limited integer , represented 3 significant digits. consider these 2 pairs of successive numbers in representation:
- 100 x 100 , 101 x 100 (100 , 101)
- 100 x 101 , 101 x 101 (1000 , 1010)
note distance (a.k.a. difference) between numbers in first pair 1, while second pair 10. in both pairs, mantissas differ 1, smallest difference there can between integers, difference scaled exponent. that's why larger numbers have bigger steps between them in floating point (your first question).
regarding second question, let's @ adding 1 (100 x 10-2) number 1000 (100 x 101):
- 100 x 101 + 100 x 10-2 = 1001 x 100
but limited 3 significant digits in mantissa, last number gets normalized (after rounding) to:
- 100 x 101
which leaves @ 1000. change floating point value, need add @ least half difference between number , next number; minimum difference varies scale of number.
exactly same kind of thing going on binary floating point. there more details (e.g., normalization, guard digits, implied radix point, implied bit), can read in excellent article what every computer scientist should know floating-point arithmetic
Comments
Post a Comment