[Tutor] The remainder %

Bob Gailer bgailer@alum.rpi.edu
Tue Jun 3 11:58:23 2003


--=====================_10823152==.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 02:09 PM 6/3/2003 +0200, Guillaume wrote:

>Hello,
>I'm not very good in maths and I don't understand why
>14 % 3 = 2
>Could someone explain me this mystery?
>Thanks in advance :)

What's really frustrating here is that the language reference says "% 
(modulo) operator yields the remainder..."

In math modulo is NOT the same as remainder. At least the explanation says 
"remainder". Modulo and remainder have the same values when the left 
argument is >= 0, and different values for < 0. Example:
n   n modulo 3  n % 3
3           0            0
2           2            2
1           1            1
0           0            0
-1          2            1
-2          1            2
-3          0            0

The only programming language I know that "got is right" is APL. There the 
function is named modulo and it DOES modulo. Most other languages call it 
modulo and it does remainder.

Bob Gailer
bgailer@alum.rpi.edu
303 442 2625

--=====================_10823152==.ALT
Content-Type: text/html; charset="us-ascii"

<html>
<body>
At 02:09 PM 6/3/2003 +0200, Guillaume wrote:<br><br>
<blockquote type=cite class=cite cite><font face="Arial Narrow, Helvetica">Hello,</font><br>
<font face="Arial Narrow, Helvetica">I'm not very good in maths and I
don't understand why </font><br>
<font face="Arial Narrow, Helvetica">14 % 3 = 2</font><br>
<font face="Arial Narrow, Helvetica">Could someone explain me this
mystery?</font><br>
<font face="Arial Narrow, Helvetica">Thanks in advance
:)</font></blockquote><br>
What's really frustrating here is that the language reference says
&quot;% (modulo) operator yields the remainder...&quot;<br><br>
In math modulo is NOT the same as remainder. At least the explanation
says &quot;remainder&quot;. Modulo and remainder have the same values
when the left argument is &gt;= 0, and different values for &lt; 0.
Example:<br>
n&nbsp;&nbsp; n modulo 3&nbsp; n % 3<br>
3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0<br>
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2<br>
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1<br>
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0<br>
-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1<br>
-2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2<br>
-3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0<br><br>
The only programming language I know that &quot;got is right&quot; is
APL. There the function is named modulo and it DOES modulo. Most other
languages call it modulo and it does remainder.<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>

--=====================_10823152==.ALT--