From:

Date:

October 24, 2001 09:28Subject:

Re: Bug with modulo operator and negative operandsMessage ID:

E15wQt2-00029t-00@virgo.cus.cam.ac.ukJohn Peacock <jpeacock@rowman.com> 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 GuyThread Previous | Thread Next

- Bug with modulo operator and negative operands by John Peacock
**Re: Bug with modulo operator and negative operands**by Mike Guy- Re: Bug with modulo operator and negative operands by John Peacock
- Re: Bug with modulo operator and negative operands by Nicholas Clark
- Re: Bug with modulo operator and negative operands by Nick Ing-Simmons
- Re: Bug with modulo operator and negative operands by Jarkko Hietaniemi

nntp.perl.org: Perl Programming lists via nntp and http.

Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About