From:

Date:

March 31, 2003 13:47Subject:

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

FFC8F8B126A0C147AF6485CADC099C370506F76F@pai820excuag03.agere.comJust 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.orgThread Previous | Thread Next

- [perl #21770] modulus function not working for negative numbers by perlbug-followup
**RE: [perl #21770] modulus function not working for negative numbers**by bplatz- Re: [perl #21770] modulus function not working for negative numbers by Benjamin Goldberg

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

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