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

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

Thread Previous
From:
Benjamin Goldberg
Date:
March 31, 2003 14:14
Subject:
Re: [perl #21770] modulus function not working for negative numbers
Message ID:
3E88C11E.9F9DE091@earthlink.net
"Schulte, David (Dave)" wrote:
> 
> 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.

It does use the C modulo operator *if* the 'use integer' pragma is in
effect.

> 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.

Dead trees are not the primary documentation for perl.

The primary documentation for perl is the .pod files which come with
perl, and are accessible through the "perldoc" program.

The primary documentation for perl operators is the "perlop.pod" file,
which you can read by running "perldoc perlop" from the commandline.

[snip]
> Lesson learned, avoid negative operands.

Wrong lesson learned.  The right lesson would be to read the right
documentation -- "perldoc".

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}

Thread Previous


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