This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In Perl_sv_gets(), shortbuffered is always 0 when rslen is 0.
[perl5.git] / lib / bigint.pl
index a274736..cf915f7 100644 (file)
@@ -1,4 +1,16 @@
+warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
+
 package bigint;
+#
+# This library is no longer being maintained, and is included for backward
+# compatibility with Perl 4 programs which may require it.
+#
+# In particular, this should not be used as an example of modern Perl
+# programming techniques.
+# This legacy library is deprecated and will be removed in a future
+# release of perl.
+#
+# Suggested alternative:  Math::BigInt
 
 # arbitrary size integer math package
 #
@@ -12,7 +24,7 @@ package bigint;
 #   '+0'                            canonical zero value
 #   '   -123 123 123'               canonical value '-123123123'
 #   '1 23 456 7890'                 canonical value '+1234567890'
-# Output values always always in canonical form
+# Output values always in canonical form
 #
 # Actual math is done in an internal format consisting of an array
 #   whose first element is the sign (/^[+-]$/) and whose remaining 
@@ -34,6 +46,12 @@ package bigint;
 #   bnorm(BINT) return BINT             normalization
 #
 
+# overcome a floating point problem on certain osnames (posix-bc, os390)
+BEGIN {
+    my $x = 100000.0;
+    my $use_mult = int($x*1e-5)*1e5 == $x ? 1 : 0;
+}
+
 $zero = 0;
 
 \f
@@ -45,7 +63,7 @@ sub main'bnorm { #(num_str) return num_str
     local($_) = @_;
     s/\s+//g;                           # strip white space
     if (s/^([+-]?)0*(\d+)$/$1$2/) {     # test if number
-       substr($_,$[,0) = '+' unless $1; # Add missing sign
+       substr($_,0,0) = '+' unless $1; # Add missing sign
        s/^-0/+0/;
        $_;
     } else {
@@ -57,8 +75,8 @@ sub main'bnorm { #(num_str) return num_str
 #   Assumes normalized value as input.
 sub internal { #(num_str) return int_num_array
     local($d) = @_;
-    ($is,$il) = (substr($d,$[,1),length($d)-2);
-    substr($d,$[,1) = '';
+    ($is,$il) = (substr($d,0,1),length($d)-2);
+    substr($d,0,1) = '';
     ($is, reverse(unpack("a" . ($il%5+1) . ("a5" x ($il/5)), $d)));
 }
 
@@ -74,7 +92,7 @@ sub external { #(int_num_array) return num_str
 sub main'bneg { #(num_str) return num_str
     local($_) = &'bnorm(@_);
     vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0';
-    s/^H/N/;
+    s/^./N/ unless /^[-+]/; # works both in ASCII and EBCDIC
     $_;
 }
 
@@ -91,7 +109,7 @@ sub abs { # post-normalized abs for internal use
 \f
 # Compares 2 values.  Returns one of undef, <0, =0, >0. (suitable for sort)
 sub main'bcmp { #(num_str, num_str) return cond_code
-    local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
+    local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1]));
     if ($x eq 'NaN') {
        undef;
     } elsif ($y eq 'NaN') {
@@ -123,7 +141,7 @@ sub cmp { # post-normalized compare for internal use
 }
 
 sub main'badd { #(num_str, num_str) return num_str
-    local(*x, *y); ($x, $y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
+    local(*x, *y); ($x, $y) = (&'bnorm($_[0]),&'bnorm($_[1]));
     if ($x eq 'NaN') {
        'NaN';
     } elsif ($y eq 'NaN') {
@@ -146,12 +164,12 @@ sub main'badd { #(num_str, num_str) return num_str
 }
 
 sub main'bsub { #(num_str, num_str) return num_str
-    &'badd($_[$[],&'bneg($_[$[+1]));    
+    &'badd($_[0],&'bneg($_[1]));    
 }
 
 # GCD -- Euclids algorithm Knuth Vol 2 pg 296
 sub main'bgcd { #(num_str, num_str) return num_str
-    local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
+    local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1]));
     if ($x eq 'NaN' || $y eq 'NaN') {
        'NaN';
     } else {
@@ -168,11 +186,11 @@ sub add { #(int_num_array, int_num_array) return int_num_array
     $car = 0;
     for $x (@x) {
        last unless @y || $car;
-       $x -= 1e5 if $car = (($x += shift(@y) + $car) >= 1e5);
+       $x -= 1e5 if $car = (($x += shift(@y) + $car) >= 1e5) ? 1 : 0;
     }
     for $y (@y) {
        last unless $car;
-       $y -= 1e5 if $car = (($y += $car) >= 1e5);
+       $y -= 1e5 if $car = (($y += $car) >= 1e5) ? 1 : 0;
     }
     (@x, @y, $car);
 }
@@ -190,7 +208,7 @@ sub sub { #(int_num_array, int_num_array) return int_num_array
 
 # multiply two numbers -- stolen from Knuth Vol 2 pg 233
 sub main'bmul { #(num_str, num_str) return num_str
-    local(*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
+    local(*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1]));
     if ($x eq 'NaN') {
        'NaN';
     } elsif ($y eq 'NaN') {
@@ -201,11 +219,17 @@ sub main'bmul { #(num_str, num_str) return num_str
        local($signr) = (shift @x ne shift @y) ? '-' : '+';
        @prod = ();
        for $x (@x) {
-           ($car, $cty) = (0, $[);
+           ($car, $cty) = (0, 0);
            for $y (@y) {
                $prod = $x * $y + $prod[$cty] + $car;
-               $prod[$cty++] =
-                   $prod - ($car = int($prod * 1e-5)) * 1e5;
+                if ($use_mult) {
+                   $prod[$cty++] =
+                       $prod - ($car = int($prod * 1e-5)) * 1e5;
+                }
+                else {
+                   $prod[$cty++] =
+                       $prod - ($car = int($prod / 1e5)) * 1e5;
+                }
            }
            $prod[$cty] += $car if $car;
            $x = shift @prod;
@@ -216,27 +240,37 @@ sub main'bmul { #(num_str, num_str) return num_str
 
 # modulus
 sub main'bmod { #(num_str, num_str) return num_str
-    (&'bdiv(@_))[$[+1];
+    (&'bdiv(@_))[1];
 }
 \f
 sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
-    local (*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
+    local (*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1]));
     return wantarray ? ('NaN','NaN') : 'NaN'
        if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0');
     return wantarray ? ('+0',$x) : '+0' if (&cmp(&abs($x),&abs($y)) < 0);
     @x = &internal($x); @y = &internal($y);
-    $srem = $y[$[];
+    $srem = $y[0];
     $sr = (shift @x ne shift @y) ? '-' : '+';
     $car = $bar = $prd = 0;
     if (($dd = int(1e5/($y[$#y]+1))) != 1) {
        for $x (@x) {
            $x = $x * $dd + $car;
+            if ($use_mult) {
            $x -= ($car = int($x * 1e-5)) * 1e5;
+            }
+            else {
+           $x -= ($car = int($x / 1e5)) * 1e5;
+            }
        }
        push(@x, $car); $car = 0;
        for $y (@y) {
            $y = $y * $dd + $car;
+            if ($use_mult) {
            $y -= ($car = int($y * 1e-5)) * 1e5;
+            }
+            else {
+           $y -= ($car = int($y / 1e5)) * 1e5;
+            }
        }
     }
     else {
@@ -249,14 +283,19 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
        --$q while ($v2*$q > ($u0*1e5+$u1-$q*$v1)*1e5+$u2);
        if ($q) {
            ($car, $bar) = (0,0);
-           for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
+           for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) {
                $prd = $q * $y[$y] + $car;
+                if ($use_mult) {
                $prd -= ($car = int($prd * 1e-5)) * 1e5;
+                }
+                else {
+               $prd -= ($car = int($prd / 1e5)) * 1e5;
+                }
                $x[$x] += 1e5 if ($bar = (($x[$x] -= $prd + $bar) < 0));
            }
            if ($x[$#x] < $car + $bar) {
                $car = 0; --$q;
-               for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
+               for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) {
                    $x[$x] -= 1e5
                        if ($car = (($x[$x] += $y[$y] + $car) > 1e5));
                }