This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move ExtUtils::Manifest from lib to ext.
[perl5.git] / lib / bigint.pl
index 45ffe1d..a31bfec 100644 (file)
@@ -1,4 +1,18 @@
 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
+
+warn( "The 'bigint.pl' legacy library is deprecated and will be"
+      . " removed in the next major release of perl. Please use the"
+      . " Math::BigInt module instead." );
 
 # arbitrary size integer math package
 #
@@ -12,7 +26,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 
@@ -33,10 +47,20 @@ package bigint;
 #   bgcd(BINT,BINT) return BINT         greatest common divisor
 #   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
 # normalize string form of number.   Strip leading zeros.  Strip any
 #   white space and add a sign, if missing.
 # Strings that are not numbers result the value 'NaN'.
+
 sub main'bnorm { #(num_str) return num_str
     local($_) = @_;
     s/\s+//g;                           # strip white space
@@ -70,7 +94,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
     $_;
 }
 
@@ -99,13 +123,23 @@ sub main'bcmp { #(num_str, num_str) return cond_code
 
 sub cmp { # post-normalized compare for internal use
     local($cx, $cy) = @_;
-    $cx cmp $cy
-    &&
-    (
-       ord($cy) <=> ord($cx)
-       ||
-       ($cx cmp ',') * (length($cy) <=> length($cx) || $cy cmp $cx)
-    );
+    return 0 if ($cx eq $cy);
+
+    local($sx, $sy) = (substr($cx, 0, 1), substr($cy, 0, 1));
+    local($ld);
+
+    if ($sx eq '+') {
+      return  1 if ($sy eq '-' || $cy eq '+0');
+      $ld = length($cx) - length($cy);
+      return $ld if ($ld);
+      return $cx cmp $cy;
+    } else { # $sx eq '-'
+      return -1 if ($sy eq '+');
+      $ld = length($cy) - length($cx);
+      return $ld if ($ld);
+      return $cy cmp $cx;
+    }
+
 }
 
 sub main'badd { #(num_str, num_str) return num_str
@@ -154,11 +188,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,8 +224,14 @@ sub main'bmul { #(num_str, num_str) return num_str
            ($car, $cty) = (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;
@@ -217,12 +257,22 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
     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 {
@@ -237,7 +287,12 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
            ($car, $bar) = (0,0);
            for ($y = $[, $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) {
@@ -263,7 +318,7 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
        else {
            @d = @x;
        }
-       (&external($sr, @q), &external($srem, @d, 0));
+       (&external($sr, @q), &external($srem, @d, $zero));
     } else {
        &external($sr, @q);
     }