5 # use warnings; # dont use warnings for older Perls
17 $CALC_EMU = Math::BigInt->config()->{'lib'};
22 my ($self,$x,$base,@r) = @_;
24 return $x->bnan() if $x->is_zero() || $base->is_zero() || $base->is_one();
26 my $acmp = $x->bacmp($base);
27 return $x->bone('+',@r) if $acmp == 0;
28 return $x->bzero(@r) if $acmp < 0 || $x->is_one();
30 # blog($x,$base) ** $base + $y = $x
32 # this trial multiplication is very fast, even for large counts (like for
33 # 2 ** 1024, since this still requires only 1024 very fast steps
34 # (multiplication of a large number by a very small number is very fast))
35 # See Calc for an even faster algorightmn
36 my $x_org = $x->copy(); # preserve orgx
37 $x->bzero(); # keep ref to $x
38 my $trial = $base->copy();
39 while ($trial->bacmp($x_org) <= 0)
41 $trial->bmul($base); $x->binc();
48 my ($self,$x,$y,@r) = @_;
50 my ($u, $u1) = ($self->bzero(), $self->bone());
51 my ($a, $b) = ($y->copy(), $x->copy());
53 # first step need always be done since $num (and thus $b) is never 0
54 # Note that the loop is aligned so that the check occurs between #2 and #1
55 # thus saving us one step #2 at the loop end. Typical loop count is 1. Even
56 # a case with 28 loops still gains about 3% with this layout.
58 ($a, $q, $b) = ($b, $a->bdiv($b)); # step #1
59 # Euclid's Algorithm (calculate GCD of ($a,$b) in $a and also calculate
60 # two values in $u and $u1, we use only $u1 afterwards)
61 my $sign = 1; # flip-flop
62 while (!$b->is_zero()) # found GCD if $b == 0
64 # the original algorithm had:
65 # ($u, $u1) = ($u1, $u->bsub($u1->copy()->bmul($q))); # step #2
66 # The following creates exact the same sequence of numbers in $u1,
67 # except for the sign ($u1 is now always positive). Since formerly
68 # the sign of $u1 was alternating between '-' and '+', the $sign
69 # flip-flop will take care of that, so that at the end of the loop
70 # we have the real sign of $u1. Keeping numbers positive gains us
71 # speed since badd() is faster than bsub() and makes it possible
72 # to have the algorithmn in Calc for even more speed.
74 ($u, $u1) = ($u1, $u->badd($u1->copy()->bmul($q))); # step #2
75 $sign = - $sign; # flip sign
77 ($a, $q, $b) = ($b, $a->bdiv($b)); # step #1 again
80 # If the gcd is not 1, then return NaN! It would be pointless to have
81 # called bgcd to check this first, because we would then be performing
82 # the same Euclidean Algorithm *twice* in case the gcd is 1.
83 return $x->bnan() unless $a->is_one();
85 $u1->bneg() if $sign != 1; # need to flip?
87 $u1->bmod($y); # calc result
88 $x->{value} = $u1->{value}; # and copy over to $x
89 $x->{sign} = $u1->{sign}; # to modify in place
95 my ($self,$num,$exp,$mod,@r) = @_;
97 # in the trivial case,
98 return $num->bzero(@r) if $mod->is_one();
99 return $num->bone('+',@r) if $num->is_zero() or $num->is_one();
101 # $num->bmod($mod); # if $x is large, make it smaller first
102 my $acc = $num->copy(); # but this is not really faster...
104 $num->bone(); # keep ref to $num
106 my $expbin = $exp->as_bin(); $expbin =~ s/^[-]?0b//; # ignore sign and prefix
107 my $len = CORE::length($expbin);
110 $num->bmul($acc)->bmod($mod) if substr($expbin,$len,1) eq '1';
111 $acc->bmul($acc)->bmod($mod);
119 my ($self,$x,@r) = @_;
121 return $x->bone('+',@r) if $x->is_zero() || $x->is_one(); # 0 or 1 => 1
125 # seems we need not to temp. clear A/P of $x since the result is the same
126 my $f = $self->new(2);
127 while ($f->bacmp($n) < 0)
129 $x->bmul($f); $f->binc();
131 $x->bmul($f,@r); # last step and also round result
136 my ($self,$x,$y,@r) = @_;
138 return $x->bone('+',@r) if $y->is_zero();
139 return $x->round(@r) if $x->is_one() || $y->is_one();
140 return $x->round(@r) if $x->is_zero(); # 0**y => 0 (if not y <= 0)
142 my $pow2 = $self->bone();
143 my $y_bin = $y->as_bin(); $y_bin =~ s/^0b//;
144 my $len = CORE::length($y_bin);
147 $pow2->bmul($x) if substr($y_bin,$len,1) eq '1'; # is odd?
151 $x->round(@r) if !exists $x->{_f} || $x->{_f} & MB_NEVER_ROUND == 0;
157 my ($self,$x,$y,$sx,$sy,@r) = @_;
159 return $x->bzero(@r) if $y->is_zero() || $x->is_zero();
161 my $sign = 0; # sign of result
162 $sign = 1 if $sx == -1 && $sy == -1;
166 if ($sx == -1) # if x is negative
168 # two's complement: inc and flip all "bits" in $bx
169 $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
171 $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
175 $bx = $x->as_hex(); # get binary representation
177 $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
179 if ($sy == -1) # if y is negative
181 # two's complement: inc and flip all "bits" in $by
182 $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
184 $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
188 $by = $y->as_hex(); # get binary representation
190 $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
192 # now we have bit-strings from X and Y, reverse them for padding
196 # cut the longer string to the length of the shorter one (the result would
197 # be 0 due to AND anyway)
198 my $diff = CORE::length($bx) - CORE::length($by);
201 $bx = substr($bx,0,CORE::length($by));
205 $by = substr($by,0,CORE::length($bx));
208 # and the strings together
211 # and reverse the result again
214 # one of $x or $y was negative, so need to flip bits in the result
215 # in both cases (one or two of them negative, or both positive) we need
216 # to get the characters back.
219 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/;
223 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/;
227 if ($CALC_EMU->can('_from_hex'))
229 $x->{value} = $CALC_EMU->_from_hex( \$bx );
233 $r = $self->new($bx);
234 $x->{value} = $r->{value};
237 # calculate sign of result
239 #$x->{sign} = '-' if $sx == $sy && $sx == -1 && !$x->is_zero();
240 $x->{sign} = '-' if $sign == 1 && !$x->is_zero();
242 $x->bdec() if $sign == 1;
249 my ($self,$x,$y,$sx,$sy,@r) = @_;
251 return $x->round(@r) if $y->is_zero();
253 my $sign = 0; # sign of result
254 $sign = 1 if ($sx == -1) || ($sy == -1);
258 if ($sx == -1) # if x is negative
260 # two's complement: inc and flip all "bits" in $bx
261 $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
263 $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
267 $bx = $x->as_hex(); # get binary representation
269 $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
271 if ($sy == -1) # if y is negative
273 # two's complement: inc and flip all "bits" in $by
274 $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
276 $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
280 $by = $y->as_hex(); # get binary representation
282 $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
284 # now we have bit-strings from X and Y, reverse them for padding
288 # padd the shorter string
289 my $xx = "\x00"; $xx = "\x0f" if $sx == -1;
290 my $yy = "\x00"; $yy = "\x0f" if $sy == -1;
291 my $diff = CORE::length($bx) - CORE::length($by);
298 $bx .= $xx x abs($diff);
301 # or the strings together
304 # and reverse the result again
307 # one of $x or $y was negative, so need to flip bits in the result
308 # in both cases (one or two of them negative, or both positive) we need
309 # to get the characters back.
312 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/;
316 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/;
320 if ($CALC_EMU->can('_from_hex'))
322 $x->{value} = $CALC_EMU->_from_hex( \$bx );
326 $r = $self->new($bx);
327 $x->{value} = $r->{value};
330 # if one of X or Y was negative, we need to decrement result
331 $x->bdec() if $sign == 1;
338 my ($self,$x,$y,$sx,$sy,@r) = @_;
340 return $x->round(@r) if $y->is_zero();
342 my $sign = 0; # sign of result
343 $sign = 1 if $x->{sign} ne $y->{sign};
347 if ($sx == -1) # if x is negative
349 # two's complement: inc and flip all "bits" in $bx
350 $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
352 $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
356 $bx = $x->as_hex(); # get binary representation
358 $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
360 if ($sy == -1) # if y is negative
362 # two's complement: inc and flip all "bits" in $by
363 $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc
365 $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
369 $by = $y->as_hex(); # get binary representation
371 $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/;
373 # now we have bit-strings from X and Y, reverse them for padding
377 # padd the shorter string
378 my $xx = "\x00"; $xx = "\x0f" if $sx == -1;
379 my $yy = "\x00"; $yy = "\x0f" if $sy == -1;
380 my $diff = CORE::length($bx) - CORE::length($by);
387 $bx .= $xx x abs($diff);
390 # xor the strings together
393 # and reverse the result again
396 # one of $x or $y was negative, so need to flip bits in the result
397 # in both cases (one or two of them negative, or both positive) we need
398 # to get the characters back.
401 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/;
405 $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/;
409 if ($CALC_EMU->can('_from_hex'))
411 $x->{value} = $CALC_EMU->_from_hex( \$bx );
415 $r = $self->new($bx);
416 $x->{value} = $r->{value};
419 # calculate sign of result
421 $x->{sign} = '-' if $sx != $sy && !$x->is_zero();
423 $x->bdec() if $sign == 1;
430 my ($self,$x,@r) = @_;
433 return $x->round(@r) if $x->is_zero(); # 0,1 => 0,1
435 return $x->bone('+',@r) if $x < 4; # 1,2,3 => 1
437 my $l = int($x->length()/2);
439 $x->bone(); # keep ref($x), but modify it
440 $x->blsft($l,10) if $l != 0; # first guess: 1.('0' x (l/2))
442 my $last = $self->bzero();
443 my $two = $self->new(2);
444 my $lastlast = $self->bzero();
445 #my $lastlast = $x+$two;
446 while ($last != $x && $lastlast != $x)
448 $lastlast = $last; $last = $x->copy();
452 $x->bdec() if $x * $x > $y; # overshot?
458 my ($self,$x,$y,@r) = @_;
460 return $x->bsqrt() if $y->bacmp(2) == 0; # 2 => square root
462 # since we take at least a cubic root, and only 8 ** 1/3 >= 2 (==2):
463 return $x->bone('+',@r) if $x < 8; # $x=2..7 => 1
465 my $num = $x->numify();
469 $x = $self->new( int ( sprintf ("%.8f", $num ** (1 / $y->numify() ))));
470 return $x->round(@r);
473 # if $n is a power of two, we can repeatedly take sqrt($X) and find the
474 # proper result, because sqrt(sqrt($x)) == root($x,4)
475 # See Calc.pm for more details
476 my $b = $y->as_bin();
477 if ($b =~ /0b1(0+)$/)
479 my $count = CORE::length($1); # 0b100 => len('00') => 2
480 my $cnt = $count; # counter for loop
481 my $shift = $self->new(6);
482 $x->blsft($shift); # add some zeros (even amount)
485 # 'inflate' $X by adding more zeros
487 # calculate sqrt($x), $x is now a bit too big, again. In the next
488 # round we make even bigger, again.
491 # $x is still to big, so truncate result
496 # trial computation by starting with 2,4,6,8,10 etc until we overstep
498 my $trial = $self->new(2);
499 my $two = $self->new(2);
500 my $s_128 = $self->new(128);
502 local undef $Math::BigInt::accuracy;
503 local undef $Math::BigInt::precision;
505 # while still to do more than X steps
508 $step = $self->new(2);
509 while ( $trial->copy->bpow($y)->bacmp($x) < 0)
516 if ( $trial->copy->bpow($y)->bacmp($x) == 0)
518 $x->{value} = $trial->{value}; # make copy while preserving ref to $x
519 return $x->round(@r);
521 # overstepped, so go back on step
523 } while ($step > $s_128);
525 $step = $two->copy();
526 while ( $trial->copy->bpow($y)->bacmp($x) < 0)
532 if ( $x->bacmp( $trial->copy()->bpow($y) ) < 0)
536 # copy result into $x (preserve ref)
537 $x->{value} = $trial->{value};
544 my ($self,$x,$s) = @_;
546 return '0x0' if $x->is_zero();
548 my $x1 = $x->copy()->babs(); my ($xr,$x10000,$h,$es);
551 $x10000 = $self->new (0x10000); $h = 'h4';
555 $x10000 = $self->new (0x1000); $h = 'h3';
557 while (!$x1->is_zero())
559 ($x1, $xr) = bdiv($x1,$x10000);
560 $es .= unpack($h,pack('v',$xr->numify()));
563 $es =~ s/^[0]+//; # strip leading zeros
569 my ($self,$x,$s) = @_;
571 return '0b0' if $x->is_zero();
573 my $x1 = $x->copy()->babs(); my ($xr,$x10000,$b,$es);
576 $x10000 = $self->new (0x10000); $b = 'b16';
580 $x10000 = $self->new (0x1000); $b = 'b12';
582 while (!$x1->is_zero())
584 ($x1, $xr) = bdiv($x1,$x10000);
585 $es .= unpack($b,pack('v',$xr->numify()));
588 $es =~ s/^[0]+//; # strip leading zeros
592 ##############################################################################
593 ##############################################################################
600 Math::BigInt::CalcEmu - Emulate low-level math with BigInt code
604 Contains routines that emulate low-level math functions in BigInt, e.g.
605 optional routines the low-level math package does not provide on it's own.
607 Will be loaded on demand and automatically by BigInt.
609 Stuff here is really low-priority to optimize,
610 since it is far better to implement the operation in the low-level math
611 libary directly, possible even using a call to the native lib.
619 This program is free software; you may redistribute it and/or modify it under
620 the same terms as Perl itself.
624 (c) Tels http://bloodgate.com 2003 - based on BigInt code by
629 L<Math::BigInt>, L<Math::BigFloat>, L<Math::BigInt::BitVect>,
630 L<Math::BigInt::GMP> and L<Math::BigInt::Pari>.