### When 2+2 != 4

I was reminded of something I learned way back in my third year of computer science 'Numerical Method's' the other day..the fact that computers can't really do math!

No I'm not taking about the Pentium flaw from 1994, which was a big thing back on those days. I'm taking about the magic number *e*_{min}_{ }

Many people know that computers only speak in binary (1's and 0's). So everything you see on the screen has to be somehow represented as a sequence of 1's and 0's.

In Mathematics we have irrational numbers, and repeating decimals. 1/3 for example is an infinite sequence of 0.3333333, and Pi is a long infinite sequence of numbers that does not even bother to repeat (so far as anyone has calculated so far)

So the other day, I was doing some calculations on currency. Specifically it was a sequence of additions and subtractions. The number I was adding/subtracting was 5.32. Here is the debug of the output of that program.

**WTF?**

And that's when I remembered my 3rd year numerical methods class!

The big secret - is that as smart as people think computers are - They can't really do math on REAL (ie: repeating decimals / irrational) numbers. Instead they approximate it using a technique known as Floating Point Arithmetic

You can experience this yourself with pretty much any calculator. Start with the number 1 and divide it by 7 over and over again... say at least 50 times. Then re-multiply by 7 those same number of times. You should get back to 1, and many times you will get back to 1. But eventually if you keep trying, you will find not the answer back at '1' but instead at something like 0.99999999999999999999999999998. Depending on the brand of calculator, the number of divides/multiples.

On a modern computer this can be much more difficult to reproduce because a) the computer uses more memory to store the numbers and b) a lot of calculator software 'cheats' so as to round when it thinks you are trying to 'fool' it so that you don't see the 'wrong' answer.. but none-the-less we can reproduce it.

In fact, for any machine / programming language combination we can calculate emin as follows

e(min) = smallest representable number such that adding "1" will still produce a number greater than 1

Consider the following simple algorithm:

float e float sum float num float tmp e=0; sum=2; num=1; while (sum > 1) { tmp=1/num sum = 1+ tmp e=tmp num++

} print "e(min) IS " + e---

**but never equal zero (ie: it's an asymptote)**

Related: What Every Computer Scientist Should Know About Floating-Point Arithmetic

_{}

## Comments

## Post a Comment