develooper Front page | perl.perl5.porters | Postings from March 2003

RE: [perl #21770] modulus function not working for negative numbers

Thread Previous | Thread Next
From:
bplatz
Date:
March 31, 2003 13:47
Subject:
RE: [perl #21770] modulus function not working for negative numbers
Message ID:
FFC8F8B126A0C147AF6485CADC099C370506F76F@pai820excuag03.agere.com
Just a follow up. Although Perl is returning the mathematical concept of a modulus value, it is not returning a integer arithmetic value of a remainder, which is what C returns. The Perl book by O'Reilly & Associates, Inc. states that Perl's % operator will return a remainder according to integer division, which clearly it is not. Using my example of -5 % 3, C produced a value of -2,
such that ((a / b) * b) + (a % b) = a (e.g., ((-5 / 3) * 3) + (-5 % 3) = -5, or -3 + -2 = -5), but Perl produced a value of 1 which does not act as a valid remainder according to integer division (e.g., ((-5 / 3) * 3) + (-5 % 3) = does not equal -5, or -3 + 1 equals -2, not -5).

Lesson learned, avoid negative operands.

Thanks,
Dave Schulte
bplatz@acm.org



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About