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

[ID 20010723.001] perl 5.6.1: BigInt booleans cannot handle negatives

Thread Next
From:
Bryce Denney
Date:
July 23, 2001 09:48
Subject:
[ID 20010723.001] perl 5.6.1: BigInt booleans cannot handle negatives
Message ID:
Pine.LNX.4.21.0107231232290.8711-100000@dante.tlw.com
And, Or, and Xor on two positive BigInts seem to work ok.  However, if you
throw in a negative operand you get some very strange behavior.  I'm
assuming that you would like integer math and bigint math to be compatible
here, but even without that assumption BigInt is still buggy.  Here is a
testing script which runs on perl 5.6.1.  (It uses booleans on a BigInt so
only recent perls will run it at all.)

use Math::BigInt;
@list = (-65536, -65535, -1, 0, 1, 65535, 65536);
foreach $x (@list) {
  foreach $y (@list) {
    $a = $x & $y;
    $b = (Math::BigInt->new($x)) & (Math::BigInt->new($y));
    if ($a != $b) { 
      print "MISMATCH:\n";
      print "$x AND $y = $a   (integer math)\n";
      print "$x AND $y = $b   (BigInt math)\n";
    }
  }
}

The problem seems to be that the BigInt boolean functions do not sign
extend negative numbers when they are breaking them into chunks with bdiv.  
This job would be easy if the numbers were stored in a base that is a
power of 2 instead of a power of 10.  (Then you could do left and right
shift without dividing as well...)

Thank you,
Bryce Denney



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