Wednesday, October 3, 2012

Numbers part 10

In my last blog post about numbers, I talked about the issues programmers can encounter when dealing with whole numbers.  Just like elementary school, things get a lot more complicated when we add fractions.

Most decimal numbers on computers are stored using the IEEE floating point specifications.  To greatly summarize the standard, binary digits after the decimal point are no longer stored as double the digit before them, but rather 1/n where n is double the previous digit.  This can cause some peculiarities, as most numbers result in a repeating decimal (much like 1/3 does in base 10).  In this system, any fraction that does not have a denominator that is not Dyadic will be repeating and will likely result in rounding errors.

This raises the question: Why on earth are we storing numbers like this?  Can't we just store the number after the decimal as another whole number and ditch all the rounding errors?  As it turns out, I was wondering the same thing and asked the kind folks at programmers.SE.  As it turns out, most calculators do use an alternate storage method that avoids these errors (at least in base two).  The problem is that doing it this way takes up a lot more space than above.

Almost all of the time, the only people worrying about these errors will be mathematicians.  Outside of research, the place you're most likely to encounter this is when you a coding an application that handles money.  Fortunately, this has a simple answer: Multiply the money by 100 (making it a whole number) do all the math/taxes/etc

No comments:

Post a Comment