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