On Aug 10, 2006, at 2:53 PM, Daniel Berger wrote:

> Help a guy out who got B's and C's in math classes in college:
>
> Ruby 1.8.4 on Solaris 10:
>
> irb(main):013:0> -1234567890987654321.remainder(13731)
> => -6966
> irb(main):014:0> -1234567890987654321.remainder(13731.24)
> => -9906.22531493148
> irb(main):015:0> -1234567890987654321.modulo(13731)
> => 6765
> irb(main):016:0> -1234567890987654321.modulo(13731.24)
> => 3825.01468506852
>
> Basically, I'm trying to figure out the nuances of Bignum#remainder  
> vs Bignum#modulo.  To confuse myself even further K&R (2nd ed, p.  
> 41) says you can't do modulus on double or long, and that the sign  
> result is machine dependent for negative operands.
>
> Can someone explain the subtleties of this to me (or point me to a  
> link that does)?  An archive search didn't reveal anything obvious.

A couple of years ago I took a discrete math course and was very  
surprised to find division defined as:

"Let a be an integer and d a *positive* integer. Then there are  
unique integers q and r, with 0 .le. r .le d,  such that a = dq + r.  
Apparently my elementary school teacher had lied to me by saying that  
division was even defined for a non-positive divisor. Furthermore, an  
examination of the above definition will show that my calculator has  
always lied to me for negative dividends since the remainder must by  
definition be non-negative. This led me to examine how various  
languages implemented the mod function for integers and reals of  
various sizes and, sure enough, it's inconsistent from one language  
to the next.

The entry at:
<http://en.wikipedia.org/wiki/Modulo_operation>
is OK but it could be a bit more in-depth.

--
In America, anybody can be president. That's one of the risks you take.
-Adlai Stevenson, statesman (1900-1965)