Skip to content Skip to sidebar Skip to footer

Float_info : Maximum Number Of Decimal Digits That Can Be Faithfully Represented

I was reading on the attribute sys.float_info.dig that is descirbed as : maximum number of decimal digits that can be faithfully represented in a float; This examples showcases

Solution 1:

"Faithfully" means only that the internal representation can still distinguish properly among the 10^n numbers that humans intend with that quantity of digits. In this particular example, it means that the computer will correctly round each of its internal values to a specific, human-expected 15-digit number, for all 10^15 numbers.

When we add one digit, it can no longer distinguish all the combinations: it doesn't have 10^16 sequences of bits. In this case, adjacent numbers get mapped to the same bit pattern; on converting back, we don't know which of the several possibilities was the original, and the conversion routine picks the one closest to the binary representation.

In any case, it does not mean that there will be no errors with 15-digit accuracy. We can still see plenty of problems with accumulated error when we combine numbers. For instance, binary cannot represent the fraction 1/3 perfectly. Try adding up 1/3, say, three thousand times, and you'll see the accumulated diversion from the expected result of 1000.0

Solution 2:

It means that if you have a string s representing a decimal float of the form

 mantissa "e" exponent

where

  • mantissa has no more than 15 digits

  • float(s) does not overflow to an infinity, underflow to a 0, and doesn't yield a subnormal number

then printing float(s) to 15 significant digits will yield a string representing the same mathematical value as s represents. And it means that this wordy mess isn't always true for any integer greater than 15.

About the "doesn't yield a subnormal" part, behold:

>>>3e-324
5e-324

It's easy to find examples for which 16 digits doesn't work, and you already found one. But it's not easy to prove that it will always work for 15 digits. Finite floating point numbers are a "lumpy" subset of rational numbers, where the gap between representable floats varies depending on magnitude. A correct proof generally requires looking at each possible range of floats, as sketched here:

http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/

Post a Comment for "Float_info : Maximum Number Of Decimal Digits That Can Be Faithfully Represented"