Search code examples
floating-pointcomputer-scienceprecision

Are there any binary values that don't have exact representation in decimal?


We all know that there are values in decimal that don't have an exact representation in binary.

For example the decimal value 0.1.

In IEEE floating point format it can have different representations depending on how many bits you want to dedicate to representing a floating-point value.

Single Precision (32-bits)

  • Hex: 0x3DCCCCCD
  • Binary: 1.10011001100110011001101×10-4
  • Decimal: 0.10000 00014 90116 11938 47656 25

Double Precision (64-bits)

  • Hex: 0x3FB999999999999A
  • Binary: 1.1001100110011001100110011001100110011001100110011010×10-4
  • Decimal: 0.10000 00000 00000 00555 11151 23125 78270 21181 58340 45410 15625

Extended Precision (80-bits)

  • Hex: 0x3FFBCCCCCCCCCCCCCCCD
  • Binary: 1.100110011001100110011001100110011001100110011001100110011001101×10-4
  • Decimal: 0.10000 00000 00000 00000 13552 52715 60688 05425 09316 00108 74271 39282 22656 25

In other words, the decimal value 0.1 does not have an exact representation in binary, it is endlessly repeating value:

0.0 0011 0011 0011 0011 0011 0011 0011 0011 ...
0.0 ̅0̅0̅1̅1

In the same way 1⁄3 has no exact representation in decimal - its digits after "decimal point" keep repeating forever:

0.33333 33333 33333 33333 33333 33333 33333 ...
0.̅3

But does it go the other way?

Are there any values in binary that don't have an exact representation in decimal?

Why am I asking? To extend the bounds of understanding, and add to the sum of human knowledge.

Edit: I don't know why someone is voting to close as a duplicate when the linked question is literally the exact opposite of mine.


Solution

  • No, every binary floating-point number has an exact representation in decimal.

    The numbers that can be represented exactly in binary floating-point (with a finite number of bits) are precisely those rational numbers which can be written with a denominator which is a power of 2, i.e. x = a/2n for integers a and n.

    The numbers that can be represented exactly in binary floating-point (with a finite number of digits) are precisely those rational numbers which can be written with a denominator which is a power of 10, i.e. y = b/10n.

    But every number from the first category is also in the second category, since a/2n = a•5n/10n.

    More generally, every number with an exact representation in base u will also have an exact representation in base v, if and only if every prime factor of u is also a prime factor of v. This will ensure that 1/un can be written as a/vm for some a and m.

    This explains the asymmetry: 2 is prime, and the prime factors of 10 are 2 and 5. So every prime factor of 2 is a prime factor of 10, but not the other way around.