One of the most reported bugs in JavaScript is “Math is broken!” (see this for an example).

This issue normally manifests itself with something like the following:

0.1 + 0.2 != 0.3

And I think you could probably be forgiven for expecting the answer to this to be 0.3…

However open up the console in your favourite browser and you will find you have the following answer:


Whilst some of you might know this problem has got something to do with floating point do you know why beyond this?

Well it turns out quite a lot of people don’t (and I didnt either beyond "floating point weirdness" before researching this area) and a quick Google search will bring up literally thousands of questions regarding this or a similar issue.

And don’t think this issue is constrained to JavaScript either. It happens in .NET too.

A very brief and western centric history of math

Prior to the 1200’s in the western world the Roman numeral system was pretty much the only number system in use and for many purposes this worked pretty well. It probably wasn’t so great however for performing calculations) as it quickly became confusing. For example imagine working out XCVIII (which is 98 in case you are wondering) + LXXI (71) = ?

Fibonacci in 1202 – yep the very same Fibonacci responsible for a well-known number sequence (which is inspired by a theoretical problem about rabbits mating - also remarkably a good predictor of city population growth even today) brought the Hindu/Arabic number system to the western world via his best-selling book Liber Abacai.

Liber Abaci was an important book as it set out and standardized a complete number system and rules of how numbers worked together. It was also especially interesting as provided examples of how to use this system in commerce which may well have been responsible for its success. The number system described in Liber Abaci is still very similar to what we use today.

Counting in 10s

As humans we like to count in 10s - no one knows for sure why this is so but it seems likely this might be because we have 5 fingers on each hand. Anyway in our number system each number column represents a power of 10.

For example the number 205 reading the columns left to right is actually:

(2x 100) + (0x 10) + (5 x 1) = 205

This method is so ingrained into us that you probably no longer consciously about adding the various columns as powers of 10.

Counting in 2s

Computers and robots (+ Keanu Reeves) on the other hand like to count in 2’s.

The reason for this is that it would be very expensive to build an electronic component capable of holding 10 different states and it’s much easier to build one that can hold two – on/off much like a switch.

This number system is of course called binary. With binary instead of each column representing a power of 10 each column is a power of 2 with the first column on the right representing 1.

Thus the binary number 1001 (reading the columns left to right) =

8 + 0 + 0 + 1 = 9

Fractions in binary

You can also hold fractions in binary. Working from left to right each column represents half the value of the one before with the first 0.5.

So working from the left the columns would have the values: 0.5, 0.25, 0.125, 0.0625 and so on.

So 1100 in binary fraction representation is:

0.5 + 0.25 = 0.75

Storing numbers

I want you to imagine you were developing a system to store numbers and had 64 bits of space to do so. How might you go about storing a value?

One option is just to use all the 64 bits of space like so:

10101010 01001111 00101010 10101010 00110110 01010010 01111001 10101010

You’ll be able to store a fairly large number this way.

However this has an issue!

You’ll also need to be able to store fractional values – uh oh!

Well maybe you solve this by having a convention & sticking a decimal point in the middle (32 bits in) like so:

10101010 01001111 00101010 10101010 . 00110110 01010010 01111001 10101010

Now this system is in use & does have applications but it’s not the most efficient way possible and what are you going to do about negative numbers and special numbers such as numbers which can’t be represented (NaN) or infinity eeek!

Well there is a more efficient way to store numbers and one of the most popular methods is defined in as a standard called IEEE-754 - the IEEE bit stands for Institute of Electrical and Electronic Engineers if you are wondering and we will talk more about this in my next post.

Continue reading part two of this series.