Let’s do some simple math. What is 0.1 + 0.2? It’s 0.3. Very simple, isn’t it? Let’s see what your computer says. Open your browser console (press Ctrl + Shift + J on chrome or Ctrl + Shift + I on firefox and then got to console tab) and type 0.1 + 0.2 and press enter. What do you see?

Interesting, isn’t it? You can also try 0.3 + 0.6. If you think that, javascript is a bad language and it can’t handel math properly, then you are wrong. You can also try this in java or in python or in c or any of you favorite language. If you don’t want to run all the languages please have a look at https://0.30000000000000004.com/ . The question is why. Why all programming languages are unable to handle so simple math? The answer is it’s not a language issue. It’s a more general computer problem.

If I say in simply way, It’s rounding error. When we have to handle decimal numbers like 0.33333… or any other decimal numbers that goes forever, (i.e. pi = 3.14159…..) we have to round it at a point. For example we write pi = 3.1416. But 3.1416 is not equal to 3.14159… There is a small error here. This error is called rounding error.

In decimal system or base 10 system 0.1 or 0.2 is not a complex decimal number. But let’s try to convert them in binary system or base 2 system. For 0.1 we get

0.1 * 2 = 0.2 → 0
0.2 * 2 = 0.4 → 0
0.4 * 2 = 0.8 → 0
0.8 * 2 = 1.6 → 1
0.6 * 2 = 1.2 → 1
0.2 * 2 = 0.4 → 0
0.4 * 2 = 0.8 → 0

So  (0.1)10 = (0.000110011001100……………)2
And  (0.2)10 = (0.001100110011001……………)2

Here is the main problem. If we convert 0.1 or 0.2 into binary number, then it goes forever. As the computer has to store numbers within a limited bit (32 bit or 64 bit or higher), it has to round the complex floating point number in a certain point. That’s why rounding error occurs.

To understand more clearly, why 0.1 + 0.2 = 0.30000000000000004 or 0.3 + 0.6 = 0.8999999999999999 in computer, we have to understand “floating point arithmetic”. How computer handle floating point numbers? In computer all numbers or anything is represented in binary format. There are many algorithm to represent floating point numbers in binary. Here is the most common algorithm that is used to represent a floating point number in binary (IEEE 754 Floating Point Representation).

Step 1: Normalization:-
            In our real world we use a dot “.” to write floating point numbers(i.e. 0.12). But in computer, there is no dot. So how can we represent floating point numbers? That’s why normalization comes in.

            In case of decimal numbers, 12.324 can be written as 12324 × 10−3   In the same way in binary we can normalize a binary number. So for 1101.1001 we can write 11011001 × 2−4

In a normalization step the floating point number is normalized as

Step 2: Bit Representation:-
            To represent a floating point number in bit, computer use a fixed number of bits. For 32 bit representation, the first bit is sign bit. It defines the number is positive or negative. Next eight bit is exponent bits those are used to store the exponential part of the normalized floating point number. And the remaining 23 bits are called mantissa or significant bits those are used to store the number in binary format.

So, for 32 bit floating point number representation we get 23 bits to store the significant bits. If a floating point number has more than 23 bits or 23 digits in binary system (we need one bit to store a binary digit) we must round it. For example, in case of

(0.1)10 = (0.000110011001100……………)2 ,  

After normalization 

mantissa = 100110011001100110011001…………..

But we can store only 23 bis. So we have to round the bits. For rounding we convert the 23th 0 to 1. Here is the error.

If you are interested to learn more about “Floating Point Representation and Rounding Error”, you can see the youtube video   https://www.youtube.com/watch?v=wbxSTxhTmrs

Previous ArticleNext Article

This post has 7 Comments


Leave a Reply

Your email address will not be published. Required fields are marked *