From: Rudolf Polzer Date: 2003-07-30T11:24:37+09:00 Subject: Re: -3 / 10 = -1 ?! Scripsit ille �John Andrews� : > Why does this program: > > print -3 / 10, "\n" > > print -1? > > If I underflow a negative int in C, I get zero. > > If I underflow a negative integer in Perl (using "use integer"), > I get zero. > > Why does Ruby give me -1? It's doing the "mathematical" division. Integer division with remainder is mathematically defined as follows: For a given a \in Z and b \in N without 0 you calculate q \in Z and r \in [0, b[ with a = bq + r q is the quotient (in Ruby a/b) and r is the remainder (in Ruby a%b). I'll write for this: div(a, b) = (q, r). Perl also does the "mathematical" division: rpolzer@katsuragi rpolzer $ perl -e 'print -1%10, "\n"' 9 But: rpolzer@katsuragi rpolzer $ perl -Minteger -e 'print -1%10, "\n"' -1 as defined by Intel on x86 systems (other architectures may behave differently). Without 'use integer', there's no such thing as integer division in Perl, so I assume you were using it. BTW: Ruby seems to define division by negative divisors like this: , r == 0: (-q, r) div(a, -b) = < ` r != 0: (-q-1, r-b) or like above if you define [0, b[ being equivalent to ]b, 0] for negative b. -- >>>+++++[<<++<+++++++>>>-]+[+[>>+<+<-]>[<+>-]>-[[>>>>>+<<<<<<+>-]<[>+<-]<[>>>+<< +<-]>[<+>-]>>>-[+>>>[>>+<+<-]>[<+>-]>[<<<<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<+<[>-<[ >>+<<-]]>>[<<+>>-]]<<->>>>>>-]<<<<<<<<+>>>-]+<<<->>>>>>[<<<<+>>>>-]<<<-<<-<[-]+> >[<<->>[-]]<<[->[<<<+>>>-]<]>]<+<<[>>-<<[-]]>>[-<[>+<<<<.>>>-]>[<+>-]<<<.>>>]<],