1 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";
5 # This library is no longer being maintained, and is included for backward
6 # compatibility with Perl 4 programs which may require it.
8 # In particular, this should not be used as an example of modern Perl
9 # programming techniques.
10 # This legacy library is deprecated and will be removed in a future
13 # Suggested alternative: Math::BigInt
15 # arbitrary size integer math package
19 # Canonical Big integer value are strings of the form
20 # /^[+-]\d+$/ with leading zeros suppressed
21 # Input values to these routines may be strings of the form
22 # /^\s*[+-]?[\d\s]+$/.
24 # '+0' canonical zero value
25 # ' -123 123 123' canonical value '-123123123'
26 # '1 23 456 7890' canonical value '+1234567890'
27 # Output values always in canonical form
29 # Actual math is done in an internal format consisting of an array
30 # whose first element is the sign (/^[+-]$/) and whose remaining
31 # elements are base 100000 digits with the least significant digit first.
32 # The string 'NaN' is used to represent the result when input arguments
33 # are not numbers, as well as the result of dividing by zero
35 # routines provided are:
37 # bneg(BINT) return BINT negation
38 # babs(BINT) return BINT absolute value
39 # bcmp(BINT,BINT) return CODE compare numbers (undef,<0,=0,>0)
40 # badd(BINT,BINT) return BINT addition
41 # bsub(BINT,BINT) return BINT subtraction
42 # bmul(BINT,BINT) return BINT multiplication
43 # bdiv(BINT,BINT) return (BINT,BINT) division (quo,rem) just quo if scalar
44 # bmod(BINT,BINT) return BINT modulus
45 # bgcd(BINT,BINT) return BINT greatest common divisor
46 # bnorm(BINT) return BINT normalization
49 # overcome a floating point problem on certain osnames (posix-bc, os390)
52 my $use_mult = int($x*1e-5)*1e5 == $x ? 1 : 0;
58 # normalize string form of number. Strip leading zeros. Strip any
59 # white space and add a sign, if missing.
60 # Strings that are not numbers result the value 'NaN'.
62 sub main'bnorm { #(num_str) return num_str
64 s/\s+//g; # strip white space
65 if (s/^([+-]?)0*(\d+)$/$1$2/) { # test if number
66 substr($_,0,0) = '+' unless $1; # Add missing sign
74 # Convert a number from string format to internal base 100000 format.
75 # Assumes normalized value as input.
76 sub internal { #(num_str) return int_num_array
78 ($is,$il) = (substr($d,0,1),length($d)-2);
80 ($is, reverse(unpack("a" . ($il%5+1) . ("a5" x ($il/5)), $d)));
83 # Convert a number from internal base 100000 format to string format.
84 # This routine scribbles all over input array.
85 sub external { #(int_num_array) return num_str
87 grep($_ > 9999 || ($_ = substr('0000'.$_,-5)), @_); # zero pad
88 &'bnorm(join('', $es, reverse(@_))); # reverse concat and normalize
92 sub main'bneg { #(num_str) return num_str
93 local($_) = &'bnorm(@_);
94 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0';
95 s/^./N/ unless /^[-+]/; # works both in ASCII and EBCDIC
99 # Returns the absolute value of the input.
100 sub main'babs { #(num_str) return num_str
104 sub abs { # post-normalized abs for internal use
110 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
111 sub main'bcmp { #(num_str, num_str) return cond_code
112 local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1]));
115 } elsif ($y eq 'NaN') {
122 sub cmp { # post-normalized compare for internal use
123 local($cx, $cy) = @_;
124 return 0 if ($cx eq $cy);
126 local($sx, $sy) = (substr($cx, 0, 1), substr($cy, 0, 1));
130 return 1 if ($sy eq '-' || $cy eq '+0');
131 $ld = length($cx) - length($cy);
134 } else { # $sx eq '-'
135 return -1 if ($sy eq '+');
136 $ld = length($cy) - length($cx);
143 sub main'badd { #(num_str, num_str) return num_str
144 local(*x, *y); ($x, $y) = (&'bnorm($_[0]),&'bnorm($_[1]));
147 } elsif ($y eq 'NaN') {
150 @x = &internal($x); # convert to internal form
152 local($sx, $sy) = (shift @x, shift @y); # get signs
154 &external($sx, &add(*x, *y)); # if same sign add
156 ($x, $y) = (&abs($x),&abs($y)); # make abs
157 if (&cmp($y,$x) > 0) {
158 &external($sy, &sub(*y, *x));
160 &external($sx, &sub(*x, *y));
166 sub main'bsub { #(num_str, num_str) return num_str
167 &'badd($_[0],&'bneg($_[1]));
170 # GCD -- Euclid's algorithm Knuth Vol 2 pg 296
171 sub main'bgcd { #(num_str, num_str) return num_str
172 local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1]));
173 if ($x eq 'NaN' || $y eq 'NaN') {
176 ($x, $y) = ($y,&'bmod($x,$y)) while $y ne '+0';
181 # routine to add two base 1e5 numbers
182 # stolen from Knuth Vol 2 Algorithm A pg 231
183 # there are separate routines to add and sub as per Kunth pg 233
184 sub add { #(int_num_array, int_num_array) return int_num_array
188 last unless @y || $car;
189 $x -= 1e5 if $car = (($x += shift(@y) + $car) >= 1e5) ? 1 : 0;
193 $y -= 1e5 if $car = (($y += $car) >= 1e5) ? 1 : 0;
198 # subtract base 1e5 numbers -- stolen from Knuth Vol 2 pg 232, $x > $y
199 sub sub { #(int_num_array, int_num_array) return int_num_array
200 local(*sx, *sy) = @_;
203 last unless @y || $bar;
204 $sx += 1e5 if $bar = (($sx -= shift(@sy) + $bar) < 0);
209 # multiply two numbers -- stolen from Knuth Vol 2 pg 233
210 sub main'bmul { #(num_str, num_str) return num_str
211 local(*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1]));
214 } elsif ($y eq 'NaN') {
219 local($signr) = (shift @x ne shift @y) ? '-' : '+';
222 ($car, $cty) = (0, 0);
224 $prod = $x * $y + $prod[$cty] + $car;
227 $prod - ($car = int($prod * 1e-5)) * 1e5;
231 $prod - ($car = int($prod / 1e5)) * 1e5;
234 $prod[$cty] += $car if $car;
237 &external($signr, @x, @prod);
242 sub main'bmod { #(num_str, num_str) return num_str
246 sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
247 local (*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1]));
248 return wantarray ? ('NaN','NaN') : 'NaN'
249 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0');
250 return wantarray ? ('+0',$x) : '+0' if (&cmp(&abs($x),&abs($y)) < 0);
251 @x = &internal($x); @y = &internal($y);
253 $sr = (shift @x ne shift @y) ? '-' : '+';
254 $car = $bar = $prd = 0;
255 if (($dd = int(1e5/($y[$#y]+1))) != 1) {
257 $x = $x * $dd + $car;
259 $x -= ($car = int($x * 1e-5)) * 1e5;
262 $x -= ($car = int($x / 1e5)) * 1e5;
265 push(@x, $car); $car = 0;
267 $y = $y * $dd + $car;
269 $y -= ($car = int($y * 1e-5)) * 1e5;
272 $y -= ($car = int($y / 1e5)) * 1e5;
279 @q = (); ($v2,$v1) = @y[-2,-1];
281 ($u2,$u1,$u0) = @x[-3..-1];
282 $q = (($u0 == $v1) ? 99999 : int(($u0*1e5+$u1)/$v1));
283 --$q while ($v2*$q > ($u0*1e5+$u1-$q*$v1)*1e5+$u2);
285 ($car, $bar) = (0,0);
286 for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) {
287 $prd = $q * $y[$y] + $car;
289 $prd -= ($car = int($prd * 1e-5)) * 1e5;
292 $prd -= ($car = int($prd / 1e5)) * 1e5;
294 $x[$x] += 1e5 if ($bar = (($x[$x] -= $prd + $bar) < 0));
296 if ($x[$#x] < $car + $bar) {
298 for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) {
300 if ($car = (($x[$x] += $y[$y] + $car) > 1e5));
304 pop(@x); unshift(@q, $q);
310 for $x (reverse @x) {
311 $prd = $car * 1e5 + $x;
312 $car = $prd - ($tmp = int($prd / $dd)) * $dd;
319 (&external($sr, @q), &external($srem, @d, $zero));