From: "Benjamin J. Tilly" Date: 2001-05-08T08:03:48+09:00 Subject: [ruby-talk:14827] Re: Integer division oddness >===== Original Message From "Mike T. Miller" ===== >Ok, I think I understand now. But to clarify, when you mention >the range as [0,1) you are referring to the remainder with respect >to the denominator, right? I am talking the remainder as a floating point once you divide by the denominatory. Thinking integers the range is 0...d (note the triple ., that means that d is not in the range). >So given > > n % d = m > >then > > 0 <= m < d Supposed to be. >Is that it? But this doesn't hold if d is negative: 10 % -9 = -8 >Argh! Maybe I still don't quite get it. > You have it right, and I would call that answer a bug in Ruby. Thinking this through carefully can be surprisingly hard. I remember with my silly Math::Fleximal in Perl having to eventually just write down 4 cases first before I could get the logic right: 4/ 3 = 1* 3 + 1 4/-3 = -1*-3 + 1 -4/ 3 = -2* 3 + 2 -4/-3 = 2*-3 + 2 Given my druthers, this is the behaviour I want to see from division and modulus. Cheers, Ben PS Math::Fleximal is on CPAN for those who want to see absolutely pointless code...