Decimal Arithmetic FAQ
Part 6 – Miscellaneous Questions
think 10
Copyright © IBM Corporation, 2000, 2007. All rights reserved.

Contents [back to FAQ contents]


How many decimal digits are needed to represent a binary floating-point number as an exact fraction?

When the exponent is zero, you need as many decimal digits as there are binary bits in the significand of the binary floating-point number (you get one power of two in each digit).

For example, the 64-bit IEEE 754 binary floating-point format (often called a ‘double’) has a 53-bit significand (52 bits explicit in the format and one implied). To represent this exactly needs 53 decimal digits.

Here’s a brief proof-by-example:

Consider the double which has all ones and an exponent of zero; that is, the binary fraction 1.1111111.... (53 one bits in all). That has the value 20 + 2−1 + 2−2 + .... + 2−52.

Now consider the first and last terms of that expansion:

  20 = 1

  2−52 = 0.0000000000000002220446049250313080847263336181640625

The total must be less than 2, so the first term (1) provides the leftmost digit of the total. Also, no term is smaller than 2−52, so there will be no digits further to the right than those of that term. The sum of these two terms is:

  1.0000000000000002220446049250313080847263336181640625

which has 53 digits.

Using a power of two

Note, however, that if we can assume a multiplier of 2−52 then we can express the same number exactly instead of as a decimal fraction by using the expression

  9007199254740991 × 2−52

which needs only 16 digits. But, if the value2 −52 is written out in decimal the expression becomes

  9007199254740991 × 0.0000000000000002220446049250313080847263336181640625

There are 37 significant digits in the right-hand term, again giving a total of 53 significant digits.

See also see item 2 in an earlier answer for a couple more examples where you need over 50 digits for the exact conversion of a binary double to a decimal fraction.

Non-zero exponents

Additional digits may be needed when the exponent is non-zero; probably up to 63 or 64 (again, one digit for each bit of information in the original number). Leading zeros (depending on the chosen format for the result) may also be needed.

How many decimal digits are needed to represent a binary floating-point number reversibly?

As described in the last answer, representing a binary floating-point (BFP) number as an exact decimal number needs as many decimal digits as there are bits in the binary number.

However, far fewer are needed for a safe reversible conversion (that is, converting a BFP number to decimal and then back to BFP giving the identical BFP to that which one started with). For the 64-bit IEEE 754 binary floating-point format (‘double’), it’s 17.

Why 17? Think of the significand as a 53-bit integer. This can have (simplifying slightly, but erring on the safe side) 253 different values. All we need to do is convert each of those values to a different decimal digit sequence (and ensure that converting back gives us the original binary integer). To do that we need a decimal number that can encode 253 or more values.

253 is 9007199254740992, which has 16 digits. To accomodate edge cases when taking into account the exponent it turns out one more digit than that is needed, which is 17 digits.

For more detail on this, see the Conversions section of links at the General Decimal Arithmetic page, especially Steele & White’s paper and others which cite it.


Please send any comments or corrections to Mike Cowlishaw, mfc@speleotrove.com
Copyright © IBM Corporation 2000, 2007. Parts © Mike Cowlishaw 1997, 2013. All rights reserved.