top of page

Is Floating-Point math broken?

Have you ever wondered why most of the programming language has below-wired behaviour?

Console.WriteLine("{0:R}", .1 + .2);
// Output :- 0.30000000000000004

You may think that it is a standard programming bug or a broken feature that has yet to be fixed, but let me tell you, there is an excellent reason behind this.


Although I am not a maths professor, I will try to explain it in detail and provide materials for further readings for all interested.



FLoating point problem

Why does a floating-point math problem happen?

As you may know, our typical number system is a base 10, decimal system.

However, the computers are binary, also known as Base 2 systems. So, expressing one type of number in another system sometimes creates an issue. Now, what exact issue? Let's see that.


When using the decimal or base 10 system, we can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators can be broken into prime factors 10. On the other hand, 1/3, 1/6, 1/7 and 1/9 are all repeating decimals because their denominators use a prime factor of 3 or 7.


For the base 2 system or binary system, the only prime factor is 2, so 1/2, 1/4, and 1/8 would all be expressed cleanly as decimals, but 1/5 or 1/10 would be repeating decimals.


So 0.1 and 0.2 (1/10 and 1/5), while clean decimals in a base-10 system, repeat decimals in the base-2 system used by the computer. Whenever there is a mathematical operation on repeating digits, they end with some remaining values or leftovers when converted from base 2 to base 10 representation.


This carrying over of the remaining values results in the wired value, as seen above.


Further reading


3,467 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page