Multiplication of floating numbers to a modulus

490 Views Asked by At

As we all know, the integers follow the following identity :

$$(A\cdot B\cdot C) \bmod M = ((A\cdot B) \bmod M\cdot C) \bmod M$$

But it does not work for real numbers having fractional part. For example:

$$1.7\cdot 1.7\cdot 1.7 = 0.913 \bmod 2.0$$

whereas,

$$1.7\cdot 1.7 = 0.89 \bmod 2.0$$

and, $0.89\cdot 1.7 = 1.513 \bmod 2.0$

We see the two results are unequal. So, does the modular identities do not work with real numbers? If yes, is there any way I could do multiplication of real numbers to get the expected result?

1

There are 1 best solutions below

0
On

They do not work this way. In your example, we have $1.7 \cdot 1.7 = 2+0.89$ and $1.7 \cdot 1.7 \cdot 1.7 = (2+0.89)\cdot 1.7 = 3.4 + 0.89 \cdot 1.7$. The reason it works with integers is the term that wound up $3.4$ here would have been an even integer, so could have been ignored when working $\pmod 2$

For reals I don't know of a workaround. It really depends on the "granularity" of the integers.