Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't decimal numbers be represented exactly in binary?
    primarykey
    data
    text
    <p>There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn't have an exact binary representation, so it's dangerous to use the == operator to compare it to another floating-point number. I understand the principles behind floating-point representation.</p> <p>What I don't understand is why, from a mathematical perspective, are the numbers to the right of the decimal point any more "special" that the ones to the left?</p> <p>For example, the number 61.0 has an exact binary representation because the integral portion of any number is always exact. But the number 6.10 is not exact. All I did was move the decimal one place and suddenly I've gone from Exactopia to Inexactville. Mathematically, there should be no intrinsic difference between the two numbers -- they're just numbers.</p> <p>By contrast, if I move the decimal one place in the other direction to produce the number 610, I'm still in Exactopia. I can keep going in that direction (6100, 610000000, 610000000000000) and they're still exact, exact, exact. But as soon as the decimal crosses some threshold, the numbers are no longer exact.</p> <p>What's going on?</p> <p>Edit: to clarify, I want to stay away from discussion about industry-standard representations, such as IEEE, and stick with what I believe is the mathematically "pure" way. In base 10, the positional values are:</p> <pre><code>... 1000 100 10 1 1/10 1/100 ... </code></pre> <p>In binary, they would be:</p> <pre><code>... 8 4 2 1 1/2 1/4 1/8 ... </code></pre> <p>There are also no arbitrary limits placed on these numbers. The positions increase indefinitely to the left and to the right.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIn binary, the number 3 is represented as 2¹+2°=2+1. Nice and easy. Now, take a look at 1/3. How would you represent that, using negative powers of 2? Experiment a little and you'll see that 1/3 equals the sum of the infinite sequence 2^-2 + 2^-4 + 2^-6 + 2^-8 + ..., ie. not that easy to represent exact in binary.
      singulars
    2. COJon Skeet answers the question in your body very well. One thing that is missing is that you actually ask two different questions. The title question is "why can't decimal numbers be represented exactly in binary?" The answer is, they can be. Between your title and body you conflate the idea of "binary" and the idea of a "floating point representation." Floating point is a way of expressing decimal numbers in a fixed number of binary digits at the cost of precision. Binary is just a different base for counting and can express any number decimal can, given an infinite number of digits.
      singulars
    3. COThere's several systems that have exact decimal representation. It works pretty much like you describe. The SQL decimal type is one example. LISP languages have it built in. There are several commercial and opensource libraries for using exact decimal calculations. It's just that there's no hardware support for this, and just most languages and hardware out there implements the IEEE standards for representing an infinite amount of numbers in 32 or 64 bits.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload