Search code examples
floating-pointdoubletheory

Why do doubles work how they do


I know that this may be a common question, but I have never found an answer (and maybe it has to do with not knowing how to search google correctly, so if somebody can point me to a reference, I will remove this question).

Why do doubles work how they do in terms of representing the right side of the decimal with an inverse power of 2, and the left side of the decimal with a power of 2? I know that it allows very large numbers to be represented, but are there any other advantages? The .NET framework has the decimal data structure available, which seems much more logical to use becuase it is how we represent numbers in human notation.

Really, my question is why doubles were created the way they were instead of initially creating something like decimal instead (which seems to be far less common).


Solution

  • Your confusion seems to be unfounded. The right side of the decimal point is always represented in an inverse power of the base and the left side is always represented as a power of the base. This is true for base 10 and base 2 as well. Binary floating point numbers store an exponent that controls where the decimal point is on the mantissa.

    As for why they exist: binary floating point notation has two convenient properties:

    1. It is relatively fast, because it uses binary arithmetic
    2. It can represent either very large or very small numbers with certain accuracy.

    Those properties make them pretty good for e.g. physical calculations, because a small error in the last place doesn't matter much, but make them unusable for monetary applications (where you want decimal, despite it being much slower for computation).