develooper Front page | perl.perl5.porters | Postings from October 2001

Re: Bug with modulo operator and negative operands

Thread Previous | Thread Next
Mike Guy
October 24, 2001 09:28
Re: Bug with modulo operator and negative operands
Message ID:
John Peacock <> wrote
> With the following trivial code Tels and I have rediscovered a bug
> with the code underlying the modulo operator in Perl as far back
> as 5.005_02 and platforms as varied as Linux, CygWin, and AIX.
> $numerator = -11;
> $denominator = 2;
> $modulo = $numerator % $denominator;
> use integer;
> $numerator = -11;
> $denominator = 2;
> $modulo = $numerator % $denominator;
> which prints out
> -11 % 2 = 1
> -11 % 2 = -1

which is exactly as it's defined to behave.

> Regardless of the validity of the argument that the modulo should
> be -1 in this case, the 'use integer;' should have no effect on
> the answer on any single machine.

What "argument that the modulo should be -1" ?    The mathematically
correct value is 1.    "use integer" is defined to give the same
result as the underlying C, which on most platforms does not give
mathematically correct results.

Note that "use integer" is a bit of a misnomer.   It really means
"Give me speed, even at the expense of correctness".

> Comments, arguments, etc. gladly accepted.  Hearing none, I will
> endeavor to produce a patch.

Since there's no bug, a patch would be most inappropriate.   Unless
different results from the above are obtained in the 64-bit case.
But that would be caught by the regression tests.

Mike Guy

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About