X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fbigint.pl;h=4044f7f6348379e67a01adafaca46578697c18b8;hb=ad1ebafb1ecaed58230ed26d371e08c109ae3ac4;hp=45ffe1d4026da2e05b088b5fa79cb6533d95fbe3;hpb=ed6116ce9b9d13712ea252ee248b0400653db7f9;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/bigint.pl b/lib/bigint.pl index 45ffe1d..4044f7f 100644 --- a/lib/bigint.pl +++ b/lib/bigint.pl @@ -1,5 +1,13 @@ 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. +# +# Suggested alternative: Math::BigInt +# # arbitrary size integer math package # # by Mark Biggar @@ -33,10 +41,14 @@ package bigint; # bgcd(BINT,BINT) return BINT greatest common divisor # bnorm(BINT) return BINT normalization # + +$zero = 0; + # 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 +82,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 +111,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 +176,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); } @@ -263,7 +285,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); }