Miscalculation of rest of division

Asked

Viewed 56 times

-1

I’m studying and setting up programs. There’s a part I put in

     if litro % 3.6 == 0:

And when litro=36, the result doesn’t have to be true? For 36/3.6=10 and there’s nothing left.

But when I put that in the program the value is 3.59999999.... When I put

     55%5.5 == 0

the result is true.

I want to know why 55%5.5 stays 0 and 36%3.6 nay...

Can someone please help me?

1 answer

3

In general, it is all a consequence of understanding this question and its consequences.

Amazingly, unless you have a proper numerical system, when using a comma number, you will be using a binary floating point number, usually float or double. Each has its IEEE-defined specification, more specifically IEEE 754.

So, come on. I’m going to make a tough but true statement: there is no number 3.6 in the numeric system used (which is binary floating point), however there is 5.5. Watch the this video (in English) to better understand the subject.

When I say that "does not exist in the numerical system" I am referring to a representation finite; in a periodic tithe (which extends to infinity and beyond) there is representation.

To understand this, and to be able to prove it, one first needs to understand that the binary floating point is a kind of scientific notation using only binary digits. So, 5.5 is represented, finitely, by 1.011 * 2^2. How did I come to this conclusion? Follow below:

The highest power of 2 that fits in 5.5 is 4, that is 2^2. Thus, the first digit of scientific notation is 1 and the exponent is 2^2 (since we are working on binary digits). So, after we remove 4 leftover 1.5; the next power is 2^1 = 2, but it doesn’t fit in that rest, so now we have 1.0 in mantissa, the exponent will not change and will continue as 2^2 and we still need to fit 1.5. The next power is 2^0 = 1, and that number fits within 1.5, soon we have the mantissa of 1.01 under construction, missing fit 0.5. Then the next power is 2^-1 = 0.5, that fits exactly in 0.5, resulting in the mantissa 1.011 and exponent of 2^2. A table summarizing this table test follows below:

passo           | 0       | 1       | 2       | 3
potência        | 2^2 = 4 | 2^1 = 2 | 2^0 = 1 | 2^-1 = 0.5
resto anterior  | 5.5     | 1.5     | 1.5     | 0.5
cabe no resto?  | S       | N       | S       | S
resto novo      | 1.5     | 1.5     | 0.5     | 0
mantissa        | 1       | 1.0     | 1.01    | 1.011

And if we did it to 3.6? Well, the greatest power of 2 that fits within that number is 2^1 = 2. Let’s put on the table test?

passo           | 0       | 1       | 2          | 3           | 4            | 5             | 6
potência        | 2^1 = 2 | 2^0 = 1 | 2^-1 = 0.5 | 2^-2 = 0.25 | 2^-3 = 0.125 | 2^-4 = 0.0625 | 2^-5 = 0.03125
resto anterior  | 3.6     | 1.6     | 0.6        | 0.1         | 0.1          | 0.1           | 0.0375
cabe no resto?  | S       | S       | S          | N           | N            | S             | S
resto novo      | 1.6     | 0.6     | 0.1        | 0.1         | 0.1          | 0.0375        | 0.00625
mantissa        | 1       | 1.1     | 1.11       | 1.110       | 1.1100       | 1.11001       | 1.110011

The test has begun to become quite onerous, isn’t it? I can advance that decimal 0.1 cannot be represented by a finite number of binary houses, so we would end up in a periodic tithe (which in the case for 0.1 decimally is 0.0[0011] in binary, where the numbers [between brackets] are the repeating part of the tithe). See 1 2 3.

To complicate life, any integer can be represented as the sum of integer powers of 2. Therefore, the number 36 has exact representation. The highest power of 2 that fits in 36 is 2^5 = 32. Applying that table test we get the following:

passo           | 0        | 1        | 2       | 3
potência        | 2^5 = 32 | 2^4 = 16 | 2^3 = 8 | 2^2 = 4
resto anterior  | 36       | 4        | 4       | 4
cabe no resto?  | S        | N        | N       | S
resto novo      | 4        | 4        | 4       | 0
mantissa        | 1        | 1.0      | 1.00    | 1.001

Soon, 36 decimally can be written as 1.001 * 2^5 in binary scientific notation, which can be transformed into a binary integer 100100.


So back to the focus of the question: you have a finite representation number (36) being divided by an approximation of the infinite representation of another number (3.6, whose binary approximation I cannot express here). As the divisor is no longer the exact number, but an approximation of it, the dividend will no longer be multiple of the divisor. Then there will be, yes, rest in the division.

  • Um..... Thank you very much!!!!!!!!! Not totally, but manage to understand. In the next schedules that I will do, I will take care of this so as not to give error in the end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.