develooper Front page | perl.perl5.porters | Postings from July 2005

[perl #5070] Inconsistent arithmetic with % and * operators

From:
Michael G Schwern via RT
Date:
July 23, 2005 01:51
Subject:
[perl #5070] Inconsistent arithmetic with % and * operators
Message ID:
rt-3.0.11-5070-118190.13.2124102249657@perl.org
> [dickinsm@math.lsa.umich.edu - Thu Jan 04 10:10:18 2001]:
> 
> # The following complete four-line perl script produces unexpected
> # results on my machine.  The multiplication in the second line of
> # code below produces a negative result where a positive number is
> # expected, presumably because an integer multiplication was used
> # internally where a floating point operation was needed.  If the
> # expression is broken up and evaluated piece by piece then the
> # correct result is produced (line 4). (The difference between the two
> # results is, naturally, 2**32.)  The same results were produced under
> # perl v5.6.0.
> 
> my ($p, $x, $y) = (65537, 64645, 34463);
> print (($x % $p) * ($y % $p), "\n");     # produces -2067106661
> (incorrect)
> my ($a, $b) = ($x % $p, $y % $p);
> print $a * $b, "\n";                     # produces  2227860635
> (correct)

Confirmed broken in 5.6.2.

Confirmed fixed in 5.8.0.


0 windhund ~/tmp$ perl5.6.2 test
-2067106661
2227860635
0 windhund ~/tmp$ perl5.5.4 test
-2067106661
2227860635
0 windhund ~/tmp$ perl5.8.6 test
2227860635
2227860635
0 windhund ~/tmp$ perl5.8.0 test
2227860635
2227860635
0 windhund ~/tmp$ perl5.8.1 test
2227860635
2227860635




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