1 package Math::BigInt::Calc;
5 # use warnings; # dont use warnings for older Perls
8 use vars qw/@ISA $VERSION/;
13 # Package to store unsigned big integers in decimal and do math with them
15 # Internally the numbers are stored in an array with at least 1 element, no
16 # leading zero parts (except the first) and in base 1eX where X is determined
17 # automatically at loading time to be the maximum possible value
20 # - fully remove funky $# stuff (maybe)
22 # USE_MUL: due to problems on certain os (os390, posix-bc) "* 1e-5" is used
23 # instead of "/ 1e5" at some places, (marked with USE_MUL). Other platforms
24 # BS2000, some Crays need USE_DIV instead.
25 # The BEGIN block is used to determine which of the two variants gives the
28 ##############################################################################
29 # global constants, flags and accessory
31 # constants for easier life
33 my ($MBASE,$BASE,$RBASE,$BASE_LEN,$MAX_VAL,$BASE_LEN2,$BASE_LEN_SMALL);
34 my ($AND_BITS,$XOR_BITS,$OR_BITS);
35 my ($AND_MASK,$XOR_MASK,$OR_MASK);
40 # set/get the BASE_LEN and assorted other, connected values
41 # used only be the testsuite, set is used only by the BEGIN block below
47 # find whether we can use mul or div or none in mul()/div()
48 # (in last case reduce BASE_LEN_SMALL)
49 $BASE_LEN_SMALL = $b+1;
51 while (--$BASE_LEN_SMALL > 5)
53 $MBASE = int("1e".$BASE_LEN_SMALL);
54 $RBASE = abs('1e-'.$BASE_LEN_SMALL); # see USE_MUL
56 $caught += 1 if (int($MBASE * $RBASE) != 1); # should be 1
57 $caught += 2 if (int($MBASE / $MBASE) != 1); # should be 1
60 # BASE_LEN is used for anything else than mul()/div()
61 $BASE_LEN = $BASE_LEN_SMALL;
62 $BASE_LEN = shift if (defined $_[0]); # one more arg?
63 $BASE = int("1e".$BASE_LEN);
65 $BASE_LEN2 = int($BASE_LEN_SMALL / 2); # for mul shortcut
66 $MBASE = int("1e".$BASE_LEN_SMALL);
67 $RBASE = abs('1e-'.$BASE_LEN_SMALL); # see USE_MUL
70 $LEN_CONVERT = 1 if $BASE_LEN_SMALL != $BASE_LEN;
72 #print "BASE_LEN: $BASE_LEN MAX_VAL: $MAX_VAL BASE: $BASE RBASE: $RBASE ";
73 #print "BASE_LEN_SMALL: $BASE_LEN_SMALL MBASE: $MBASE\n";
81 *{_mul} = \&_mul_use_mul;
82 *{_div} = \&_div_use_mul;
84 else # $caught must be 2, since it can't be 1 nor 3
87 *{_mul} = \&_mul_use_div;
88 *{_div} = \&_div_use_div;
91 return $BASE_LEN unless wantarray;
92 return ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL);
97 # from Daniel Pfeiffer: determine largest group of digits that is precisely
98 # multipliable with itself plus carry
99 # Test now changed to expect the proper pattern, not a result off by 1 or 2
100 my ($e, $num) = 3; # lowest value we will use is 3+1-1 = 3
103 $num = ('9' x ++$e) + 0;
105 } while ("$num" =~ /9{$e}0{$e}/); # must be a certain pattern
106 $e--; # last test failed, so retract one step
107 # the limits below brush the problems with the test above under the rug:
108 # the test should be able to find the proper $e automatically
109 $e = 5 if $^O =~ /^uts/; # UTS get's some special treatment
110 $e = 5 if $^O =~ /^unicos/; # unicos is also problematic (6 seems to work
111 # there, but we play safe)
112 $e = 5 if $] < 5.006; # cap, for older Perls
113 $e = 7 if $e > 7; # cap, for VMS, OS/390 and other 64 bit systems
114 # 8 fails inside random testsuite, so take 7
116 # determine how many digits fit into an integer and can be safely added
117 # together plus carry w/o causing an overflow
119 # this below detects 15 on a 64 bit system, because after that it becomes
120 # 1e16 and not 1000000 :/ I can make it detect 18, but then I get a lot of
121 # test failures. Ugh! (Tomake detect 18: uncomment lines marked with *)
123 my $bi = 5; # approx. 16 bit
124 $num = int('9' x $bi);
126 # while ( ($num+$num+1) eq '1' . '9' x $bi) # *
127 while ( int($num+$num+1) eq '1' . '9' x $bi)
129 $bi++; $num = int('9' x $bi);
130 # $bi++; $num *= 10; $num += 9; # *
132 $bi--; # back off one step
133 # by setting them equal, we ignore the findings and use the default
134 # one-size-fits-all approach from former versions
135 $bi = $e; # XXX, this should work always
137 __PACKAGE__->_base_len($e,$bi); # set and store
139 # find out how many bits _and, _or and _xor can take (old default = 16)
140 # I don't think anybody has yet 128 bit scalars, so let's play safe.
141 local $^W = 0; # don't warn about 'nonportable number'
142 $AND_BITS = 15; $XOR_BITS = 15; $OR_BITS = 15;
144 # find max bits, we will not go higher than numberofbits that fit into $BASE
145 # to make _and etc simpler (and faster for smaller, slower for large numbers)
147 while (2 ** $max < $BASE) { $max++; }
150 $max = 16 if $] < 5.006; # older Perls might not take >16 too well
155 $x = oct('0b' . '1' x $AND_BITS); $y = $x & $x;
156 $z = (2 ** $AND_BITS) - 1;
157 } while ($AND_BITS < $max && $x == $z && $y == $x);
158 $AND_BITS --; # retreat one step
161 $x = oct('0b' . '1' x $XOR_BITS); $y = $x ^ 0;
162 $z = (2 ** $XOR_BITS) - 1;
163 } while ($XOR_BITS < $max && $x == $z && $y == $x);
164 $XOR_BITS --; # retreat one step
167 $x = oct('0b' . '1' x $OR_BITS); $y = $x | $x;
168 $z = (2 ** $OR_BITS) - 1;
169 } while ($OR_BITS < $max && $x == $z && $y == $x);
170 $OR_BITS --; # retreat one step
174 ##############################################################################
175 # convert between the "small" and the "large" representation
179 # take an array in base $BASE_LEN_SMALL and convert it in-place to $BASE_LEN
182 # print "_to_large $BASE_LEN_SMALL => $BASE_LEN\n";
184 return $x if $LEN_CONVERT == 0 || # nothing to converconvertor
185 @$x == 1; # only one element => early out
187 # 12345 67890 12345 67890 contents
189 # 123456 7890123 4567890 contents
192 # my @d; my $str = '';
193 # my $z = '0' x $BASE_LEN_SMALL;
196 # # ... . 04321 . 000321
197 # $str = substr($z.$_,-$BASE_LEN_SMALL,$BASE_LEN_SMALL) . $str;
198 # if (length($str) > $BASE_LEN)
200 # push @d, substr($str,-$BASE_LEN,$BASE_LEN); # extract one piece
201 # substr($str,-$BASE_LEN,$BASE_LEN) = ''; # remove it
204 # push @d, $str if $str !~ /^0*$/; # extract last piece
206 # $x->[-1] = int($x->[-1]); # strip leading zero
210 my $l = scalar @$x; # number of parts
211 $l --; $ret .= int($x->[$l]); $l--;
212 my $z = '0' x ($BASE_LEN_SMALL-1);
215 $ret .= substr($z.$x->[$l],-$BASE_LEN_SMALL);
218 my $str = _new($c,\$ret); # make array
219 @$x = @$str; # clobber contents of $x
220 $x->[-1] = int($x->[-1]); # strip leading zero
225 # take an array in base $BASE_LEN and convert it in-place to $BASE_LEN_SMALL
228 return $x if $LEN_CONVERT == 0; # nothing to do
229 return $x if @$x == 1 && length(int($x->[0])) <= $BASE_LEN_SMALL;
232 my $il = length($$d)-1;
233 ## this leaves '00000' instead of int 0 and will be corrected after any op
234 # clobber contents of $x
235 @$x = reverse(unpack("a" . ($il % $BASE_LEN_SMALL+1)
236 . ("a$BASE_LEN_SMALL" x ($il / $BASE_LEN_SMALL)), $$d));
238 $x->[-1] = int($x->[-1]); # strip leading zero
241 ###############################################################################
245 # (ref to string) return ref to num_array
246 # Convert a number from string format (without sign) to internal base
247 # 1ex format. Assumes normalized value as input.
249 my $il = length($$d)-1;
250 # this leaves '00000' instead of int 0 and will be corrected after any op
251 [ reverse(unpack("a" . ($il % $BASE_LEN+1)
252 . ("a$BASE_LEN" x ($il / $BASE_LEN)), $$d)) ];
257 $AND_MASK = __PACKAGE__->_new( \( 2 ** $AND_BITS ));
258 $XOR_MASK = __PACKAGE__->_new( \( 2 ** $XOR_BITS ));
259 $OR_MASK = __PACKAGE__->_new( \( 2 ** $OR_BITS ));
276 # create a two (used internally for shifting)
285 # catch and throw away
288 ##############################################################################
289 # convert back to string and number
293 # (ref to BINT) return num_str
294 # Convert number from internal base 100000 format to string format.
295 # internal format is always normalized (no leading zeros, "-0" => "+0")
299 my $l = scalar @$ar; # number of parts
300 return $nan if $l < 1; # should not happen
302 # handle first one different to strip leading zeros from it (there are no
303 # leading zero parts in internal representation)
304 $l --; $ret .= int($ar->[$l]); $l--;
305 # Interestingly, the pre-padd method uses more time
306 # the old grep variant takes longer (14 to 10 sec)
307 my $z = '0' x ($BASE_LEN-1);
310 $ret .= substr($z.$ar->[$l],-$BASE_LEN); # fastest way I could think of
318 # Make a number (scalar int/float) from a BigInt object
320 return $x->[0] if scalar @$x == 1; # below $BASE
325 $num += $fac*$_; $fac *= $BASE;
330 ##############################################################################
335 # (ref to int_num_array, ref to int_num_array)
336 # routine to add two base 1eX numbers
337 # stolen from Knuth Vol 2 Algorithm A pg 231
338 # there are separate routines to add and sub as per Knuth pg 233
339 # This routine clobbers up array x, but not y.
343 return $x if (@$y == 1) && $y->[0] == 0; # $x + 0 => $x
344 if ((@$x == 1) && $x->[0] == 0) # 0 + $y => $y->copy
346 # twice as slow as $x = [ @$y ], but necc. to retain $x as ref :(
347 @$x = @$y; return $x;
350 # for each in Y, add Y to X and carry. If after that, something is left in
351 # X, foreach in X add carry to X and then return X, carry
352 # Trades one "$j++" for having to shift arrays, $j could be made integer
353 # but this would impose a limit to number-length of 2**32.
354 my $i; my $car = 0; my $j = 0;
357 $x->[$j] -= $BASE if $car = (($x->[$j] += $i + $car) >= $BASE) ? 1 : 0;
362 $x->[$j] -= $BASE if $car = (($x->[$j] += $car) >= $BASE) ? 1 : 0; $j++;
369 # (ref to int_num_array, ref to int_num_array)
370 # routine to add 1 to a base 1eX numbers
371 # This routine clobbers up array x, but not y.
376 return $x if (($i += 1) < $BASE); # early out
377 $i = 0; # overflow, next
379 push @$x,1 if ($x->[-1] == 0); # last overflowed, so extend
385 # (ref to int_num_array, ref to int_num_array)
386 # routine to add 1 to a base 1eX numbers
387 # This routine clobbers up array x, but not y.
390 my $MAX = $BASE-1; # since MAX_VAL based on MBASE
393 last if (($i -= 1) >= 0); # early out
394 $i = $MAX; # overflow, next
396 pop @$x if $x->[-1] == 0 && @$x > 1; # last overflowed (but leave 0)
402 # (ref to int_num_array, ref to int_num_array, swap)
403 # subtract base 1eX numbers -- stolen from Knuth Vol 2 pg 232, $x > $y
404 # subtract Y from X by modifying x in place
405 my ($c,$sx,$sy,$s) = @_;
407 my $car = 0; my $i; my $j = 0;
413 last unless defined $sy->[$j] || $car;
414 $i += $BASE if $car = (($i -= ($sy->[$j] || 0) + $car) < 0); $j++;
416 # might leave leading zeros, so fix that
417 return __strip_zeros($sx);
419 #print "case 1 (swap)\n";
422 # we can't do an early out if $x is < than $y, since we
423 # need to copy the high chunks from $y. Found by Bob Mathews.
424 #last unless defined $sy->[$j] || $car;
426 if $car = (($sy->[$j] = $i-($sy->[$j]||0) - $car) < 0);
429 # might leave leading zeros, so fix that
435 # compute $x ** 2 or $x * $x in-place and return $x
438 # From: Handbook of Applied Cryptography by A. Menezes, P. van Oorschot and
439 # S. Vanstone., Chapter 14
441 #14.16 Algorithm Multiple-precision squaring
442 #INPUT: positive integer x = (xt 1 xt 2 ... x1 x0)b.
443 #OUTPUT: x * x = x ** 2 in radix b representation.
444 #1. For i from 0 to (2t - 1) do: wi <- 0.
445 #2. For i from 0 to (t - 1) do the following:
446 # 2.1 (uv)b w2i + xi * xi, w2i v, c u.
447 # 2.2 For j from (i + 1)to (t - 1) do the following:
448 # (uv)b <- wi+j + 2*xj * xi + c, wi+j <- v, c <- u.
450 #3. Return((w2t-1 w2t-2 ... w1 w0)b).
452 # # Note: That description is crap. Half of the symbols are not explained or
453 # # used with out beeing set.
454 # my $t = scalar @$x; # count
456 # for ($i = 0; $i < $t; $i++)
458 # $x->[$i] = $x->[$i*2] + $x[$i]*$x[$i];
459 # $x->[$i*2] = $x[$i]; $c = $x[$i];
460 # for ($j = $i+1; $j < $t; $j++)
462 # $x->[$i] = $x->[$i+$j] + 2 * $x->[$i] * $x->[$j];
463 # $x->[$i+$j] = $x[$j]; $c = $x[$i];
465 # $x->[$i+$t] = $x[$i];
472 # (ref to int_num_array, ref to int_num_array)
473 # multiply two numbers in internal representation
474 # modifies first arg, second need not be different from first
475 my ($c,$xv,$yv) = @_;
477 # shortcut for two very short numbers (improved by Nathan Zook)
478 # works also if xv and yv are the same reference
479 if ((@$xv == 1) && (@$yv == 1))
481 if (($xv->[0] *= $yv->[0]) >= $MBASE)
483 $xv->[0] = $xv->[0] - ($xv->[1] = int($xv->[0] * $RBASE)) * $MBASE;
487 # shortcut for result == 0
488 if ( ((@$xv == 1) && ($xv->[0] == 0)) ||
489 ((@$yv == 1) && ($yv->[0] == 0)) )
495 # since multiplying $x with $x fails, make copy in this case
496 $yv = [@$xv] if $xv == $yv; # same references?
497 # $yv = [@$xv] if "$xv" eq "$yv"; # same references?
499 # since multiplying $x with $x would fail here, use the faster squaring
500 # return _square($c,$xv) if $xv == $yv; # same reference?
502 if ($LEN_CONVERT != 0)
504 $c->_to_small($xv); $c->_to_small($yv);
507 my @prod = (); my ($prod,$car,$cty,$xi,$yi);
516 # $prod = $xi * $yi + ($prod[$cty] || 0) + $car;
518 # $prod - ($car = int($prod * RBASE)) * $MBASE; # see USE_MUL
520 # $prod[$cty] += $car if $car; # need really to check for 0?
524 # looping through this if $xi == 0 is silly - so optimize it away!
525 $xi = (shift @prod || 0), next if $xi == 0;
528 $prod = $xi * $yi + ($prod[$cty] || 0) + $car;
529 ## this is actually a tad slower
530 ## $prod = $prod[$cty]; $prod += ($car + $xi * $yi); # no ||0 here
532 $prod - ($car = int($prod * $RBASE)) * $MBASE; # see USE_MUL
534 $prod[$cty] += $car if $car; # need really to check for 0?
535 $xi = shift @prod || 0; # || 0 makes v5.005_3 happy
538 if ($LEN_CONVERT != 0)
552 # (ref to int_num_array, ref to int_num_array)
553 # multiply two numbers in internal representation
554 # modifies first arg, second need not be different from first
555 my ($c,$xv,$yv) = @_;
557 # shortcut for two very short numbers (improved by Nathan Zook)
558 # works also if xv and yv are the same reference
559 if ((@$xv == 1) && (@$yv == 1))
561 if (($xv->[0] *= $yv->[0]) >= $MBASE)
564 $xv->[0] - ($xv->[1] = int($xv->[0] / $MBASE)) * $MBASE;
568 # shortcut for result == 0
569 if ( ((@$xv == 1) && ($xv->[0] == 0)) ||
570 ((@$yv == 1) && ($yv->[0] == 0)) )
577 # since multiplying $x with $x fails, make copy in this case
578 $yv = [@$xv] if $xv == $yv; # same references?
579 # $yv = [@$xv] if "$xv" eq "$yv"; # same references?
580 # since multiplying $x with $x would fail here, use the faster squaring
581 # return _square($c,$xv) if $xv == $yv; # same reference?
583 if ($LEN_CONVERT != 0)
585 $c->_to_small($xv); $c->_to_small($yv);
588 my @prod = (); my ($prod,$car,$cty,$xi,$yi);
592 # looping through this if $xi == 0 is silly - so optimize it away!
593 $xi = (shift @prod || 0), next if $xi == 0;
596 $prod = $xi * $yi + ($prod[$cty] || 0) + $car;
598 $prod - ($car = int($prod / $MBASE)) * $MBASE;
600 $prod[$cty] += $car if $car; # need really to check for 0?
601 $xi = shift @prod || 0; # || 0 makes v5.005_3 happy
604 if ($LEN_CONVERT != 0)
618 # ref to array, ref to array, modify first array and return remainder if
620 my ($c,$x,$yorg) = @_;
622 if (@$x == 1 && @$yorg == 1)
624 # shortcut, $yorg and $x are two small numbers
627 my $r = [ $x->[0] % $yorg->[0] ];
628 $x->[0] = int($x->[0] / $yorg->[0]);
633 $x->[0] = int($x->[0] / $yorg->[0]);
640 $rem = _mod($c,[ @$x ],$yorg) if wantarray;
642 # shortcut, $y is < $BASE
643 my $j = scalar @$x; my $r = 0;
644 my $y = $yorg->[0]; my $b;
647 $b = $r * $MBASE + $x->[$j];
648 $x->[$j] = int($b/$y);
651 pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero
652 return ($x,$rem) if wantarray;
656 my $y = [ @$yorg ]; # always make copy to preserve
657 if ($LEN_CONVERT != 0)
659 $c->_to_small($x); $c->_to_small($y);
662 my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0);
664 $car = $bar = $prd = 0;
665 if (($dd = int($MBASE/($y->[-1]+1))) != 1)
669 $xi = $xi * $dd + $car;
670 $xi -= ($car = int($xi * $RBASE)) * $MBASE; # see USE_MUL
672 push(@$x, $car); $car = 0;
675 $yi = $yi * $dd + $car;
676 $yi -= ($car = int($yi * $RBASE)) * $MBASE; # see USE_MUL
683 @q = (); ($v2,$v1) = @$y[-2,-1];
687 ($u2,$u1,$u0) = @$x[-3..-1];
689 #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n"
691 $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$MBASE+$u1)/$v1));
692 --$q while ($v2*$q > ($u0*$MBASE+$u1-$q*$v1)*$MBASE+$u2);
695 ($car, $bar) = (0,0);
696 for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi)
698 $prd = $q * $y->[$yi] + $car;
699 $prd -= ($car = int($prd * $RBASE)) * $MBASE; # see USE_MUL
700 $x->[$xi] += $MBASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0));
702 if ($x->[-1] < $car + $bar)
705 for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi)
708 if ($car = (($x->[$xi] += $y->[$yi] + $car) > $MBASE));
712 pop(@$x); unshift(@q, $q);
720 for $xi (reverse @$x)
722 $prd = $car * $MBASE + $xi;
723 $car = $prd - ($tmp = int($prd / $dd)) * $dd; # see USE_MUL
733 if ($LEN_CONVERT != 0)
735 $c->_to_large($x); $c->_to_large($d);
745 if ($LEN_CONVERT != 0)
758 # ref to array, ref to array, modify first array and return remainder if
760 my ($c,$x,$yorg) = @_;
762 if (@$x == 1 && @$yorg == 1)
764 # shortcut, $yorg and $x are two small numbers
767 my $r = [ $x->[0] % $yorg->[0] ];
768 $x->[0] = int($x->[0] / $yorg->[0]);
773 $x->[0] = int($x->[0] / $yorg->[0]);
780 $rem = _mod($c,[ @$x ],$yorg) if wantarray;
782 # shortcut, $y is < $BASE
783 my $j = scalar @$x; my $r = 0;
784 my $y = $yorg->[0]; my $b;
787 $b = $r * $MBASE + $x->[$j];
788 $x->[$j] = int($b/$y);
791 pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero
792 return ($x,$rem) if wantarray;
796 my $y = [ @$yorg ]; # always make copy to preserve
797 if ($LEN_CONVERT != 0)
799 $c->_to_small($x); $c->_to_small($y);
802 my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0);
804 $car = $bar = $prd = 0;
805 if (($dd = int($MBASE/($y->[-1]+1))) != 1)
809 $xi = $xi * $dd + $car;
810 $xi -= ($car = int($xi / $MBASE)) * $MBASE;
812 push(@$x, $car); $car = 0;
815 $yi = $yi * $dd + $car;
816 $yi -= ($car = int($yi / $MBASE)) * $MBASE;
823 @q = (); ($v2,$v1) = @$y[-2,-1];
827 ($u2,$u1,$u0) = @$x[-3..-1];
829 #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n"
831 $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$MBASE+$u1)/$v1));
832 --$q while ($v2*$q > ($u0*$MBASE+$u1-$q*$v1)*$MBASE+$u2);
835 ($car, $bar) = (0,0);
836 for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi)
838 $prd = $q * $y->[$yi] + $car;
839 $prd -= ($car = int($prd / $MBASE)) * $MBASE;
840 $x->[$xi] += $MBASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0));
842 if ($x->[-1] < $car + $bar)
845 for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi)
848 if ($car = (($x->[$xi] += $y->[$yi] + $car) > $MBASE));
852 pop(@$x); unshift(@q, $q);
860 for $xi (reverse @$x)
862 $prd = $car * $MBASE + $xi;
863 $car = $prd - ($tmp = int($prd / $dd)) * $dd;
873 if ($LEN_CONVERT != 0)
875 $c->_to_large($x); $c->_to_large($d);
885 if ($LEN_CONVERT != 0)
896 ##############################################################################
901 # internal absolute post-normalized compare (ignore signs)
902 # ref to array, ref to array, return <0, 0, >0
903 # arrays must have at least one entry; this is not checked for
905 my ($c,$cx,$cy) = @_;
907 # fast comp based on number of array elements (aka pseudo-length)
908 my $lxy = scalar @$cx - scalar @$cy;
909 return -1 if $lxy < 0; # already differs, ret
910 return 1 if $lxy > 0; # ditto
912 # now calculate length based on digits, not parts
913 $lxy = _len($c,$cx) - _len($c,$cy); # difference
914 return -1 if $lxy < 0;
915 return 1 if $lxy > 0;
917 # hm, same lengths, but same contents?
919 # first way takes 5.49 sec instead of 4.87, but has the early out advantage
920 # so grep is slightly faster, but more inflexible. hm. $_ instead of $k
921 # yields 5.6 instead of 5.5 sec huh?
922 # manual way (abort if unequal, good for early ne)
923 my $j = scalar @$cx - 1;
926 last if ($a = $cx->[$j] - $cy->[$j]); $j--;
928 # my $j = scalar @$cx;
931 # last if ($a = $cx->[$j] - $cy->[$j]);
937 # while it early aborts, it is even slower than the manual variant
938 #grep { return $a if ($a = $_ - $cy->[$i++]); } @$cx;
939 # grep way, go trough all (bad for early ne)
940 #grep { $a = $_ - $cy->[$i++]; } @$cx;
946 # compute number of digits in bigint, minus the sign
948 # int() because add/sub sometimes leaves strings (like '00005') instead of
949 # '5' in this place, thus causing length() to report wrong length
952 return (@$cx-1)*$BASE_LEN+length(int($cx->[-1]));
957 # return the nth digit, negative values count backward
958 # zero is rightmost, so _digit(123,0) will give 3
961 my $len = _len('',$x);
963 $n = $len+$n if $n < 0; # -1 last, -2 second-to-last
964 $n = abs($n); # if negative was too big
965 $len--; $n = $len if $n > $len; # n to big?
967 my $elem = int($n / $BASE_LEN); # which array element
968 my $digit = $n % $BASE_LEN; # which digit in this element
969 $elem = '0000'.@$x[$elem]; # get element padded with 0's
970 return substr($elem,-$digit-1,1);
975 # return amount of trailing zeros in decimal
976 # check each array elem in _m for having 0 at end as long as elem == 0
977 # Upon finding a elem != 0, stop
979 my $zeros = 0; my $elem;
984 $elem = "$e"; # preserve x
985 $elem =~ s/.*?(0*$)/$1/; # strip anything not zero
986 $zeros *= $BASE_LEN; # elems * 5
987 $zeros += length($elem); # count trailing zeros
990 $zeros ++; # real else branch: 50% slower!
995 ##############################################################################
1000 # return true if arg (BINT or num_str) is zero (array '+', '0')
1003 (((scalar @$x == 1) && ($x->[0] == 0))) <=> 0;
1008 # return true if arg (BINT or num_str) is even
1010 (!($x->[0] & 1)) <=> 0;
1015 # return true if arg (BINT or num_str) is even
1018 (($x->[0] & 1)) <=> 0;
1023 # return true if arg (BINT or num_str) is one (array '+', '1')
1026 (scalar @$x == 1) && ($x->[0] == 1) <=> 0;
1031 # internal normalization function that strips leading zeros from the array
1032 # args: ref to array
1035 my $cnt = scalar @$s; # get count of parts
1037 push @$s,0 if $i < 0; # div might return empty results, so fix it
1039 return $s if @$s == 1; # early out
1041 #print "strip: cnt $cnt i $i\n";
1042 # '0', '3', '4', '0', '0',
1047 # => fcnt = cnt - i (5-2 => 3, cnt => 5-1 = 4, throw away from 4th pos)
1048 # >= 1: skip first part (this can be zero)
1049 while ($i > 0) { last if $s->[$i] != 0; $i--; }
1050 $i++; splice @$s,$i if ($i < $cnt); # $i cant be 0
1054 ###############################################################################
1055 # check routine to test internal state of corruptions
1059 # used by the test suite
1062 return "$x is not a reference" if !ref($x);
1064 # are all parts are valid?
1065 my $i = 0; my $j = scalar @$x; my ($e,$try);
1068 $e = $x->[$i]; $e = 'undef' unless defined $e;
1069 $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e)";
1070 last if $e !~ /^[+]?[0-9]+$/;
1071 $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (stringify)";
1072 last if "$e" !~ /^[+]?[0-9]+$/;
1073 $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (cat-stringify)";
1074 last if '' . "$e" !~ /^[+]?[0-9]+$/;
1075 $try = ' < 0 || >= $BASE; '."($x, $e)";
1076 last if $e <0 || $e >= $BASE;
1077 # this test is disabled, since new/bnorm and certain ops (like early out
1078 # in add/sub) are allowed/expected to leave '00000' in some elements
1079 #$try = '=~ /^00+/; '."($x, $e)";
1080 #last if $e =~ /^00+/;
1083 return "Illegal part '$e' at pos $i (tested: $try)" if $i < $j;
1088 ###############################################################################
1089 ###############################################################################
1090 # some optional routines to make BigInt faster
1094 # if possible, use mod shortcut
1095 my ($c,$x,$yo) = @_;
1097 # slow way since $y to big
1098 if (scalar @$yo > 1)
1100 my ($xo,$rem) = _div($c,$x,$yo);
1104 # both are single element arrays
1105 if (scalar @$x == 1)
1111 # @y is single element, but @x has more than one
1115 # when BASE % Y == 0 then (B * BASE) % Y == 0
1116 # (B * BASE) % $y + A % Y => A % Y
1117 # so need to consider only last element: O(1)
1122 # else need to go trough all elements: O(N), but loop is a bit simplified
1126 $r = ($r + $_) % $y; # not much faster, but heh...
1127 #$r += $_ % $y; $r %= $y;
1134 # else need to go trough all elements: O(N)
1135 my $r = 0; my $bm = 1;
1138 $r = ($_ * $bm + $r) % $y;
1139 $bm = ($bm * $b) % $y;
1141 #$r += ($_ % $y) * $bm;
1153 ##############################################################################
1158 my ($c,$x,$y,$n) = @_;
1162 $n = _new($c,\$n); return _div($c,$x, _pow($c,$n,$y));
1165 # shortcut (faster) for shifting by 10)
1166 # multiples of $BASE_LEN
1167 my $dst = 0; # destination
1168 my $src = _num($c,$y); # as normal int
1169 my $rem = $src % $BASE_LEN; # remainder to shift
1170 $src = int($src / $BASE_LEN); # source
1173 splice (@$x,0,$src); # even faster, 38.4 => 39.3
1177 my $len = scalar @$x - $src; # elems to go
1178 my $vd; my $z = '0'x $BASE_LEN;
1179 $x->[scalar @$x] = 0; # avoid || 0 test inside loop
1182 $vd = $z.$x->[$src];
1183 $vd = substr($vd,-$BASE_LEN,$BASE_LEN-$rem);
1185 $vd = substr($z.$x->[$src],-$rem,$rem) . $vd;
1186 $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN;
1187 $x->[$dst] = int($vd);
1190 splice (@$x,$dst) if $dst > 0; # kill left-over array elems
1191 pop @$x if $x->[-1] == 0 && @$x > 1; # kill last element if 0
1198 my ($c,$x,$y,$n) = @_;
1202 $n = _new($c,\$n); return _mul($c,$x, _pow($c,$n,$y));
1205 # shortcut (faster) for shifting by 10) since we are in base 10eX
1206 # multiples of $BASE_LEN:
1207 my $src = scalar @$x; # source
1208 my $len = _num($c,$y); # shift-len as normal int
1209 my $rem = $len % $BASE_LEN; # remainder to shift
1210 my $dst = $src + int($len/$BASE_LEN); # destination
1211 my $vd; # further speedup
1212 $x->[$src] = 0; # avoid first ||0 for speed
1213 my $z = '0' x $BASE_LEN;
1216 $vd = $x->[$src]; $vd = $z.$vd;
1217 $vd = substr($vd,-$BASE_LEN+$rem,$BASE_LEN-$rem);
1218 $vd .= $src > 0 ? substr($z.$x->[$src-1],-$BASE_LEN,$rem) : '0' x $rem;
1219 $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN;
1220 $x->[$dst] = int($vd);
1223 # set lowest parts to 0
1224 while ($dst >= 0) { $x->[$dst--] = 0; }
1225 # fix spurios last zero element
1226 splice @$x,-1 if $x->[-1] == 0;
1233 # ref to array, ref to array, return ref to array
1234 my ($c,$cx,$cy) = @_;
1238 my $y_bin = ${_as_bin($c,$cy)}; $y_bin =~ s/^0b//;
1239 my $len = length($y_bin);
1242 _mul($c,$pow2,$cx) if substr($y_bin,$len,1) eq '1'; # is odd?
1253 # ref to array, return ref to array
1256 if ((@$cx == 1) && ($cx->[0] <= 2))
1258 $cx->[0] = 1 * ($cx->[0]||1); # 0,1 => 1, 2 => 2
1262 # go forward until $base is exceeded
1263 # limit is either $x or $base (x == 100 means as result too high)
1264 my $steps = 100; $steps = $cx->[0] if @$cx == 1;
1265 my $r = 2; my $cf = 3; my $step = 1; my $last = $r;
1266 while ($r < $BASE && $step < $steps)
1268 $last = $r; $r *= $cf++; $step++;
1270 if ((@$cx == 1) && ($step == $cx->[0]))
1276 my $n = _copy($c,$cx);
1280 while (!(@$n == 1 && $n->[0] == $step))
1282 _mul($c,$cx,$n); _dec($c,$n);
1287 use constant DEBUG => 0;
1291 sub steps { $steps };
1296 # ref to array, return ref to array
1299 if (scalar @$x == 1)
1301 # fit's into one Perl scalar
1302 $x->[0] = int(sqrt($x->[0]));
1305 my $y = _copy($c,$x);
1306 # hopefully _len/2 is < $BASE, the -1 is to always undershot the guess
1307 # since our guess will "grow"
1308 my $l = int((_len($c,$x)-1) / 2);
1310 my $lastelem = $x->[-1]; # for guess
1311 my $elems = scalar @$x - 1;
1312 # not enough digits, but could have more?
1313 if ((length($lastelem) <= 3) && ($elems > 1))
1315 # right-align with zero pad
1316 my $len = length($lastelem) & 1;
1317 print "$lastelem => " if DEBUG;
1318 $lastelem .= substr($x->[-2] . '0' x $BASE_LEN,0,$BASE_LEN);
1319 # former odd => make odd again, or former even to even again
1320 $lastelem = $lastelem / 10 if (length($lastelem) & 1) != $len;
1321 print "$lastelem\n" if DEBUG;
1324 # construct $x (instead of _lsft($c,$x,$l,10)
1325 my $r = $l % $BASE_LEN; # 10000 00000 00000 00000 ($BASE_LEN=5)
1326 $l = int($l / $BASE_LEN);
1327 print "l = $l " if DEBUG;
1329 splice @$x,$l; # keep ref($x), but modify it
1331 # we make the first part of the guess not '1000...0' but int(sqrt($lastelem))
1333 # 14400 00000 => sqrt(14400) => 120
1334 # 144000 000000 => sqrt(144000) => 379
1336 # $x->[$l--] = int('1' . '0' x $r); # old way of guessing
1337 print "$lastelem (elems $elems) => " if DEBUG;
1338 $lastelem = $lastelem / 10 if ($elems & 1 == 1); # odd or even?
1339 my $g = sqrt($lastelem); $g =~ s/\.//; # 2.345 => 2345
1340 $r -= 1 if $elems & 1 == 0; # 70 => 7
1342 # padd with zeros if result is too short
1343 $x->[$l--] = int(substr($g . '0' x $r,0,$r+1));
1344 print "now ",$x->[-1] if DEBUG;
1345 print " would have been ", int('1' . '0' x $r),"\n" if DEBUG;
1347 # If @$x > 1, we could compute the second elem of the guess, too, to create
1348 # an even better guess. Not implemented yet.
1349 $x->[$l--] = 0 while ($l >= 0); # all other digits of guess are zero
1351 print "start x= ",${_str($c,$x)},"\n" if DEBUG;
1354 my $lastlast = _zero();
1355 $steps = 0 if DEBUG;
1356 while (_acmp($c,$last,$x) != 0 && _acmp($c,$lastlast,$x) != 0)
1359 $lastlast = _copy($c,$last);
1360 $last = _copy($c,$x);
1361 _add($c,$x, _div($c,_copy($c,$y),$x));
1363 print " x= ",${_str($c,$x)},"\n" if DEBUG;
1365 print "\nsteps in sqrt: $steps, " if DEBUG;
1366 _dec($c,$x) if _acmp($c,$y,_mul($c,_copy($c,$x),$x)) < 0; # overshot?
1367 print " final ",$x->[-1],"\n" if DEBUG;
1371 ##############################################################################
1378 # the shortcut makes equal, large numbers _really_ fast, and makes only a
1379 # very small performance drop for small numbers (e.g. something with less
1380 # than 32 bit) Since we optimize for large numbers, this is enabled.
1381 return $x if _acmp($c,$x,$y) == 0; # shortcut
1383 my $m = _one(); my ($xr,$yr);
1384 my $mask = $AND_MASK;
1387 my $y1 = _copy($c,$y); # make copy
1391 while (!_is_zero($c,$x1) && !_is_zero($c,$y1))
1393 ($x1, $xr) = _div($c,$x1,$mask);
1394 ($y1, $yr) = _div($c,$y1,$mask);
1396 # make ints() from $xr, $yr
1397 # this is when the AND_BITS are greater tahn $BASE and is slower for
1398 # small (<256 bits) numbers, but faster for large numbers. Disabled
1399 # due to KISS principle
1401 # $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; }
1402 # $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; }
1403 # _add($c,$x, _mul($c, _new( $c, \($xrr & $yrr) ), $m) );
1405 # 0+ due to '&' doesn't work in strings
1406 _add($c,$x, _mul($c, [ 0+$xr->[0] & 0+$yr->[0] ], $m) );
1416 return _zero() if _acmp($c,$x,$y) == 0; # shortcut (see -and)
1418 my $m = _one(); my ($xr,$yr);
1419 my $mask = $XOR_MASK;
1422 my $y1 = _copy($c,$y); # make copy
1426 while (!_is_zero($c,$x1) && !_is_zero($c,$y1))
1428 ($x1, $xr) = _div($c,$x1,$mask);
1429 ($y1, $yr) = _div($c,$y1,$mask);
1430 # make ints() from $xr, $yr (see _and())
1431 #$b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; }
1432 #$b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; }
1433 #_add($c,$x, _mul($c, _new( $c, \($xrr ^ $yrr) ), $m) );
1435 # 0+ due to '^' doesn't work in strings
1436 _add($c,$x, _mul($c, [ 0+$xr->[0] ^ 0+$yr->[0] ], $m) );
1439 # the loop stops when the shorter of the two numbers is exhausted
1440 # the remainder of the longer one will survive bit-by-bit, so we simple
1441 # multiply-add it in
1442 _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1);
1443 _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1);
1452 return $x if _acmp($c,$x,$y) == 0; # shortcut (see _and)
1454 my $m = _one(); my ($xr,$yr);
1455 my $mask = $OR_MASK;
1458 my $y1 = _copy($c,$y); # make copy
1462 while (!_is_zero($c,$x1) && !_is_zero($c,$y1))
1464 ($x1, $xr) = _div($c,$x1,$mask);
1465 ($y1, $yr) = _div($c,$y1,$mask);
1466 # make ints() from $xr, $yr (see _and())
1467 # $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; }
1468 # $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; }
1469 # _add($c,$x, _mul($c, _new( $c, \($xrr | $yrr) ), $m) );
1471 # 0+ due to '|' doesn't work in strings
1472 _add($c,$x, _mul($c, [ 0+$xr->[0] | 0+$yr->[0] ], $m) );
1475 # the loop stops when the shorter of the two numbers is exhausted
1476 # the remainder of the longer one will survive bit-by-bit, so we simple
1477 # multiply-add it in
1478 _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1);
1479 _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1);
1486 # convert a decimal number to hex (ref to array, return ref to string)
1489 my $x1 = _copy($c,$x);
1492 my ($xr, $h, $x10000);
1495 $x10000 = [ 0x10000 ]; $h = 'h4';
1499 $x10000 = [ 0x1000 ]; $h = 'h3';
1501 while (! _is_zero($c,$x1))
1503 ($x1, $xr) = _div($c,$x1,$x10000);
1504 $es .= unpack($h,pack('v',$xr->[0]));
1507 $es =~ s/^[0]+//; # strip leading zeros
1514 # convert a decimal number to bin (ref to array, return ref to string)
1517 my $x1 = _copy($c,$x);
1520 my ($xr, $b, $x10000);
1523 $x10000 = [ 0x10000 ]; $b = 'b16';
1527 $x10000 = [ 0x1000 ]; $b = 'b12';
1529 while (! _is_zero($c,$x1))
1531 ($x1, $xr) = _div($c,$x1,$x10000);
1532 $es .= unpack($b,pack('v',$xr->[0]));
1535 $es =~ s/^[0]+//; # strip leading zeros
1542 # convert a hex number to decimal (ref to string, return ref to array)
1546 my $m = [ 0x10000 ]; # 16 bit at a time
1549 my $len = length($$hs)-2;
1550 $len = int($len/4); # 4-digit parts, w/o '0x'
1551 my $val; my $i = -4;
1554 $val = substr($$hs,$i,4);
1555 $val =~ s/^[+-]?0x// if $len == 0; # for last part only because
1556 $val = hex($val); # hex does not like wrong chars
1558 _add ($c, $x, _mul ($c, [ $val ], $mul ) ) if $val != 0;
1559 _mul ($c, $mul, $m ) if $len >= 0; # skip last mul
1566 # convert a hex number to decimal (ref to string, return ref to array)
1569 # instead of converting 8 bit at a time, it is faster to convert the
1570 # number to hex, and then call _from_hex.
1573 $hs =~ s/^[+-]?0b//; # remove sign and 0b
1574 my $l = length($hs); # bits
1575 $hs = '0' x (8-($l % 8)) . $hs if ($l % 8) != 0; # padd left side w/ 0
1576 my $h = unpack('H*', pack ('B*', $hs)); # repack as hex
1577 return $c->_from_hex(\('0x'.$h));
1580 my $m = [ 0x100 ]; # 8 bit at a time
1583 my $len = length($$bs)-2;
1584 $len = int($len/8); # 4-digit parts, w/o '0x'
1585 my $val; my $i = -8;
1588 $val = substr($$bs,$i,8);
1589 $val =~ s/^[+-]?0b// if $len == 0; # for last part only
1591 $val = ord(pack('B8',substr('00000000'.$val,-8,8)));
1594 _add ($c, $x, _mul ($c, [ $val ], $mul ) ) if $val != 0;
1595 _mul ($c, $mul, $m ) if $len >= 0; # skip last mul
1600 ##############################################################################
1601 # special modulus functions
1603 # not ready yet, since it would need to deal with unsigned numbers
1607 my ($c,$num,$mod) = @_;
1609 my $u = _zero(); my $u1 = _one();
1610 my $a = _copy($c,$mod); my $b = _copy($c,$num);
1612 # Euclid's Algorithm for bgcd(), only that we calc bgcd() ($a) and the
1613 # result ($u) at the same time
1614 while (!_is_zero($c,$b))
1616 # print ${_str($c,$a)}, " ", ${_str($c,$b)}, " ", ${_str($c,$u)}, " ",
1617 # ${_str($c,$u1)}, "\n";
1618 ($a, my $q, $b) = ($b, _div($c,$a,$b));
1619 # print ${_str($c,$a)}, " ", ${_str($c,$q)}, " ", ${_str($c,$b)}, "\n";
1620 # original: ($u,$u1) = ($u1, $u - $u1 * $q);
1621 my $t = _copy($c,$u);
1625 # print ${_str($c,$a)}, " ", ${_str($c,$b)}, " ", ${_str($c,$u)}, " ",
1626 # ${_str($c,$u1)}, "\n";
1629 # if the gcd is not 1, then return NaN
1630 return undef unless _is_one($c,$a);
1632 $num = _mod($c,$u,$mod);
1633 # print ${_str($c,$num)},"\n";
1639 # modulus of power ($x ** $y) % $z
1640 my ($c,$num,$exp,$mod) = @_;
1642 # in the trivial case,
1643 if (_is_one($c,$mod))
1645 splice @$num,0,1; $num->[0] = 0;
1648 if ((scalar @$num == 1) && (($num->[0] == 0) || ($num->[0] == 1)))
1654 # $num = _mod($c,$num,$mod); # this does not make it faster
1656 my $acc = _copy($c,$num); my $t = _one();
1658 my $expbin = ${_as_bin($c,$exp)}; $expbin =~ s/^0b//;
1659 my $len = length($expbin);
1662 if ( substr($expbin,$len,1) eq '1') # is_odd
1665 $t = _mod($c,$t,$mod);
1668 $acc = _mod($c,$acc,$mod);
1674 ##############################################################################
1675 ##############################################################################
1682 Math::BigInt::Calc - Pure Perl module to support Math::BigInt
1686 Provides support for big integer calculations. Not intended to be used by other
1687 modules (except Math::BigInt::Cached). Other modules which sport the same
1688 functions can also be used to support Math::Bigint, like Math::BigInt::Pari.
1692 In order to allow for multiple big integer libraries, Math::BigInt was
1693 rewritten to use library modules for core math routines. Any module which
1694 follows the same API as this can be used instead by using the following:
1696 use Math::BigInt lib => 'libname';
1698 'libname' is either the long name ('Math::BigInt::Pari'), or only the short
1699 version like 'Pari'.
1703 The following functions MUST be defined in order to support the use by
1706 _new(string) return ref to new object from ref to decimal string
1707 _zero() return a new object with value 0
1708 _one() return a new object with value 1
1710 _str(obj) return ref to a string representing the object
1711 _num(obj) returns a Perl integer/floating point number
1712 NOTE: because of Perl numeric notation defaults,
1713 the _num'ified obj may lose accuracy due to
1714 machine-dependend floating point size limitations
1716 _add(obj,obj) Simple addition of two objects
1717 _mul(obj,obj) Multiplication of two objects
1718 _div(obj,obj) Division of the 1st object by the 2nd
1719 In list context, returns (result,remainder).
1720 NOTE: this is integer math, so no
1721 fractional part will be returned.
1722 _sub(obj,obj) Simple subtraction of 1 object from another
1723 a third, optional parameter indicates that the params
1724 are swapped. In this case, the first param needs to
1725 be preserved, while you can destroy the second.
1726 sub (x,y,1) => return x - y and keep x intact!
1727 _dec(obj) decrement object by one (input is garant. to be > 0)
1728 _inc(obj) increment object by one
1731 _acmp(obj,obj) <=> operator for objects (return -1, 0 or 1)
1733 _len(obj) returns count of the decimal digits of the object
1734 _digit(obj,n) returns the n'th decimal digit of object
1736 _is_one(obj) return true if argument is +1
1737 _is_zero(obj) return true if argument is 0
1738 _is_even(obj) return true if argument is even (0,2,4,6..)
1739 _is_odd(obj) return true if argument is odd (1,3,5,7..)
1741 _copy return a ref to a true copy of the object
1743 _check(obj) check whether internal representation is still intact
1744 return 0 for ok, otherwise error message as string
1746 The following functions are optional, and can be defined if the underlying lib
1747 has a fast way to do them. If undefined, Math::BigInt will use pure Perl (hence
1748 slow) fallback routines to emulate these:
1750 _from_hex(str) return ref to new object from ref to hexadecimal string
1751 _from_bin(str) return ref to new object from ref to binary string
1753 _as_hex(str) return ref to scalar string containing the value as
1754 unsigned hex string, with the '0x' prepended.
1755 Leading zeros must be stripped.
1756 _as_bin(str) Like as_hex, only as binary string containing only
1757 zeros and ones. Leading zeros must be stripped and a
1758 '0b' must be prepended.
1760 _rsft(obj,N,B) shift object in base B by N 'digits' right
1761 For unsupported bases B, return undef to signal failure
1762 _lsft(obj,N,B) shift object in base B by N 'digits' left
1763 For unsupported bases B, return undef to signal failure
1765 _xor(obj1,obj2) XOR (bit-wise) object 1 with object 2
1766 Note: XOR, AND and OR pad with zeros if size mismatches
1767 _and(obj1,obj2) AND (bit-wise) object 1 with object 2
1768 _or(obj1,obj2) OR (bit-wise) object 1 with object 2
1770 _mod(obj,obj) Return remainder of div of the 1st by the 2nd object
1771 _sqrt(obj) return the square root of object (truncate to int)
1772 _fac(obj) return factorial of object 1 (1*2*3*4..)
1773 _pow(obj,obj) return object 1 to the power of object 2
1774 _gcd(obj,obj) return Greatest Common Divisor of two objects
1776 _zeros(obj) return number of trailing decimal zeros
1777 _modinv return inverse modulus
1778 _modpow return modulus of power ($x ** $y) % $z
1780 Input strings come in as unsigned but with prefix (i.e. as '123', '0xabc'
1783 Testing of input parameter validity is done by the caller, so you need not
1784 worry about underflow (f.i. in C<_sub()>, C<_dec()>) nor about division by
1785 zero or similar cases.
1787 The first parameter can be modified, that includes the possibility that you
1788 return a reference to a completely different object instead. Although keeping
1789 the reference and just changing it's contents is prefered over creating and
1790 returning a different reference.
1792 Return values are always references to objects or strings. Exceptions are
1793 C<_lsft()> and C<_rsft()>, which return undef if they can not shift the
1794 argument. This is used to delegate shifting of bases different than the one
1795 you can support back to Math::BigInt, which will use some generic code to
1796 calculate the result.
1798 =head1 WRAP YOUR OWN
1800 If you want to port your own favourite c-lib for big numbers to the
1801 Math::BigInt interface, you can take any of the already existing modules as
1802 a rough guideline. You should really wrap up the latest BigInt and BigFloat
1803 testsuites with your module, and replace in them any of the following:
1809 use Math::BigInt lib => 'yourlib';
1811 This way you ensure that your library really works 100% within Math::BigInt.
1815 This program is free software; you may redistribute it and/or modify it under
1816 the same terms as Perl itself.
1820 Original math code by Mark Biggar, rewritten by Tels L<http://bloodgate.com/>
1822 Seperated from BigInt and shaped API with the help of John Peacock.
1826 L<Math::BigInt>, L<Math::BigFloat>, L<Math::BigInt::BitVect>,
1827 L<Math::BigInt::GMP>, L<Math::BigInt::Cached> and L<Math::BigInt::Pari>.