This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate from mainperl.
[perl5.git] / lib / bigint.pl
CommitLineData
5303340c
LW
1package bigint;
2
3# arbitrary size integer math package
4#
5# by Mark Biggar
6#
7# Canonical Big integer value are strings of the form
8# /^[+-]\d+$/ with leading zeros suppressed
9# Input values to these routines may be strings of the form
10# /^\s*[+-]?[\d\s]+$/.
11# Examples:
12# '+0' canonical zero value
13# ' -123 123 123' canonical value '-123123123'
14# '1 23 456 7890' canonical value '+1234567890'
15# Output values always always in canonical form
16#
17# Actual math is done in an internal format consisting of an array
18# whose first element is the sign (/^[+-]$/) and whose remaining
19# elements are base 100000 digits with the least significant digit first.
20# The string 'NaN' is used to represent the result when input arguments
21# are not numbers, as well as the result of dividing by zero
22#
23# routines provided are:
24#
25# bneg(BINT) return BINT negation
26# babs(BINT) return BINT absolute value
27# bcmp(BINT,BINT) return CODE compare numbers (undef,<0,=0,>0)
28# badd(BINT,BINT) return BINT addition
29# bsub(BINT,BINT) return BINT subtraction
30# bmul(BINT,BINT) return BINT multiplication
31# bdiv(BINT,BINT) return (BINT,BINT) division (quo,rem) just quo if scalar
32# bmod(BINT,BINT) return BINT modulus
33# bgcd(BINT,BINT) return BINT greatest common divisor
34# bnorm(BINT) return BINT normalization
35#
8990e307
LW
36
37$zero = 0;
38
5303340c
LW
39\f
40# normalize string form of number. Strip leading zeros. Strip any
41# white space and add a sign, if missing.
42# Strings that are not numbers result the value 'NaN'.
8990e307 43
5303340c
LW
44sub main'bnorm { #(num_str) return num_str
45 local($_) = @_;
46 s/\s+//g; # strip white space
47 if (s/^([+-]?)0*(\d+)$/$1$2/) { # test if number
79072805 48 substr($_,$[,0) = '+' unless $1; # Add missing sign
5303340c
LW
49 s/^-0/+0/;
50 $_;
51 } else {
52 'NaN';
53 }
54}
55
56# Convert a number from string format to internal base 100000 format.
57# Assumes normalized value as input.
58sub internal { #(num_str) return int_num_array
59 local($d) = @_;
79072805
LW
60 ($is,$il) = (substr($d,$[,1),length($d)-2);
61 substr($d,$[,1) = '';
5303340c
LW
62 ($is, reverse(unpack("a" . ($il%5+1) . ("a5" x ($il/5)), $d)));
63}
64
65# Convert a number from internal base 100000 format to string format.
66# This routine scribbles all over input array.
67sub external { #(int_num_array) return num_str
68 $es = shift;
69 grep($_ > 9999 || ($_ = substr('0000'.$_,-5)), @_); # zero pad
70 &'bnorm(join('', $es, reverse(@_))); # reverse concat and normalize
71}
72
73# Negate input value.
74sub main'bneg { #(num_str) return num_str
75 local($_) = &'bnorm(@_);
76 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0';
9d116dd7 77 s/^./N/ unless /^[-+]/; # works both in ASCII and EBCDIC
5303340c
LW
78 $_;
79}
80
81# Returns the absolute value of the input.
82sub main'babs { #(num_str) return num_str
83 &abs(&'bnorm(@_));
84}
85
86sub abs { # post-normalized abs for internal use
87 local($_) = @_;
88 s/^-/+/;
89 $_;
90}
91\f
92# Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
93sub main'bcmp { #(num_str, num_str) return cond_code
79072805 94 local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
5303340c
LW
95 if ($x eq 'NaN') {
96 undef;
97 } elsif ($y eq 'NaN') {
98 undef;
99 } else {
100 &cmp($x,$y);
101 }
102}
103
104sub cmp { # post-normalized compare for internal use
105 local($cx, $cy) = @_;
1e2e1ae8
JH
106 return 0 if ($cx eq $cy);
107
108 local($sx, $sy) = (substr($cx, 0, 1), substr($cy, 0, 1));
109 local($ld);
110
111 if ($sx eq '+') {
112 return 1 if ($sy eq '-' || $cy eq '+0');
113 $ld = length($cx) - length($cy);
114 return $ld if ($ld);
115 return $cx cmp $cy;
116 } else { # $sx eq '-'
117 return -1 if ($sy eq '+');
118 $ld = length($cy) - length($cx);
119 return $ld if ($ld);
120 return $cy cmp $cx;
121 }
122
5303340c
LW
123}
124
125sub main'badd { #(num_str, num_str) return num_str
79072805 126 local(*x, *y); ($x, $y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
5303340c
LW
127 if ($x eq 'NaN') {
128 'NaN';
129 } elsif ($y eq 'NaN') {
130 'NaN';
131 } else {
132 @x = &internal($x); # convert to internal form
133 @y = &internal($y);
134 local($sx, $sy) = (shift @x, shift @y); # get signs
135 if ($sx eq $sy) {
136 &external($sx, &add(*x, *y)); # if same sign add
137 } else {
138 ($x, $y) = (&abs($x),&abs($y)); # make abs
139 if (&cmp($y,$x) > 0) {
140 &external($sy, &sub(*y, *x));
141 } else {
142 &external($sx, &sub(*x, *y));
143 }
144 }
145 }
146}
147
148sub main'bsub { #(num_str, num_str) return num_str
79072805 149 &'badd($_[$[],&'bneg($_[$[+1]));
5303340c
LW
150}
151
152# GCD -- Euclids algorithm Knuth Vol 2 pg 296
153sub main'bgcd { #(num_str, num_str) return num_str
79072805 154 local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
68decaef 155 if ($x eq 'NaN' || $y eq 'NaN') {
5303340c 156 'NaN';
68decaef 157 } else {
5303340c
LW
158 ($x, $y) = ($y,&'bmod($x,$y)) while $y ne '+0';
159 $x;
160 }
161}
162\f
68decaef 163# routine to add two base 1e5 numbers
5303340c
LW
164# stolen from Knuth Vol 2 Algorithm A pg 231
165# there are separate routines to add and sub as per Kunth pg 233
166sub add { #(int_num_array, int_num_array) return int_num_array
167 local(*x, *y) = @_;
168 $car = 0;
169 for $x (@x) {
170 last unless @y || $car;
55497cff 171 $x -= 1e5 if $car = (($x += shift(@y) + $car) >= 1e5) ? 1 : 0;
5303340c
LW
172 }
173 for $y (@y) {
174 last unless $car;
55497cff 175 $y -= 1e5 if $car = (($y += $car) >= 1e5) ? 1 : 0;
5303340c
LW
176 }
177 (@x, @y, $car);
178}
179
68decaef 180# subtract base 1e5 numbers -- stolen from Knuth Vol 2 pg 232, $x > $y
5303340c
LW
181sub sub { #(int_num_array, int_num_array) return int_num_array
182 local(*sx, *sy) = @_;
183 $bar = 0;
184 for $sx (@sx) {
185 last unless @y || $bar;
e334a159 186 $sx += 1e5 if $bar = (($sx -= shift(@sy) + $bar) < 0);
5303340c
LW
187 }
188 @sx;
189}
190
191# multiply two numbers -- stolen from Knuth Vol 2 pg 233
192sub main'bmul { #(num_str, num_str) return num_str
79072805 193 local(*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
5303340c
LW
194 if ($x eq 'NaN') {
195 'NaN';
196 } elsif ($y eq 'NaN') {
197 'NaN';
198 } else {
199 @x = &internal($x);
200 @y = &internal($y);
201 local($signr) = (shift @x ne shift @y) ? '-' : '+';
202 @prod = ();
203 for $x (@x) {
79072805 204 ($car, $cty) = (0, $[);
5303340c
LW
205 for $y (@y) {
206 $prod = $x * $y + $prod[$cty] + $car;
207 $prod[$cty++] =
68decaef 208 $prod - ($car = int($prod * 1e-5)) * 1e5;
5303340c
LW
209 }
210 $prod[$cty] += $car if $car;
211 $x = shift @prod;
212 }
213 &external($signr, @x, @prod);
214 }
215}
216
217# modulus
218sub main'bmod { #(num_str, num_str) return num_str
79072805 219 (&'bdiv(@_))[$[+1];
5303340c
LW
220}
221\f
222sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
79072805 223 local (*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
5303340c
LW
224 return wantarray ? ('NaN','NaN') : 'NaN'
225 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0');
226 return wantarray ? ('+0',$x) : '+0' if (&cmp(&abs($x),&abs($y)) < 0);
227 @x = &internal($x); @y = &internal($y);
79072805 228 $srem = $y[$[];
5303340c
LW
229 $sr = (shift @x ne shift @y) ? '-' : '+';
230 $car = $bar = $prd = 0;
68decaef 231 if (($dd = int(1e5/($y[$#y]+1))) != 1) {
5303340c
LW
232 for $x (@x) {
233 $x = $x * $dd + $car;
68decaef 234 $x -= ($car = int($x * 1e-5)) * 1e5;
5303340c
LW
235 }
236 push(@x, $car); $car = 0;
237 for $y (@y) {
238 $y = $y * $dd + $car;
68decaef 239 $y -= ($car = int($y * 1e-5)) * 1e5;
5303340c
LW
240 }
241 }
242 else {
243 push(@x, 0);
244 }
ed6116ce 245 @q = (); ($v2,$v1) = @y[-2,-1];
5303340c 246 while ($#x > $#y) {
ed6116ce 247 ($u2,$u1,$u0) = @x[-3..-1];
68decaef
LW
248 $q = (($u0 == $v1) ? 99999 : int(($u0*1e5+$u1)/$v1));
249 --$q while ($v2*$q > ($u0*1e5+$u1-$q*$v1)*1e5+$u2);
5303340c
LW
250 if ($q) {
251 ($car, $bar) = (0,0);
79072805 252 for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
5303340c 253 $prd = $q * $y[$y] + $car;
68decaef
LW
254 $prd -= ($car = int($prd * 1e-5)) * 1e5;
255 $x[$x] += 1e5 if ($bar = (($x[$x] -= $prd + $bar) < 0));
5303340c
LW
256 }
257 if ($x[$#x] < $car + $bar) {
258 $car = 0; --$q;
79072805 259 for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
68decaef
LW
260 $x[$x] -= 1e5
261 if ($car = (($x[$x] += $y[$y] + $car) > 1e5));
5303340c
LW
262 }
263 }
264 }
265 pop(@x); unshift(@q, $q);
266 }
267 if (wantarray) {
268 @d = ();
269 if ($dd != 1) {
270 $car = 0;
271 for $x (reverse @x) {
68decaef 272 $prd = $car * 1e5 + $x;
5303340c
LW
273 $car = $prd - ($tmp = int($prd / $dd)) * $dd;
274 unshift(@d, $tmp);
275 }
276 }
277 else {
278 @d = @x;
279 }
8990e307 280 (&external($sr, @q), &external($srem, @d, $zero));
5303340c
LW
281 } else {
282 &external($sr, @q);
283 }
284}
2851;