Correct behaviour of mod operation compared to Google calculator

484 Views Asked by At

What is happening here? Why is the first calculation returning an integer, but not the second calculation? What are the rules for computing mod for floats and negative numbers?

2

There are 2 best solutions below

0
On

I just think it extended classic rules. $$2\times\frac{21}{2} - 16 = 5$$ So $-16 = 5 \mod \frac{21}{2}$

0
On

Mathematically $x\bmod y$ means $x-ky$ with $k\in\Bbb Z$ such that $ky$ is the largest integer multiple of $y$ for which $ky\leq x$ (at least this is the definition when $y>0$; when $y<0$ conventions vary). In your example $k=-2$ both times, so $ky=-21$ respectively $ky\approx-21.069$.

By the way, most computer hardware designers either didn't understand the mathematical meaning of $\bmod$ our found it too hard to implement, so that most computer hardware implements a different remainder operation (at least when $x,y$ are integers). See here. But the calculator software does seem to get it right here.