From: Daniel Berger Date: 2006-08-11T07:26:28+09:00 Subject: Re: Confused by Bignum#remainder Rick DeNatale wrote: > On 8/10/06, 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. >> >> Perhaps the documentation in bignum.c needs further exposition? Or >> should I >> just *know* this? > > Well perhaps the c code isn't the best place to understand ruby > semantics. I'd look at the class library documentation > http://www.ruby-doc.org is a good online resource for that. > If we look at the Numeric class (from which all the standard numeric > classes descend: > http://www.ruby-doc.org/core/classes/Numeric.html > > And poke around at the modulo and remainder method definitions, we > find that remainder is defined in terms of modulo, so that > dividend.remainder(divisor) is divendend.modulo(divisor) if dividend > and divisor have the same sign, and dividend.mod(divisor)-divisor > otherwise. Ah, hah! I get it now. > And if we look at mod we find that it's implemented as if it called > divmod in whose specification we find a nice table showing how modulo > and remainder are related under various conditions. > http://www.ruby-doc.org/core/classes/Numeric.html#M000032 Yep, that cleared it right up. I should have looked. Many thanks, Dan This communication is the property of Qwest and may contain confidential or privileged information. Unauthorized use of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy all copies of the communication and any attachments.