From: Mathieu Bouchard Date: 2001-05-08T06:52:01+09:00 Subject: [ruby-talk:14822] Re: Integer division oddness On Tue, 8 May 2001, Benjamin J. Tilly wrote: > >Getting -2 seems really wrong to me. I looked at the > >code for ruby and found this: (numeric.c: line 1056) > > if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) { > > mod += y; > > div -= 1; > > } > 3. If you use the % (ie modulus) operator by default you > get 8 (reflecting the "careful" rounding) but if you > have "use integer" in place you get -1 for an answer. > Note with item 3 that the answer that Perl gives *changes* > depending on whether you are hinting that you care about > speed. which is pretty much pointless for an interpreter, which spends most of its running time reading bytecodes and making hash lookups. by the way, i thought about this: the above code snippet could be rewritten as: int foo = -( (mod<0) ^ (y<0) ); mod += foo & y; div += foo; which is free of any kind of branching, and therefore should be faster. That is, if the difference was large enough to care about. (Ruby, in addition, has to make method lookups all over) matju