Why can’t computers calculate decimals as floats properly?

33 views
0

Why can’t computers calculate decimals as floats properly?

In: 2

The basic version is that it’s really tricky to represent decimals in binary, so we use approximations. Look up IEEE754 if you are curious about the inner workings, but that is far too complicated of a beast to describe here.

Most numbers are not possible to represent in a finite number of digits in the decimal form. 1/3 = 0.33333333333333333333333… repeating forever. In fact, there are a lot of (infinite amount) numbers that are such that they don’t have a regular pattern repeating, such as pi. For those numbers you would need infinite memory to write them down accurately, and since infinite memory cannot exist in real life, you need to compromise hard between being able to write numbers to a limited accuracy and buying a RAM stick the size of the planet to run Windows Calculator.

Computers work in binary, also known as base-2. So representing 1/2, 1/4 or even 3/64 can be done very accurately.

Problem is 10 is five times two (10 = 5 * 2). So to represent 1/10 accurately you need to store data in base-10. Any other base cannot store this fraction correctly.

To tag along what /r/pocok5 said – if a computer operated in trinary (base-3) it could represent 1/3 perfectly.

You can, we just have to understand what “properly” means in this context. In order to represent a larger span of numbers with the same amount of bit space you have to sacrifice accuracy. This means that, in a sense, each float number actually represents a *range* of numbers, not just a single number. By that I mean there are many numbers which map to the same specific float representation.

So when you give the computer a number then tell it represent it as a float, then do some arithmetic (or even just try to get back your original number) the answer you get from your computer is going to be “wrong.” But it’s not really wrong, it’s right according to the agreed upon protocol for how float numbers are supposed to work. The computer is doing everything properly it is just you have used an inappropriate format to get the answer you want.

When you choose to use float, you must accept that you will lose this level of accuracy for whatever operations you wish to perform and either account for that in other ways or use a different number format.

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