Home>America>education>Floating Point Math | Floating point type operations

Floating Point Math | Floating point type operations

Floating Point Math

Your language is not damaged, it is performing floating-point operations. Computers themselves can only store integers, so they need some way to represent decimal numbers. This representation is not entirely accurate. That's why, often not, 0.1+0.2= 0.3.

Why is that?

This is actually quite interesting. When you have a system with a base of 10 (such as our system), it can only represent the fraction of prime factors using cardinality. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be clearly expressed because the denominator uses a prime factor of 10. In contrast, 1/3, 1/6, 1/7, and 1/9 are all repeating decimals because their denominators use prime factors of 3 or 7.

In binary (or base 2), the only prime factor is 2, so you can only clearly express fractions with a denominator of only 2 as the prime factor. In binary, 1/2, 1/4, and 1/8 can all be clearly represented as decimals, while 1/5 or 1/10 will be repeated decimals. So, although 0.1 and 0.2 (1/10 and 1/5) are clean decimals in systems based on 10, they are repeated decimals in systems based on 2 used by computers. When you perform mathematical operations on these repeated decimals, you will eventually get the remainder, which will be carried over when you convert the base-2 (binary) digits of the computer to a base-10 representation that is easier for humans to read.