Why can’t computers calculate decimals as floats properly?

238 views

Why can’t computers calculate decimals as floats properly?

In: 2

9 Answers

Anonymous 0 Comments

Decimals are coming you have a number of base 10. They are ways to represent fractions in a decimal number system 0.1 = 1/10 and 0.01 = 1/100 so a multiple of 1/10^n

The standard floating point number computer use is binary numbers and uses a binary fractions, not decimals. So the first number has a value of 1/2 the next of 1/(2*2) = 1/4 and so on that is a multiple of 1/2^n. The problem is 1/10 is not the sum finite amount of 1/2^n fraction so it can’t be exactly represented by a binary floating point number.

If I am not misstating so can all binary fractions be exactly represented by decimal fractions exactly. The reason is 1/2 = 0.5 and all other of them will be 0.5 multiplied by itself.

It is not because base 10 is special. 10 can be evenly divided by 5 and 2. That means bases 5 and 2 work fine. So do bases 4 and 8 because they are 2 x 2 and 2 x 2 x 2 and any other number that just has the prime factors 2 and 5. This means base 3 6 7 9 11 12 13 14 17 and so on have fractions that not can be exactly represented with decimal number. A simple example is 1/3= 0.33333333 and so on forever. But in base 3 it is exactly 0.1

So because 10 have the prime factors 2 and 5 but 2 only have 2 there are decimal fractions that can be exactly be represented by a binary fraction.

That said you can have a floating point number with base 10. Scientific notation is just that so 1.25 * 10^3 is a footing point number equal to 1250. 1.1 * 10 ^-2 =0.11 You can do calculations like that on a computer too there is just not a dedicated hard wart to it and you need do the maths with regular instruction. It can be done and you can fide libraries that do that like https://github.com/libdfp/libdfp

You are viewing 1 out of 9 answers, click here to view all answers.