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