1 package Math::BigFloat;
4 # Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After'
7 # The following hash values are internally used:
8 # _e : exponent (ref to $CALC object)
9 # _m : mantissa (ref to $CALC object)
11 # sign : +,-,+inf,-inf, or "NaN" if not a number
19 @ISA = qw/Math::BigInt/;
23 # $_trap_inf/$_trap_nan are internal and should never be accessed from outside
24 use vars qw/$AUTOLOAD $accuracy $precision $div_scale $round_mode $rnd_mode
25 $upgrade $downgrade $_trap_nan $_trap_inf/;
26 my $class = "Math::BigFloat";
29 '<=>' => sub { my $rc = $_[2] ?
30 ref($_[0])->bcmp($_[1],$_[0]) :
31 ref($_[0])->bcmp($_[0],$_[1]);
32 $rc = 1 unless defined $rc;
35 # we need '>=' to get things like "1 >= NaN" right:
36 '>=' => sub { my $rc = $_[2] ?
37 ref($_[0])->bcmp($_[1],$_[0]) :
38 ref($_[0])->bcmp($_[0],$_[1]);
39 # if there was a NaN involved, return false
40 return '' unless defined $rc;
43 'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint
46 ##############################################################################
47 # global constants, flags and assorted stuff
49 # the following are public, but their usage is not recommended. Use the
50 # accessor methods instead.
52 # class constants, use Class->constant_name() to access
53 # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'
61 # the package we are using for our private parts, defaults to:
62 # Math::BigInt->config()->{lib}
63 my $MBI = 'Math::BigInt::FastCalc';
65 # are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config()
67 # the same for infinity
70 # constant for easier life
73 my $IMPORT = 0; # was import() called yet? used to make require work
75 # some digits of accuracy for blog(undef,10); which we use in blog() for speed
77 '2.3025850929940456840179914546843642076011014886287729760333279009675726097';
78 my $LOG_10_A = length($LOG_10)-1;
81 '0.6931471805599453094172321214581765680755001343602552541206800094933936220';
82 my $LOG_2_A = length($LOG_2)-1;
83 my $HALF = '0.5'; # made into an object if nec.
85 ##############################################################################
86 # the old code had $rnd_mode, so we need to support it, too
88 sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; }
89 sub FETCH { return $round_mode; }
90 sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); }
94 # when someone sets $rnd_mode, we catch this and check the value to see
95 # whether it is valid or not.
96 $rnd_mode = 'even'; tie $rnd_mode, 'Math::BigFloat';
98 # we need both of them in this package:
99 *as_int = \&as_number;
102 ##############################################################################
105 # valid method aliases for AUTOLOAD
106 my %methods = map { $_ => 1 }
107 qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm
108 fint facmp fcmp fzero fnan finf finc fdec ffac fneg
109 fceil ffloor frsft flsft fone flog froot fexp
111 # valid methods that can be handed up (for AUTOLOAD)
112 my %hand_ups = map { $_ => 1 }
113 qw / is_nan is_inf is_negative is_positive is_pos is_neg
114 accuracy precision div_scale round_mode fabs fnot
115 objectify upgrade downgrade
120 sub _method_alias { exists $methods{$_[0]||''}; }
121 sub _method_hand_up { exists $hand_ups{$_[0]||''}; }
124 ##############################################################################
129 # create a new BigFloat object from a string or another bigfloat object.
132 # sign => sign (+/-), or "NaN"
134 my ($class,$wanted,@r) = @_;
136 # avoid numify-calls by not using || on $wanted!
137 return $class->bzero() if !defined $wanted; # default to 0
138 return $wanted->copy() if UNIVERSAL::isa($wanted,'Math::BigFloat');
140 $class->import() if $IMPORT == 0; # make require work
142 my $self = {}; bless $self, $class;
143 # shortcut for bigints and its subclasses
144 if ((ref($wanted)) && UNIVERSAL::can( $wanted, "as_number"))
146 $self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy
147 $self->{_e} = $MBI->_zero();
149 $self->{sign} = $wanted->sign();
150 return $self->bnorm();
152 # else: got a string or something maskerading as number (with overload)
154 # handle '+inf', '-inf' first
155 if ($wanted =~ /^[+-]?inf\z/)
157 return $downgrade->new($wanted) if $downgrade;
159 $self->{sign} = $wanted; # set a default sign for bstr()
160 return $self->binf($wanted);
163 # shortcut for simple forms like '12' that neither have trailing nor leading
165 if ($wanted =~ /^([+-]?)([1-9][0-9]*[1-9])$/)
167 $self->{_e} = $MBI->_zero();
169 $self->{sign} = $1 || '+';
170 $self->{_m} = $MBI->_new($2);
171 return $self->round(@r) if !$downgrade;
174 my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($wanted);
180 Carp::croak ("$wanted is not a number initialized to $class");
183 return $downgrade->bnan() if $downgrade;
185 $self->{_e} = $MBI->_zero();
187 $self->{_m} = $MBI->_zero();
188 $self->{sign} = $nan;
192 # make integer from mantissa by adjusting exp, then convert to int
193 $self->{_e} = $MBI->_new($$ev); # exponent
194 $self->{_es} = $$es || '+';
195 my $mantissa = "$$miv$$mfv"; # create mant.
196 $mantissa =~ s/^0+(\d)/$1/; # strip leading zeros
197 $self->{_m} = $MBI->_new($mantissa); # create mant.
199 # 3.123E0 = 3123E-3, and 3.123E-2 => 3123E-5
200 if (CORE::length($$mfv) != 0)
202 my $len = $MBI->_new( CORE::length($$mfv));
203 ($self->{_e}, $self->{_es}) =
204 _e_sub ($self->{_e}, $len, $self->{_es}, '+');
206 # we can only have trailing zeros on the mantissa if $$mfv eq ''
209 # Use a regexp to count the trailing zeros in $$miv instead of _zeros()
210 # because that is faster, especially when _m is not stored in base 10.
211 my $zeros = 0; $zeros = CORE::length($1) if $$miv =~ /[1-9](0*)$/;
214 my $z = $MBI->_new($zeros);
215 # turn '120e2' into '12e3'
216 $MBI->_rsft ( $self->{_m}, $z, 10);
217 ($self->{_e}, $self->{_es}) =
218 _e_add ( $self->{_e}, $z, $self->{_es}, '+');
221 $self->{sign} = $$mis;
223 # for something like 0Ey, set y to 1, and -0 => +0
224 # Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not
225 # have become 0. That's faster than to call $MBI->_is_zero().
226 $self->{sign} = '+', $self->{_e} = $MBI->_one()
227 if $$miv eq '0' and $$mfv eq '';
229 return $self->round(@r) if !$downgrade;
231 # if downgrade, inf, NaN or integers go down
233 if ($downgrade && $self->{_es} eq '+')
235 if ($MBI->_is_zero( $self->{_e} ))
237 return $downgrade->new($$mis . $MBI->_str( $self->{_m} ));
239 return $downgrade->new($self->bsstr());
241 $self->bnorm()->round(@r); # first normalize, then round
246 # if two arguments, the first one is the class to "swallow" subclasses
250 sign => $_[1]->{sign},
252 _m => $MBI->_copy($_[1]->{_m}),
253 _e => $MBI->_copy($_[1]->{_e}),
256 $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a};
257 $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p};
262 sign => $_[0]->{sign},
264 _m => $MBI->_copy($_[0]->{_m}),
265 _e => $MBI->_copy($_[0]->{_e}),
268 $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a};
269 $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p};
275 # used by parent class bone() to initialize number to NaN
281 my $class = ref($self);
282 Carp::croak ("Tried to set $self to NaN in $class\::_bnan()");
285 $IMPORT=1; # call our import only once
286 $self->{_m} = $MBI->_zero();
287 $self->{_e} = $MBI->_zero();
293 # used by parent class bone() to initialize number to +-inf
299 my $class = ref($self);
300 Carp::croak ("Tried to set $self to +-inf in $class\::_binf()");
303 $IMPORT=1; # call our import only once
304 $self->{_m} = $MBI->_zero();
305 $self->{_e} = $MBI->_zero();
311 # used by parent class bone() to initialize number to 1
313 $IMPORT=1; # call our import only once
314 $self->{_m} = $MBI->_one();
315 $self->{_e} = $MBI->_zero();
321 # used by parent class bone() to initialize number to 0
323 $IMPORT=1; # call our import only once
324 $self->{_m} = $MBI->_zero();
325 $self->{_e} = $MBI->_one();
331 my ($self,$class) = @_;
332 return if $class =~ /^Math::BigInt/; # we aren't one of these
333 UNIVERSAL::isa($self,$class);
338 # return (later set?) configuration data as hash ref
339 my $class = shift || 'Math::BigFloat';
341 if (@_ == 1 && ref($_[0]) ne 'HASH')
343 my $cfg = $class->SUPER::config();
344 return $cfg->{$_[0]};
347 my $cfg = $class->SUPER::config(@_);
349 # now we need only to override the ones that are different from our parent
350 $cfg->{class} = $class;
355 ##############################################################################
356 # string conversation
360 # (ref to BFLOAT or num_str ) return num_str
361 # Convert number from internal format to (non-scientific) string format.
362 # internal format is always normalized (no leading zeros, "-0" => "+0")
363 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
365 if ($x->{sign} !~ /^[+-]$/)
367 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
371 my $es = '0'; my $len = 1; my $cad = 0; my $dot = '.';
374 my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m}));
377 $es = $MBI->_str($x->{_m});
378 $len = CORE::length($es);
379 my $e = $MBI->_num($x->{_e});
380 $e = -$e if $x->{_es} eq '-';
384 # if _e is bigger than a scalar, the following will blow your memory
387 my $r = abs($e) - $len;
388 $es = '0.'. ('0' x $r) . $es; $cad = -($len+$r);
392 substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e});
393 $cad = -$cad if $x->{_es} eq '-';
399 $es .= '0' x $e; $len += $e; $cad = 0;
403 $es = '-'.$es if $x->{sign} eq '-';
404 # if set accuracy or precision, pad with zeros on the right side
405 if ((defined $x->{_a}) && ($not_zero))
407 # 123400 => 6, 0.1234 => 4, 0.001234 => 4
408 my $zeros = $x->{_a} - $cad; # cad == 0 => 12340
409 $zeros = $x->{_a} - $len if $cad != $len;
410 $es .= $dot.'0' x $zeros if $zeros > 0;
412 elsif ((($x->{_p} || 0) < 0))
414 # 123400 => 6, 0.1234 => 4, 0.001234 => 6
415 my $zeros = -$x->{_p} + $cad;
416 $es .= $dot.'0' x $zeros if $zeros > 0;
423 # (ref to BFLOAT or num_str ) return num_str
424 # Convert number from internal format to scientific string format.
425 # internal format is always normalized (no leading zeros, "-0E0" => "+0E0")
426 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
428 if ($x->{sign} !~ /^[+-]$/)
430 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
433 my $sep = 'e'.$x->{_es};
434 my $sign = $x->{sign}; $sign = '' if $sign eq '+';
435 $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e});
440 # Make a number from a BigFloat object
441 # simple return a string and let Perl's atoi()/atof() handle the rest
442 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
446 ##############################################################################
447 # public stuff (usually prefixed with "b")
451 # (BINT or num_str) return BINT
452 # negate number or make a negated number from string
453 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
455 return $x if $x->modify('bneg');
457 # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN'
458 $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m}));
463 # XXX TODO this must be overwritten and return NaN for non-integer values
464 # band(), bior(), bxor(), too
467 # $class->SUPER::bnot($class,@_);
472 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
475 my ($self,$x,$y) = (ref($_[0]),@_);
476 # objectify is costly, so avoid it
477 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
479 ($self,$x,$y) = objectify(2,@_);
482 return $upgrade->bcmp($x,$y) if defined $upgrade &&
483 ((!$x->isa($self)) || (!$y->isa($self)));
485 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
487 # handle +-inf and NaN
488 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
489 return 0 if ($x->{sign} eq $y->{sign}) && ($x->{sign} =~ /^[+-]inf$/);
490 return +1 if $x->{sign} eq '+inf';
491 return -1 if $x->{sign} eq '-inf';
492 return -1 if $y->{sign} eq '+inf';
496 # check sign for speed first
497 return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y
498 return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0
501 my $xz = $x->is_zero();
502 my $yz = $y->is_zero();
503 return 0 if $xz && $yz; # 0 <=> 0
504 return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
505 return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
507 # adjust so that exponents are equal
508 my $lxm = $MBI->_len($x->{_m});
509 my $lym = $MBI->_len($y->{_m});
510 # the numify somewhat limits our length, but makes it much faster
511 my ($xes,$yes) = (1,1);
512 $xes = -1 if $x->{_es} ne '+';
513 $yes = -1 if $y->{_es} ne '+';
514 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
515 my $ly = $lym + $yes * $MBI->_num($y->{_e});
516 my $l = $lx - $ly; $l = -$l if $x->{sign} eq '-';
517 return $l <=> 0 if $l != 0;
519 # lengths (corrected by exponent) are equal
520 # so make mantissa equal length by padding with zero (shift left)
521 my $diff = $lxm - $lym;
522 my $xm = $x->{_m}; # not yet copy it
526 $ym = $MBI->_copy($y->{_m});
527 $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10);
531 $xm = $MBI->_copy($x->{_m});
532 $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10);
534 my $rc = $MBI->_acmp($xm,$ym);
535 $rc = -$rc if $x->{sign} eq '-'; # -124 < -123
541 # Compares 2 values, ignoring their signs.
542 # Returns one of undef, <0, =0, >0. (suitable for sort)
545 my ($self,$x,$y) = (ref($_[0]),@_);
546 # objectify is costly, so avoid it
547 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
549 ($self,$x,$y) = objectify(2,@_);
552 return $upgrade->bacmp($x,$y) if defined $upgrade &&
553 ((!$x->isa($self)) || (!$y->isa($self)));
555 # handle +-inf and NaN's
556 if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/)
558 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
559 return 0 if ($x->is_inf() && $y->is_inf());
560 return 1 if ($x->is_inf() && !$y->is_inf());
565 my $xz = $x->is_zero();
566 my $yz = $y->is_zero();
567 return 0 if $xz && $yz; # 0 <=> 0
568 return -1 if $xz && !$yz; # 0 <=> +y
569 return 1 if $yz && !$xz; # +x <=> 0
571 # adjust so that exponents are equal
572 my $lxm = $MBI->_len($x->{_m});
573 my $lym = $MBI->_len($y->{_m});
574 my ($xes,$yes) = (1,1);
575 $xes = -1 if $x->{_es} ne '+';
576 $yes = -1 if $y->{_es} ne '+';
577 # the numify somewhat limits our length, but makes it much faster
578 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
579 my $ly = $lym + $yes * $MBI->_num($y->{_e});
581 return $l <=> 0 if $l != 0;
583 # lengths (corrected by exponent) are equal
584 # so make mantissa equal-length by padding with zero (shift left)
585 my $diff = $lxm - $lym;
586 my $xm = $x->{_m}; # not yet copy it
590 $ym = $MBI->_copy($y->{_m});
591 $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10);
595 $xm = $MBI->_copy($x->{_m});
596 $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10);
598 $MBI->_acmp($xm,$ym);
603 # add second arg (BFLOAT or string) to first (BFLOAT) (modifies first)
604 # return result as BFLOAT
607 my ($self,$x,$y,@r) = (ref($_[0]),@_);
608 # objectify is costly, so avoid it
609 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
611 ($self,$x,$y,@r) = objectify(2,@_);
614 return $x if $x->modify('badd');
616 # inf and NaN handling
617 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
620 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
622 if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/))
624 # +inf++inf or -inf+-inf => same, rest is NaN
625 return $x if $x->{sign} eq $y->{sign};
628 # +-inf + something => +inf; something +-inf => +-inf
629 $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/;
633 return $upgrade->badd($x,$y,@r) if defined $upgrade &&
634 ((!$x->isa($self)) || (!$y->isa($self)));
636 $r[3] = $y; # no push!
638 # speed: no add for 0+y or x+0
639 return $x->bround(@r) if $y->is_zero(); # x+0
640 if ($x->is_zero()) # 0+y
642 # make copy, clobbering up x (modify in place!)
643 $x->{_e} = $MBI->_copy($y->{_e});
644 $x->{_es} = $y->{_es};
645 $x->{_m} = $MBI->_copy($y->{_m});
646 $x->{sign} = $y->{sign} || $nan;
647 return $x->round(@r);
650 # take lower of the two e's and adapt m1 to it to match m2
652 $e = $MBI->_zero() if !defined $e; # if no BFLOAT?
653 $e = $MBI->_copy($e); # make copy (didn't do it yet)
657 ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es});
659 my $add = $MBI->_copy($y->{_m});
661 if ($es eq '-') # < 0
663 $MBI->_lsft( $x->{_m}, $e, 10);
664 ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es);
666 elsif (!$MBI->_is_zero($e)) # > 0
668 $MBI->_lsft($add, $e, 10);
670 # else: both e are the same, so just leave them
672 if ($x->{sign} eq $y->{sign})
675 $x->{_m} = $MBI->_add($x->{_m}, $add);
679 ($x->{_m}, $x->{sign}) =
680 _e_add($x->{_m}, $add, $x->{sign}, $y->{sign});
683 # delete trailing zeros, then round
684 $x->bnorm()->round(@r);
687 # sub bsub is inherited from Math::BigInt!
691 # increment arg by one
692 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
694 return $x if $x->modify('binc');
696 if ($x->{_es} eq '-')
698 return $x->badd($self->bone(),@r); # digits after dot
701 if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf
703 # 1e2 => 100, so after the shift below _m has a '0' as last digit
704 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
705 $x->{_e} = $MBI->_zero(); # normalize
707 # we know that the last digit of $x will be '1' or '9', depending on the
711 if ($x->{sign} eq '+')
713 $MBI->_inc($x->{_m});
714 return $x->bnorm()->bround(@r);
716 elsif ($x->{sign} eq '-')
718 $MBI->_dec($x->{_m});
719 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
720 return $x->bnorm()->bround(@r);
722 # inf, nan handling etc
723 $x->badd($self->bone(),@r); # badd() does round
728 # decrement arg by one
729 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
731 return $x if $x->modify('bdec');
733 if ($x->{_es} eq '-')
735 return $x->badd($self->bone('-'),@r); # digits after dot
738 if (!$MBI->_is_zero($x->{_e}))
740 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
741 $x->{_e} = $MBI->_zero(); # normalize
745 my $zero = $x->is_zero();
747 if (($x->{sign} eq '-') || $zero)
749 $MBI->_inc($x->{_m});
750 $x->{sign} = '-' if $zero; # 0 => 1 => -1
751 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
752 return $x->bnorm()->round(@r);
755 elsif ($x->{sign} eq '+')
757 $MBI->_dec($x->{_m});
758 return $x->bnorm()->round(@r);
760 # inf, nan handling etc
761 $x->badd($self->bone('-'),@r); # does round
768 my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
770 return $x if $x->modify('blog');
772 # $base > 0, $base != 1; if $base == undef default to $base == e
775 # we need to limit the accuracy to protect against overflow
778 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
780 # also takes care of the "error in _find_round_parameters?" case
781 return $x->bnan() if $x->{sign} ne '+' || $x->is_zero();
783 # no rounding at all, so must use fallback
784 if (scalar @params == 0)
786 # simulate old behaviour
787 $params[0] = $self->div_scale(); # and round to it as accuracy
788 $params[1] = undef; # P = undef
789 $scale = $params[0]+4; # at least four more for proper round
790 $params[2] = $r; # round mode by caller or undef
791 $fallback = 1; # to clear a/p afterwards
795 # the 4 below is empirical, and there might be cases where it is not
797 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
800 return $x->bzero(@params) if $x->is_one();
801 # base not defined => base == Euler's number e
804 # make object, since we don't feed it through objectify() to still get the
805 # case of $base == undef
806 $base = $self->new($base) unless ref($base);
807 # $base > 0; $base != 1
808 return $x->bnan() if $base->is_zero() || $base->is_one() ||
809 $base->{sign} ne '+';
810 # if $x == $base, we know the result must be 1.0
811 if ($x->bcmp($base) == 0)
813 $x->bone('+',@params);
816 # clear a/p after round, since user did not request it
817 delete $x->{_a}; delete $x->{_p};
823 # when user set globals, they would interfere with our calculation, so
824 # disable them and later re-enable them
826 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
827 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
828 # we also need to disable any set A or P on $x (_find_round_parameters took
829 # them already into account), since these would interfere, too
830 delete $x->{_a}; delete $x->{_p};
831 # need to disable $upgrade in BigInt, to avoid deep recursion
832 local $Math::BigInt::upgrade = undef;
833 local $Math::BigFloat::downgrade = undef;
835 # upgrade $x if $x is not a BigFloat (handle BigInt input)
837 if (!$x->isa('Math::BigFloat'))
839 $x = Math::BigFloat->new($x);
845 # If the base is defined and an integer, try to calculate integer result
846 # first. This is very fast, and in case the real result was found, we can
848 if (defined $base && $base->is_int() && $x->is_int())
850 my $i = $MBI->_copy( $x->{_m} );
851 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
852 my $int = Math::BigInt->bzero();
854 $int->blog($base->as_number());
856 if ($base->as_number()->bpow($int) == $x)
858 # found result, return it
859 $x->{_m} = $int->{value};
860 $x->{_e} = $MBI->_zero();
869 # base is undef, so base should be e (Euler's number), so first calculate the
870 # log to base e (using reduction by 10 (and probably 2)):
871 $self->_log_10($x,$scale);
873 # and if a different base was requested, convert it
876 $base = Math::BigFloat->new($base) unless $base->isa('Math::BigFloat');
877 # not ln, but some other base (don't modify $base)
878 $x->bdiv( $base->copy()->blog(undef,$scale), $scale );
882 # shortcut to not run through _find_round_parameters again
883 if (defined $params[0])
885 $x->bround($params[0],$params[2]); # then round accordingly
889 $x->bfround($params[1],$params[2]); # then round accordingly
893 # clear a/p after round, since user did not request it
894 delete $x->{_a}; delete $x->{_p};
897 $$abr = $ab; $$pbr = $pb;
904 # Given D (digits in decimal), compute N so that N! (N factorial) is
905 # at least D digits long. D should be at least 50.
908 # two constants for the Ramanujan estimate of ln(N!)
909 my $lg2 = log(2 * 3.14159265) / 2;
912 # D = 50 => N => 42, so L = 40 and R = 50
913 my $l = 40; my $r = $d;
915 # Otherwise this does not work under -Mbignum and we do not yet have "no bignum;" :(
916 $l = $l->numify if ref($l);
917 $r = $r->numify if ref($r);
918 $lg2 = $lg2->numify if ref($lg2);
919 $lg10 = $lg10->numify if ref($lg10);
921 # binary search for the right value (could this be written as the reverse of lg(n!)?)
924 my $n = int(($r - $l) / 2) + $l;
926 int(($n * log($n) - $n + log( $n * (1 + 4*$n*(1+2*$n)) ) / 6 + $lg2) / $lg10);
927 $ramanujan > $d ? $r = $n : $l = $n;
934 # Calculate n over k (binomial coefficient or "choose" function) as integer.
936 my ($self,$x,$y,@r) = (ref($_[0]),@_);
938 # objectify is costly, so avoid it
939 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
941 ($self,$x,$y,@r) = objectify(2,@_);
944 return $x if $x->modify('bnok');
946 return $x->bnan() if $x->is_nan() || $y->is_nan();
947 return $x->binf() if $x->is_inf();
949 my $u = $x->as_int();
950 $u->bnok($y->as_int());
952 $x->{_m} = $u->{value};
953 $x->{_e} = $MBI->_zero();
961 # Calculate e ** X (Euler's number to the power of X)
962 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
964 return $x if $x->modify('bexp');
966 return $x->binf() if $x->{sign} eq '+inf';
967 return $x->bzero() if $x->{sign} eq '-inf';
969 # we need to limit the accuracy to protect against overflow
972 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
974 # also takes care of the "error in _find_round_parameters?" case
975 return $x if $x->{sign} eq 'NaN';
977 # no rounding at all, so must use fallback
978 if (scalar @params == 0)
980 # simulate old behaviour
981 $params[0] = $self->div_scale(); # and round to it as accuracy
982 $params[1] = undef; # P = undef
983 $scale = $params[0]+4; # at least four more for proper round
984 $params[2] = $r; # round mode by caller or undef
985 $fallback = 1; # to clear a/p afterwards
989 # the 4 below is empirical, and there might be cases where it's not enough...
990 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
993 return $x->bone(@params) if $x->is_zero();
995 if (!$x->isa('Math::BigFloat'))
997 $x = Math::BigFloat->new($x);
1001 # when user set globals, they would interfere with our calculation, so
1002 # disable them and later re-enable them
1004 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
1005 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
1006 # we also need to disable any set A or P on $x (_find_round_parameters took
1007 # them already into account), since these would interfere, too
1008 delete $x->{_a}; delete $x->{_p};
1009 # need to disable $upgrade in BigInt, to avoid deep recursion
1010 local $Math::BigInt::upgrade = undef;
1011 local $Math::BigFloat::downgrade = undef;
1013 my $x_org = $x->copy();
1015 # We use the following Taylor series:
1018 # e = 1 + --- + --- + --- + --- ...
1021 # The difference for each term is X and N, which would result in:
1022 # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term
1024 # But it is faster to compute exp(1) and then raising it to the
1025 # given power, esp. if $x is really big and an integer because:
1027 # * The numerator is always 1, making the computation faster
1028 # * the series converges faster in the case of x == 1
1029 # * We can also easily check when we have reached our limit: when the
1030 # term to be added is smaller than "1E$scale", we can stop - f.i.
1031 # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5.
1032 # * we can compute the *exact* result by simulating bigrat math:
1034 # 1 1 gcd(3,4) = 1 1*24 + 1*6 5
1035 # - + - = ---------- = --
1038 # We do not compute the gcd() here, but simple do:
1040 # - + - = --------- = --
1044 # a c a*d + c*b and note that c is always 1 and d = (b*f)
1048 # This leads to: which can be reduced by b to:
1049 # a 1 a*b*f + b a*f + 1
1050 # - + - = --------- = -------
1053 # The first terms in the series are:
1055 # 1 1 1 1 1 1 1 1 13700
1056 # -- + -- + -- + -- + -- + --- + --- + ---- = -----
1057 # 1 1 2 6 24 120 720 5040 5040
1059 # Note that we cannot simple reduce 13700/5040 to 685/252, but must keep A and B!
1063 # set $x directly from a cached string form
1064 $x->{_m} = $MBI->_new(
1065 "27182818284590452353602874713526624977572470936999595749669676277240766303535476");
1068 $x->{_e} = $MBI->_new(79);
1072 # compute A and B so that e = A / B.
1074 # After some terms we end up with this, so we use it as a starting point:
1075 my $A = $MBI->_new("90933395208605785401971970164779391644753259799242");
1076 my $F = $MBI->_new(42); my $step = 42;
1078 # Compute how many steps we need to take to get $A and $B sufficiently big
1079 my $steps = _len_to_steps($scale - 4);
1080 # print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
1081 while ($step++ <= $steps)
1083 # calculate $a * $f + 1
1084 $A = $MBI->_mul($A, $F);
1085 $A = $MBI->_inc($A);
1087 $F = $MBI->_inc($F);
1089 # compute $B as factorial of $steps (this is faster than doing it manually)
1090 my $B = $MBI->_fac($MBI->_new($steps));
1092 # print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n";
1094 # compute A/B with $scale digits in the result (truncate, not round)
1095 $A = $MBI->_lsft( $A, $MBI->_new($scale), 10);
1096 $A = $MBI->_div( $A, $B );
1101 $x->{_e} = $MBI->_new($scale);
1104 # $x contains now an estimate of e, with some surplus digits, so we can round
1105 if (!$x_org->is_one())
1107 # raise $x to the wanted power and round it in one step:
1108 $x->bpow($x_org, @params);
1112 # else just round the already computed result
1113 delete $x->{_a}; delete $x->{_p};
1114 # shortcut to not run through _find_round_parameters again
1115 if (defined $params[0])
1117 $x->bround($params[0],$params[2]); # then round accordingly
1121 $x->bfround($params[1],$params[2]); # then round accordingly
1126 # clear a/p after round, since user did not request it
1127 delete $x->{_a}; delete $x->{_p};
1130 $$abr = $ab; $$pbr = $pb;
1132 $x; # return modified $x
1137 # internal log function to calculate ln() based on Taylor series.
1138 # Modifies $x in place.
1139 my ($self,$x,$scale) = @_;
1141 # in case of $x == 1, result is 0
1142 return $x->bzero() if $x->is_one();
1144 # XXX TODO: rewrite this in a similiar manner to bexp()
1146 # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log
1150 # Taylor: | u 1 u^3 1 u^5 |
1151 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0
1152 # |_ v 3 v^3 5 v^5 _|
1154 # This takes much more steps to calculate the result and is thus not used
1157 # Taylor: | u 1 u^2 1 u^3 |
1158 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2
1159 # |_ x 2 x^2 3 x^3 _|
1161 my ($limit,$v,$u,$below,$factor,$two,$next,$over,$f);
1163 $v = $x->copy(); $v->binc(); # v = x+1
1164 $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1
1165 $x->bdiv($v,$scale); # first term: u/v
1166 $below = $v->copy();
1168 $u *= $u; $v *= $v; # u^2, v^2
1169 $below->bmul($v); # u^3, v^3
1171 $factor = $self->new(3); $f = $self->new(2);
1173 my $steps = 0 if DEBUG;
1174 $limit = $self->new("1E-". ($scale-1));
1177 # we calculate the next term, and add it to the last
1178 # when the next term is below our limit, it won't affect the outcome
1179 # anymore, so we stop
1181 # calculating the next term simple from over/below will result in quite
1182 # a time hog if the input has many digits, since over and below will
1183 # accumulate more and more digits, and the result will also have many
1184 # digits, but in the end it is rounded to $scale digits anyway. So if we
1185 # round $over and $below first, we save a lot of time for the division
1186 # (not with log(1.2345), but try log (123**123) to see what I mean. This
1187 # can introduce a rounding error if the division result would be f.i.
1188 # 0.1234500000001 and we round it to 5 digits it would become 0.12346, but
1189 # if we truncated $over and $below we might get 0.12345. Does this matter
1190 # for the end result? So we give $over and $below 4 more digits to be
1191 # on the safe side (unscientific error handling as usual... :+D
1193 $next = $over->copy->bround($scale+4)->bdiv(
1194 $below->copy->bmul($factor)->bround($scale+4),
1198 ## $next = $over->copy()->bdiv($below->copy()->bmul($factor),$scale);
1200 last if $next->bacmp($limit) <= 0;
1202 delete $next->{_a}; delete $next->{_p};
1204 # calculate things for the next term
1205 $over *= $u; $below *= $v; $factor->badd($f);
1208 $steps++; print "step $steps = $x\n" if $steps % 10 == 0;
1211 print "took $steps steps\n" if DEBUG;
1212 $x->bmul($f); # $x *= 2
1217 # Internal log function based on reducing input to the range of 0.1 .. 9.99
1218 # and then "correcting" the result to the proper one. Modifies $x in place.
1219 my ($self,$x,$scale) = @_;
1221 # Taking blog() from numbers greater than 10 takes a *very long* time, so we
1222 # break the computation down into parts based on the observation that:
1223 # blog(X*Y) = blog(X) + blog(Y)
1224 # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller
1225 # $x is the faster it gets. Since 2*$x takes about 10 times as
1226 # long, we make it faster by about a factor of 100 by dividing $x by 10.
1228 # The same observation is valid for numbers smaller than 0.1, e.g. computing
1229 # log(1) is fastest, and the further away we get from 1, the longer it takes.
1230 # So we also 'break' this down by multiplying $x with 10 and subtract the
1231 # log(10) afterwards to get the correct result.
1233 # To get $x even closer to 1, we also divide by 2 and then use log(2) to
1234 # correct for this. For instance if $x is 2.4, we use the formula:
1235 # blog(2.4 * 2) == blog (1.2) + blog(2)
1236 # and thus calculate only blog(1.2) and blog(2), which is faster in total
1237 # than calculating blog(2.4).
1239 # In addition, the values for blog(2) and blog(10) are cached.
1241 # Calculate nr of digits before dot:
1242 my $dbd = $MBI->_num($x->{_e});
1243 $dbd = -$dbd if $x->{_es} eq '-';
1244 $dbd += $MBI->_len($x->{_m});
1246 # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid
1247 # infinite recursion
1249 my $calc = 1; # do some calculation?
1251 # disable the shortcut for 10, since we need log(10) and this would recurse
1253 if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m}))
1255 $dbd = 0; # disable shortcut
1256 # we can use the cached value in these cases
1257 if ($scale <= $LOG_10_A)
1259 $x->bzero(); $x->badd($LOG_10); # modify $x in place
1260 $calc = 0; # no need to calc, but round
1262 # if we can't use the shortcut, we continue normally
1266 # disable the shortcut for 2, since we maybe have it cached
1267 if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m})))
1269 $dbd = 0; # disable shortcut
1270 # we can use the cached value in these cases
1271 if ($scale <= $LOG_2_A)
1273 $x->bzero(); $x->badd($LOG_2); # modify $x in place
1274 $calc = 0; # no need to calc, but round
1276 # if we can't use the shortcut, we continue normally
1280 # if $x = 0.1, we know the result must be 0-log(10)
1281 if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) &&
1282 $MBI->_is_one($x->{_m}))
1284 $dbd = 0; # disable shortcut
1285 # we can use the cached value in these cases
1286 if ($scale <= $LOG_10_A)
1288 $x->bzero(); $x->bsub($LOG_10);
1289 $calc = 0; # no need to calc, but round
1293 return if $calc == 0; # already have the result
1295 # default: these correction factors are undef and thus not used
1296 my $l_10; # value of ln(10) to A of $scale
1297 my $l_2; # value of ln(2) to A of $scale
1299 my $two = $self->new(2);
1301 # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1
1302 # so don't do this shortcut for 1 or 0
1303 if (($dbd > 1) || ($dbd < 0))
1305 # convert our cached value to an object if not already (avoid doing this
1306 # at import() time, since not everybody needs this)
1307 $LOG_10 = $self->new($LOG_10,undef,undef) unless ref $LOG_10;
1309 #print "x = $x, dbd = $dbd, calc = $calc\n";
1310 # got more than one digit before the dot, or more than one zero after the
1312 # log(123) == log(1.23) + log(10) * 2
1313 # log(0.0123) == log(1.23) - log(10) * 2
1315 if ($scale <= $LOG_10_A)
1318 $l_10 = $LOG_10->copy(); # copy for mul
1322 # else: slower, compute and cache result
1323 # also disable downgrade for this code path
1324 local $Math::BigFloat::downgrade = undef;
1326 # shorten the time to calculate log(10) based on the following:
1327 # log(1.25 * 8) = log(1.25) + log(8)
1328 # = log(1.25) + log(2) + log(2) + log(2)
1330 # first get $l_2 (and possible compute and cache log(2))
1331 $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2;
1332 if ($scale <= $LOG_2_A)
1335 $l_2 = $LOG_2->copy(); # copy() for the mul below
1339 # else: slower, compute and cache result
1340 $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually
1341 $LOG_2 = $l_2->copy(); # cache the result for later
1342 # the copy() is for mul below
1346 # now calculate log(1.25):
1347 $l_10 = $self->new('1.25'); $self->_log($l_10, $scale); # scale+4, actually
1349 # log(1.25) + log(2) + log(2) + log(2):
1353 $LOG_10 = $l_10->copy(); # cache the result for later
1354 # the copy() is for mul below
1357 $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1
1358 $l_10->bmul( $self->new($dbd)); # log(10) * (digits_before_dot-1)
1365 ($x->{_e}, $x->{_es}) =
1366 _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23
1370 # Now: 0.1 <= $x < 10 (and possible correction in l_10)
1372 ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div
1373 ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1)
1375 $HALF = $self->new($HALF) unless ref($HALF);
1377 my $twos = 0; # default: none (0 times)
1378 while ($x->bacmp($HALF) <= 0) # X <= 0.5
1380 $twos--; $x->bmul($two);
1382 while ($x->bacmp($two) >= 0) # X >= 2
1384 $twos++; $x->bdiv($two,$scale+4); # keep all digits
1386 # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both)
1387 # So calculate correction factor based on ln(2):
1390 $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2;
1391 if ($scale <= $LOG_2_A)
1394 $l_2 = $LOG_2->copy(); # copy() for the mul below
1398 # else: slower, compute and cache result
1399 # also disable downgrade for this code path
1400 local $Math::BigFloat::downgrade = undef;
1401 $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually
1402 $LOG_2 = $l_2->copy(); # cache the result for later
1403 # the copy() is for mul below
1406 $l_2->bmul($twos); # * -2 => subtract, * 2 => add
1409 $self->_log($x,$scale); # need to do the "normal" way
1410 $x->badd($l_10) if defined $l_10; # correct it by ln(10)
1411 $x->badd($l_2) if defined $l_2; # and maybe by ln(2)
1413 # all done, $x contains now the result
1419 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
1420 # does not modify arguments, but returns new object
1421 # Lowest Common Multiplicator
1423 my ($self,@arg) = objectify(0,@_);
1424 my $x = $self->new(shift @arg);
1425 while (@arg) { $x = Math::BigInt::__lcm($x,shift @arg); }
1431 # (BINT or num_str, BINT or num_str) return BINT
1432 # does not modify arguments, but returns new object
1435 $y = __PACKAGE__->new($y) if !ref($y);
1437 my $x = $y->copy()->babs(); # keep arguments
1439 return $x->bnan() if $x->{sign} !~ /^[+-]$/ # x NaN?
1440 || !$x->is_int(); # only for integers now
1444 my $t = shift; $t = $self->new($t) if !ref($t);
1445 $y = $t->copy()->babs();
1447 return $x->bnan() if $y->{sign} !~ /^[+-]$/ # y NaN?
1448 || !$y->is_int(); # only for integers now
1450 # greatest common divisor
1451 while (! $y->is_zero())
1453 ($x,$y) = ($y->copy(), $x->copy()->bmod($y));
1456 last if $x->is_one();
1461 ##############################################################################
1465 # Internal helper sub to take two positive integers and their signs and
1466 # then add them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')),
1467 # output ($CALC,('+'|'-'))
1468 my ($x,$y,$xs,$ys) = @_;
1470 # if the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8)
1473 $x = $MBI->_add ($x, $y ); # a+b
1474 # the sign follows $xs
1478 my $a = $MBI->_acmp($x,$y);
1481 $x = $MBI->_sub ($x , $y); # abs sub
1485 $x = $MBI->_zero(); # result is 0
1490 $x = $MBI->_sub ( $y, $x, 1 ); # abs sub
1498 # Internal helper sub to take two positive integers and their signs and
1499 # then subtract them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')),
1500 # output ($CALC,('+'|'-'))
1501 my ($x,$y,$xs,$ys) = @_;
1505 _e_add($x,$y,$xs,$ys); # call add (does subtract now)
1508 ###############################################################################
1509 # is_foo methods (is_negative, is_positive are inherited from BigInt)
1513 # return true if arg (BFLOAT or num_str) is an integer
1514 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1516 (($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
1517 ($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer
1522 # return true if arg (BFLOAT or num_str) is zero
1523 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1525 ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})) ? 1 : 0;
1530 # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given
1531 my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_);
1533 $sign = '+' if !defined $sign || $sign ne '-';
1535 ($x->{sign} eq $sign &&
1536 $MBI->_is_zero($x->{_e}) &&
1537 $MBI->_is_one($x->{_m}) ) ? 1 : 0;
1542 # return true if arg (BFLOAT or num_str) is odd or false if even
1543 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1545 (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1546 ($MBI->_is_zero($x->{_e})) &&
1547 ($MBI->_is_odd($x->{_m}))) ? 1 : 0;
1552 # return true if arg (BINT or num_str) is even or false if odd
1553 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1555 (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1556 ($x->{_es} eq '+') && # 123.45 isn't
1557 ($MBI->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is
1562 # multiply two numbers
1565 my ($self,$x,$y,@r) = (ref($_[0]),@_);
1566 # objectify is costly, so avoid it
1567 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1569 ($self,$x,$y,@r) = objectify(2,@_);
1572 return $x if $x->modify('bmul');
1574 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
1577 if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/))
1579 return $x->bnan() if $x->is_zero() || $y->is_zero();
1580 # result will always be +-inf:
1581 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1582 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1583 return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
1584 return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
1585 return $x->binf('-');
1588 return $upgrade->bmul($x,$y,@r) if defined $upgrade &&
1589 ((!$x->isa($self)) || (!$y->isa($self)));
1591 # aEb * cEd = (a*c)E(b+d)
1592 $MBI->_mul($x->{_m},$y->{_m});
1593 ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1595 $r[3] = $y; # no push!
1598 $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
1599 $x->bnorm->round(@r);
1604 # multiply two numbers and add the third to the result
1607 my ($self,$x,$y,$z,@r) = (ref($_[0]),@_);
1608 # objectify is costly, so avoid it
1609 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1611 ($self,$x,$y,$z,@r) = objectify(3,@_);
1614 return $x if $x->modify('bmuladd');
1616 return $x->bnan() if (($x->{sign} eq $nan) ||
1617 ($y->{sign} eq $nan) ||
1618 ($z->{sign} eq $nan));
1621 if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/))
1623 return $x->bnan() if $x->is_zero() || $y->is_zero();
1624 # result will always be +-inf:
1625 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1626 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1627 return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
1628 return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
1629 return $x->binf('-');
1632 return $upgrade->bmul($x,$y,@r) if defined $upgrade &&
1633 ((!$x->isa($self)) || (!$y->isa($self)));
1635 # aEb * cEd = (a*c)E(b+d)
1636 $MBI->_mul($x->{_m},$y->{_m});
1637 ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1639 $r[3] = $y; # no push!
1642 $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
1644 # z=inf handling (z=NaN handled above)
1645 $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/;
1647 # take lower of the two e's and adapt m1 to it to match m2
1649 $e = $MBI->_zero() if !defined $e; # if no BFLOAT?
1650 $e = $MBI->_copy($e); # make copy (didn't do it yet)
1654 ($e,$es) = _e_sub($e, $x->{_e}, $z->{_es} || '+', $x->{_es});
1656 my $add = $MBI->_copy($z->{_m});
1658 if ($es eq '-') # < 0
1660 $MBI->_lsft( $x->{_m}, $e, 10);
1661 ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es);
1663 elsif (!$MBI->_is_zero($e)) # > 0
1665 $MBI->_lsft($add, $e, 10);
1667 # else: both e are the same, so just leave them
1669 if ($x->{sign} eq $z->{sign})
1672 $x->{_m} = $MBI->_add($x->{_m}, $add);
1676 ($x->{_m}, $x->{sign}) =
1677 _e_add($x->{_m}, $add, $x->{sign}, $z->{sign});
1680 # delete trailing zeros, then round
1681 $x->bnorm()->round(@r);
1686 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return
1687 # (BFLOAT,BFLOAT) (quo,rem) or BFLOAT (only rem)
1690 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1691 # objectify is costly, so avoid it
1692 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1694 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1697 return $x if $x->modify('bdiv');
1699 return $self->_div_inf($x,$y)
1700 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
1702 # x== 0 # also: or y == 1 or y == -1
1703 return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero();
1706 return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade;
1708 # we need to limit the accuracy to protect against overflow
1710 my (@params,$scale);
1711 ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y);
1713 return $x if $x->is_nan(); # error in _find_round_parameters?
1715 # no rounding at all, so must use fallback
1716 if (scalar @params == 0)
1718 # simulate old behaviour
1719 $params[0] = $self->div_scale(); # and round to it as accuracy
1720 $scale = $params[0]+4; # at least four more for proper round
1721 $params[2] = $r; # round mode by caller or undef
1722 $fallback = 1; # to clear a/p afterwards
1726 # the 4 below is empirical, and there might be cases where it is not
1728 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1731 my $rem; $rem = $self->bzero() if wantarray;
1733 $y = $self->new($y) unless $y->isa('Math::BigFloat');
1735 my $lx = $MBI->_len($x->{_m}); my $ly = $MBI->_len($y->{_m});
1736 $scale = $lx if $lx > $scale;
1737 $scale = $ly if $ly > $scale;
1738 my $diff = $ly - $lx;
1739 $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx!
1741 # already handled inf/NaN/-inf above:
1743 # check that $y is not 1 nor -1 and cache the result:
1744 my $y_not_one = !($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m}));
1746 # flipping the sign of $y will also flip the sign of $x for the special
1747 # case of $x->bsub($x); so we can catch it below:
1748 my $xsign = $x->{sign};
1749 $y->{sign} =~ tr/+-/-+/;
1751 if ($xsign ne $x->{sign})
1753 # special case of $x /= $x results in 1
1754 $x->bone(); # "fixes" also sign of $y, since $x is $y
1758 # correct $y's sign again
1759 $y->{sign} =~ tr/+-/-+/;
1760 # continue with normal div code:
1762 # make copy of $x in case of list context for later reminder calculation
1763 if (wantarray && $y_not_one)
1768 $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+';
1770 # check for / +-1 ( +/- 1E0)
1773 # promote BigInts and it's subclasses (except when already a BigFloat)
1774 $y = $self->new($y) unless $y->isa('Math::BigFloat');
1776 # calculate the result to $scale digits and then round it
1777 # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d)
1778 $MBI->_lsft($x->{_m},$MBI->_new($scale),10);
1779 $MBI->_div ($x->{_m},$y->{_m}); # a/c
1781 # correct exponent of $x
1782 ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1783 # correct for 10**scale
1784 ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+');
1785 $x->bnorm(); # remove trailing 0's
1787 } # ende else $x != $y
1789 # shortcut to not run through _find_round_parameters again
1790 if (defined $params[0])
1792 delete $x->{_a}; # clear before round
1793 $x->bround($params[0],$params[2]); # then round accordingly
1797 delete $x->{_p}; # clear before round
1798 $x->bfround($params[1],$params[2]); # then round accordingly
1802 # clear a/p after round, since user did not request it
1803 delete $x->{_a}; delete $x->{_p};
1810 $rem->bmod($y,@params); # copy already done
1814 # clear a/p after round, since user did not request it
1815 delete $rem->{_a}; delete $rem->{_p};
1824 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return reminder
1827 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1828 # objectify is costly, so avoid it
1829 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1831 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1834 return $x if $x->modify('bmod');
1836 # handle NaN, inf, -inf
1837 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
1839 my ($d,$re) = $self->SUPER::_div_inf($x,$y);
1840 $x->{sign} = $re->{sign};
1841 $x->{_e} = $re->{_e};
1842 $x->{_m} = $re->{_m};
1843 return $x->round($a,$p,$r,$y);
1847 return $x->bnan() if $x->is_zero();
1851 return $x->bzero() if $x->is_zero()
1853 # check that $y == +1 or $y == -1:
1854 ($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m})));
1856 my $cmp = $x->bacmp($y); # equal or $x < $y?
1857 return $x->bzero($a,$p) if $cmp == 0; # $x == $y => result 0
1859 # only $y of the operands negative?
1860 my $neg = 0; $neg = 1 if $x->{sign} ne $y->{sign};
1862 $x->{sign} = $y->{sign}; # calc sign first
1863 return $x->round($a,$p,$r) if $cmp < 0 && $neg == 0; # $x < $y => result $x
1865 my $ym = $MBI->_copy($y->{_m});
1868 $MBI->_lsft( $ym, $y->{_e}, 10)
1869 if $y->{_es} eq '+' && !$MBI->_is_zero($y->{_e});
1871 # if $y has digits after dot
1872 my $shifty = 0; # correct _e of $x by this
1873 if ($y->{_es} eq '-') # has digits after dot
1875 # 123 % 2.5 => 1230 % 25 => 5 => 0.5
1876 $shifty = $MBI->_num($y->{_e}); # no more digits after dot
1877 $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25
1879 # $ym is now mantissa of $y based on exponent 0
1881 my $shiftx = 0; # correct _e of $x by this
1882 if ($x->{_es} eq '-') # has digits after dot
1884 # 123.4 % 20 => 1234 % 200
1885 $shiftx = $MBI->_num($x->{_e}); # no more digits after dot
1886 $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230
1888 # 123e1 % 20 => 1230 % 20
1889 if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e}))
1891 $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here
1894 $x->{_e} = $MBI->_new($shiftx);
1896 $x->{_es} = '-' if $shiftx != 0 || $shifty != 0;
1897 $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0;
1899 # now mantissas are equalized, exponent of $x is adjusted, so calc result
1901 $x->{_m} = $MBI->_mod( $x->{_m}, $ym);
1903 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1906 if ($neg != 0) # one of them negative => correct in place
1909 $x->{_m} = $r->{_m};
1910 $x->{_e} = $r->{_e};
1911 $x->{_es} = $r->{_es};
1912 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1916 $x->round($a,$p,$r,$y); # round and return
1921 # calculate $y'th root of $x
1924 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1925 # objectify is costly, so avoid it
1926 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1928 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1931 return $x if $x->modify('broot');
1933 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
1934 return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() ||
1935 $y->{sign} !~ /^\+$/;
1937 return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one();
1939 # we need to limit the accuracy to protect against overflow
1941 my (@params,$scale);
1942 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1944 return $x if $x->is_nan(); # error in _find_round_parameters?
1946 # no rounding at all, so must use fallback
1947 if (scalar @params == 0)
1949 # simulate old behaviour
1950 $params[0] = $self->div_scale(); # and round to it as accuracy
1951 $scale = $params[0]+4; # at least four more for proper round
1952 $params[2] = $r; # iound mode by caller or undef
1953 $fallback = 1; # to clear a/p afterwards
1957 # the 4 below is empirical, and there might be cases where it is not
1959 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1962 # when user set globals, they would interfere with our calculation, so
1963 # disable them and later re-enable them
1965 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
1966 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
1967 # we also need to disable any set A or P on $x (_find_round_parameters took
1968 # them already into account), since these would interfere, too
1969 delete $x->{_a}; delete $x->{_p};
1970 # need to disable $upgrade in BigInt, to avoid deep recursion
1971 local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI
1973 # remember sign and make $x positive, since -4 ** (1/2) => -2
1974 my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+';
1977 if ($y->isa('Math::BigFloat'))
1979 $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e}));
1983 $is_two = ($y == 2);
1986 # normal square root if $y == 2:
1989 $x->bsqrt($scale+4);
1991 elsif ($y->is_one('-'))
1994 my $u = $self->bone()->bdiv($x,$scale);
1995 # copy private parts over
1996 $x->{_m} = $u->{_m};
1997 $x->{_e} = $u->{_e};
1998 $x->{_es} = $u->{_es};
2002 # calculate the broot() as integer result first, and if it fits, return
2003 # it rightaway (but only if $x and $y are integer):
2005 my $done = 0; # not yet
2006 if ($y->is_int() && $x->is_int())
2008 my $i = $MBI->_copy( $x->{_m} );
2009 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
2010 my $int = Math::BigInt->bzero();
2012 $int->broot($y->as_number());
2014 if ($int->copy()->bpow($y) == $x)
2016 # found result, return it
2017 $x->{_m} = $int->{value};
2018 $x->{_e} = $MBI->_zero();
2026 my $u = $self->bone()->bdiv($y,$scale+4);
2027 delete $u->{_a}; delete $u->{_p}; # otherwise it conflicts
2028 $x->bpow($u,$scale+4); # el cheapo
2031 $x->bneg() if $sign == 1;
2033 # shortcut to not run through _find_round_parameters again
2034 if (defined $params[0])
2036 $x->bround($params[0],$params[2]); # then round accordingly
2040 $x->bfround($params[1],$params[2]); # then round accordingly
2044 # clear a/p after round, since user did not request it
2045 delete $x->{_a}; delete $x->{_p};
2048 $$abr = $ab; $$pbr = $pb;
2054 # calculate square root
2055 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2057 return $x if $x->modify('bsqrt');
2059 return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0
2060 return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf
2061 return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one();
2063 # we need to limit the accuracy to protect against overflow
2065 my (@params,$scale);
2066 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
2068 return $x if $x->is_nan(); # error in _find_round_parameters?
2070 # no rounding at all, so must use fallback
2071 if (scalar @params == 0)
2073 # simulate old behaviour
2074 $params[0] = $self->div_scale(); # and round to it as accuracy
2075 $scale = $params[0]+4; # at least four more for proper round
2076 $params[2] = $r; # round mode by caller or undef
2077 $fallback = 1; # to clear a/p afterwards
2081 # the 4 below is empirical, and there might be cases where it is not
2083 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
2086 # when user set globals, they would interfere with our calculation, so
2087 # disable them and later re-enable them
2089 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
2090 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
2091 # we also need to disable any set A or P on $x (_find_round_parameters took
2092 # them already into account), since these would interfere, too
2093 delete $x->{_a}; delete $x->{_p};
2094 # need to disable $upgrade in BigInt, to avoid deep recursion
2095 local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI
2097 my $i = $MBI->_copy( $x->{_m} );
2098 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
2099 my $xas = Math::BigInt->bzero();
2102 my $gs = $xas->copy()->bsqrt(); # some guess
2104 if (($x->{_es} ne '-') # guess can't be accurate if there are
2105 # digits after the dot
2106 && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head?
2108 # exact result, copy result over to keep $x
2109 $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+';
2111 # shortcut to not run through _find_round_parameters again
2112 if (defined $params[0])
2114 $x->bround($params[0],$params[2]); # then round accordingly
2118 $x->bfround($params[1],$params[2]); # then round accordingly
2122 # clear a/p after round, since user did not request it
2123 delete $x->{_a}; delete $x->{_p};
2125 # re-enable A and P, upgrade is taken care of by "local"
2126 ${"$self\::accuracy"} = $ab; ${"$self\::precision"} = $pb;
2130 # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the accuracy
2131 # of the result by multipyling the input by 100 and then divide the integer
2132 # result of sqrt(input) by 10. Rounding afterwards returns the real result.
2134 # The following steps will transform 123.456 (in $x) into 123456 (in $y1)
2135 my $y1 = $MBI->_copy($x->{_m});
2137 my $length = $MBI->_len($y1);
2139 # Now calculate how many digits the result of sqrt(y1) would have
2140 my $digits = int($length / 2);
2142 # But we need at least $scale digits, so calculate how many are missing
2143 my $shift = $scale - $digits;
2145 # That should never happen (we take care of integer guesses above)
2146 # $shift = 0 if $shift < 0;
2148 # Multiply in steps of 100, by shifting left two times the "missing" digits
2149 my $s2 = $shift * 2;
2151 # We now make sure that $y1 has the same odd or even number of digits than
2152 # $x had. So when _e of $x is odd, we must shift $y1 by one digit left,
2153 # because we always must multiply by steps of 100 (sqrt(100) is 10) and not
2154 # steps of 10. The length of $x does not count, since an even or odd number
2155 # of digits before the dot is not changed by adding an even number of digits
2156 # after the dot (the result is still odd or even digits long).
2157 $s2++ if $MBI->_is_odd($x->{_e});
2159 $MBI->_lsft( $y1, $MBI->_new($s2), 10);
2161 # now take the square root and truncate to integer
2162 $y1 = $MBI->_sqrt($y1);
2164 # By "shifting" $y1 right (by creating a negative _e) we calculate the final
2165 # result, which is than later rounded to the desired scale.
2167 # calculate how many zeros $x had after the '.' (or before it, depending
2168 # on sign of $dat, the result should have half as many:
2169 my $dat = $MBI->_num($x->{_e});
2170 $dat = -$dat if $x->{_es} eq '-';
2175 # no zeros after the dot (e.g. 1.23, 0.49 etc)
2176 # preserve half as many digits before the dot than the input had
2177 # (but round this "up")
2178 $dat = int(($dat+1)/2);
2182 $dat = int(($dat)/2);
2184 $dat -= $MBI->_len($y1);
2188 $x->{_e} = $MBI->_new( $dat );
2193 $x->{_e} = $MBI->_new( $dat );
2199 # shortcut to not run through _find_round_parameters again
2200 if (defined $params[0])
2202 $x->bround($params[0],$params[2]); # then round accordingly
2206 $x->bfround($params[1],$params[2]); # then round accordingly
2210 # clear a/p after round, since user did not request it
2211 delete $x->{_a}; delete $x->{_p};
2214 $$abr = $ab; $$pbr = $pb;
2220 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
2221 # compute factorial number, modifies first argument
2224 my ($self,$x,@r) = (ref($_[0]),@_);
2225 # objectify is costly, so avoid it
2226 ($self,$x,@r) = objectify(1,@_) if !ref($x);
2229 return $x if $x->modify('bfac') || $x->{sign} eq '+inf';
2232 if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN
2233 ($x->{_es} ne '+')); # digits after dot?
2235 # use BigInt's bfac() for faster calc
2236 if (! $MBI->_is_zero($x->{_e}))
2238 $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0
2239 $x->{_e} = $MBI->_zero(); # normalize
2242 $MBI->_fac($x->{_m}); # calculate factorial
2243 $x->bnorm()->round(@r); # norm again and round result
2248 # Calculate a power where $y is a non-integer, like 2 ** 0.3
2252 # if $y == 0.5, it is sqrt($x)
2253 $HALF = $self->new($HALF) unless ref($HALF);
2254 return $x->bsqrt(@r,$y) if $y->bcmp($HALF) == 0;
2257 # a ** x == e ** (x * ln a)
2261 # Taylor: | u u^2 u^3 |
2262 # x ** y = 1 + | --- + --- + ----- + ... |
2265 # we need to limit the accuracy to protect against overflow
2267 my ($scale,@params);
2268 ($x,@params) = $x->_find_round_parameters(@r);
2270 return $x if $x->is_nan(); # error in _find_round_parameters?
2272 # no rounding at all, so must use fallback
2273 if (scalar @params == 0)
2275 # simulate old behaviour
2276 $params[0] = $self->div_scale(); # and round to it as accuracy
2277 $params[1] = undef; # disable P
2278 $scale = $params[0]+4; # at least four more for proper round
2279 $params[2] = $r[2]; # round mode by caller or undef
2280 $fallback = 1; # to clear a/p afterwards
2284 # the 4 below is empirical, and there might be cases where it is not
2286 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
2289 # when user set globals, they would interfere with our calculation, so
2290 # disable them and later re-enable them
2292 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
2293 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
2294 # we also need to disable any set A or P on $x (_find_round_parameters took
2295 # them already into account), since these would interfere, too
2296 delete $x->{_a}; delete $x->{_p};
2297 # need to disable $upgrade in BigInt, to avoid deep recursion
2298 local $Math::BigInt::upgrade = undef;
2300 my ($limit,$v,$u,$below,$factor,$next,$over);
2302 $u = $x->copy()->blog(undef,$scale)->bmul($y);
2303 $v = $self->bone(); # 1
2304 $factor = $self->new(2); # 2
2305 $x->bone(); # first term: 1
2307 $below = $v->copy();
2310 $limit = $self->new("1E-". ($scale-1));
2314 # we calculate the next term, and add it to the last
2315 # when the next term is below our limit, it won't affect the outcome
2316 # anymore, so we stop:
2317 $next = $over->copy()->bdiv($below,$scale);
2318 last if $next->bacmp($limit) <= 0;
2320 # calculate things for the next term
2321 $over *= $u; $below *= $factor; $factor->binc();
2323 last if $x->{sign} !~ /^[-+]$/;
2328 # shortcut to not run through _find_round_parameters again
2329 if (defined $params[0])
2331 $x->bround($params[0],$params[2]); # then round accordingly
2335 $x->bfround($params[1],$params[2]); # then round accordingly
2339 # clear a/p after round, since user did not request it
2340 delete $x->{_a}; delete $x->{_p};
2343 $$abr = $ab; $$pbr = $pb;
2349 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
2350 # compute power of two numbers, second arg is used as integer
2351 # modifies first argument
2354 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
2355 # objectify is costly, so avoid it
2356 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
2358 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
2361 return $x if $x->modify('bpow');
2363 return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
2364 return $x if $x->{sign} =~ /^[+-]inf$/;
2366 # cache the result of is_zero
2367 my $y_is_zero = $y->is_zero();
2368 return $x->bone() if $y_is_zero;
2369 return $x if $x->is_one() || $y->is_one();
2371 my $x_is_zero = $x->is_zero();
2372 return $x->_pow($y,$a,$p,$r) if !$x_is_zero && !$y->is_int(); # non-integer power
2374 my $y1 = $y->as_number()->{value}; # make MBI part
2377 if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e}))
2379 # if $x == -1 and odd/even y => +1/-1 because +-1 ^ (+-1) => +-1
2380 return $MBI->_is_odd($y1) ? $x : $x->babs(1);
2384 #return $x->bone() if $y_is_zero;
2385 return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0)
2386 # 0 ** -y => 1 / (0 ** y) => 1 / 0! (1 / 0 => +inf)
2391 $new_sign = $MBI->_is_odd($y1) ? '-' : '+' if $x->{sign} ne '+';
2393 # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster)
2394 $x->{_m} = $MBI->_pow( $x->{_m}, $y1);
2395 $x->{_e} = $MBI->_mul ($x->{_e}, $y1);
2397 $x->{sign} = $new_sign;
2399 if ($y->{sign} eq '-')
2401 # modify $x in place!
2402 my $z = $x->copy(); $x->bone();
2403 return scalar $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!)
2405 $x->round($a,$p,$r,$y);
2410 # takes a very large number to a very large exponent in a given very
2411 # large modulus, quickly, thanks to binary exponentation. Supports
2412 # negative exponents.
2413 my ($self,$num,$exp,$mod,@r) = objectify(3,@_);
2415 return $num if $num->modify('bmodpow');
2417 # check modulus for valid values
2418 return $num->bnan() if ($mod->{sign} ne '+' # NaN, - , -inf, +inf
2419 || $mod->is_zero());
2421 # check exponent for valid values
2422 if ($exp->{sign} =~ /\w/)
2424 # i.e., if it's NaN, +inf, or -inf...
2425 return $num->bnan();
2428 $num->bmodinv ($mod) if ($exp->{sign} eq '-');
2430 # check num for valid values (also NaN if there was no inverse but $exp < 0)
2431 return $num->bnan() if $num->{sign} !~ /^[+-]$/;
2433 # $mod is positive, sign on $exp is ignored, result also positive
2435 # XXX TODO: speed it up when all three numbers are integers
2436 $num->bpow($exp)->bmod($mod);
2439 ###############################################################################
2440 # trigonometric functions
2442 # helper function for bpi() and batan2(), calculates arcus tanges (1/x)
2446 # return a/b so that a/b approximates atan(1/x) to at least limit digits
2447 my ($self, $x, $limit) = @_;
2449 # Taylor: x^3 x^5 x^7 x^9
2450 # atan = x - --- + --- - --- + --- - ...
2454 # atan 1/x = - - ------- + ------- - ------- + ...
2455 # x x^3 * 3 x^5 * 5 x^7 * 7
2458 # atan 1/x = - - --------- + ---------- - ----------- + ...
2459 # 5 3 * 125 5 * 3125 7 * 78125
2461 # Subtraction/addition of a rational:
2464 # - +- - = ----------
2469 # a 1 a * d * c +- b
2470 # ----- +- ------------------ = ----------------
2473 # since b1 = b0 * (d-2) * c
2475 # a 1 a * d +- b / c
2476 # ----- +- ------------------ = ----------------
2483 # stop if length($u) > limit
2490 my $a = $MBI->_one();
2491 my $b = $MBI->_copy($x);
2493 my $x2 = $MBI->_mul( $MBI->_copy($x), $b); # x2 = x * x
2494 my $d = $MBI->_new( 3 ); # d = 3
2495 my $c = $MBI->_mul( $MBI->_copy($x), $x2); # c = x ^ 3
2496 my $two = $MBI->_new( 2 );
2498 # run the first step unconditionally
2499 my $u = $MBI->_mul( $MBI->_copy($d), $c);
2500 $a = $MBI->_mul($a, $u);
2501 $a = $MBI->_sub($a, $b);
2502 $b = $MBI->_mul($b, $u);
2503 $d = $MBI->_add($d, $two);
2504 $c = $MBI->_mul($c, $x2);
2506 # a is now a * (d-3) * c
2507 # b is now b * (d-2) * c
2509 # run the second step unconditionally
2510 $u = $MBI->_mul( $MBI->_copy($d), $c);
2511 $a = $MBI->_mul($a, $u);
2512 $a = $MBI->_add($a, $b);
2513 $b = $MBI->_mul($b, $u);
2514 $d = $MBI->_add($d, $two);
2515 $c = $MBI->_mul($c, $x2);
2517 # a is now a * (d-3) * (d-5) * c * c
2518 # b is now b * (d-2) * (d-4) * c * c
2520 # so we can remove c * c from both a and b to shorten the numbers involved:
2521 $a = $MBI->_div($a, $x2);
2522 $b = $MBI->_div($b, $x2);
2523 $a = $MBI->_div($a, $x2);
2524 $b = $MBI->_div($b, $x2);
2527 my $sign = 0; # 0 => -, 1 => +
2531 # if (($i++ % 100) == 0)
2533 # print "a=",$MBI->_str($a),"\n";
2534 # print "b=",$MBI->_str($b),"\n";
2536 # print "d=",$MBI->_str($d),"\n";
2537 # print "x2=",$MBI->_str($x2),"\n";
2538 # print "c=",$MBI->_str($c),"\n";
2540 my $u = $MBI->_mul( $MBI->_copy($d), $c);
2541 # use _alen() for libs like GMP where _len() would be O(N^2)
2542 last if $MBI->_alen($u) > $limit;
2543 my ($bc,$r) = $MBI->_div( $MBI->_copy($b), $c);
2544 if ($MBI->_is_zero($r))
2546 # b / c is an integer, so we can remove c from all terms
2547 # this happens almost every time:
2548 $a = $MBI->_mul($a, $d);
2549 $a = $MBI->_sub($a, $bc) if $sign == 0;
2550 $a = $MBI->_add($a, $bc) if $sign == 1;
2551 $b = $MBI->_mul($b, $d);
2555 # b / c is not an integer, so we keep c in the terms
2556 # this happens very rarely, for instance for x = 5, this happens only
2557 # at the following steps:
2558 # 1, 5, 14, 32, 72, 157, 340, ...
2559 $a = $MBI->_mul($a, $u);
2560 $a = $MBI->_sub($a, $b) if $sign == 0;
2561 $a = $MBI->_add($a, $b) if $sign == 1;
2562 $b = $MBI->_mul($b, $u);
2564 $d = $MBI->_add($d, $two);
2565 $c = $MBI->_mul($c, $x2);
2570 # print "Took $step steps for ", $MBI->_str($x),"\n";
2571 # print "a=",$MBI->_str($a),"\n"; print "b=",$MBI->_str($b),"\n";
2572 # return a/b so that a/b approximates atan(1/x)
2585 # called like Math::BigFloat::bpi(10);
2586 $n = $self; $self = $class;
2587 # called like Math::BigFloat->bpi();
2588 $n = undef if $n eq 'Math::BigFloat';
2590 $self = ref($self) if ref($self);
2591 $n = 40 if !defined $n || $n < 1;
2593 # after 黃見利 (Hwang Chien-Lih) (1997)
2594 # pi/4 = 183 * atan(1/239) + 32 * atan(1/1023) – 68 * atan(1/5832)
2595 # + 12 * atan(1/110443) - 12 * atan(1/4841182) - 100 * atan(1/6826318)
2597 # a few more to prevent rounding errors
2600 my ($a,$b) = $self->_atan_inv( $MBI->_new(239),$n);
2601 my ($c,$d) = $self->_atan_inv( $MBI->_new(1023),$n);
2602 my ($e,$f) = $self->_atan_inv( $MBI->_new(5832),$n);
2603 my ($g,$h) = $self->_atan_inv( $MBI->_new(110443),$n);
2604 my ($i,$j) = $self->_atan_inv( $MBI->_new(4841182),$n);
2605 my ($k,$l) = $self->_atan_inv( $MBI->_new(6826318),$n);
2607 $MBI->_mul($a, $MBI->_new(732));
2608 $MBI->_mul($c, $MBI->_new(128));
2609 $MBI->_mul($e, $MBI->_new(272));
2610 $MBI->_mul($g, $MBI->_new(48));
2611 $MBI->_mul($i, $MBI->_new(48));
2612 $MBI->_mul($k, $MBI->_new(400));
2614 my $x = $self->bone(); $x->{_m} = $a; my $x_d = $self->bone(); $x_d->{_m} = $b;
2615 my $y = $self->bone(); $y->{_m} = $c; my $y_d = $self->bone(); $y_d->{_m} = $d;
2616 my $z = $self->bone(); $z->{_m} = $e; my $z_d = $self->bone(); $z_d->{_m} = $f;
2617 my $u = $self->bone(); $u->{_m} = $g; my $u_d = $self->bone(); $u_d->{_m} = $h;
2618 my $v = $self->bone(); $v->{_m} = $i; my $v_d = $self->bone(); $v_d->{_m} = $j;
2619 my $w = $self->bone(); $w->{_m} = $k; my $w_d = $self->bone(); $w_d->{_m} = $l;
2627 delete $x->{a}; delete $y->{a}; delete $z->{a};
2628 delete $u->{a}; delete $v->{a}; delete $w->{a};
2629 $x->badd($y)->bsub($z)->badd($u)->bsub($v)->bsub($w);
2636 # Calculate a cosinus of x.
2637 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2639 # Taylor: x^2 x^4 x^6 x^8
2640 # cos = 1 - --- + --- - --- + --- ...
2643 # we need to limit the accuracy to protect against overflow
2645 my ($scale,@params);
2646 ($x,@params) = $x->_find_round_parameters(@r);
2648 # constant object or error in _find_round_parameters?
2649 return $x if $x->modify('bcos') || $x->is_nan();
2651 return $x->bone(@r) if $x->is_zero();
2653 # no rounding at all, so must use fallback
2654 if (scalar @params == 0)
2656 # simulate old behaviour
2657 $params[0] = $self->div_scale(); # and round to it as accuracy
2658 $params[1] = undef; # disable P
2659 $scale = $params[0]+4; # at least four more for proper round
2660 $params[2] = $r[2]; # round mode by caller or undef
2661 $fallback = 1; # to clear a/p afterwards
2665 # the 4 below is empirical, and there might be cases where it is not
2667 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
2670 # when user set globals, they would interfere with our calculation, so
2671 # disable them and later re-enable them
2673 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
2674 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
2675 # we also need to disable any set A or P on $x (_find_round_parameters took
2676 # them already into account), since these would interfere, too
2677 delete $x->{_a}; delete $x->{_p};
2678 # need to disable $upgrade in BigInt, to avoid deep recursion
2679 local $Math::BigInt::upgrade = undef;
2682 my $over = $x * $x; # X ^ 2
2683 my $x2 = $over->copy(); # X ^ 2; difference between terms
2684 my $sign = 1; # start with -=
2685 my $below = $self->new(2); my $factorial = $self->new(3);
2686 $x->bone(); delete $x->{a}; delete $x->{p};
2688 my $limit = $self->new("1E-". ($scale-1));
2692 # we calculate the next term, and add it to the last
2693 # when the next term is below our limit, it won't affect the outcome
2694 # anymore, so we stop:
2695 my $next = $over->copy()->bdiv($below,$scale);
2696 last if $next->bacmp($limit) <= 0;
2706 $sign = 1-$sign; # alternate
2707 # calculate things for the next term
2708 $over->bmul($x2); # $x*$x
2709 $below->bmul($factorial); $factorial->binc(); # n*(n+1)
2710 $below->bmul($factorial); $factorial->binc(); # n*(n+1)
2713 # shortcut to not run through _find_round_parameters again
2714 if (defined $params[0])
2716 $x->bround($params[0],$params[2]); # then round accordingly
2720 $x->bfround($params[1],$params[2]); # then round accordingly
2724 # clear a/p after round, since user did not request it
2725 delete $x->{_a}; delete $x->{_p};
2728 $$abr = $ab; $$pbr = $pb;
2734 # Calculate a sinus of x.
2735 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2737 # taylor: x^3 x^5 x^7 x^9
2738 # sin = x - --- + --- - --- + --- ...
2741 # we need to limit the accuracy to protect against overflow
2743 my ($scale,@params);
2744 ($x,@params) = $x->_find_round_parameters(@r);
2746 # constant object or error in _find_round_parameters?
2747 return $x if $x->modify('bsin') || $x->is_nan();
2749 return $x->bzero(@r) if $x->is_zero();
2751 # no rounding at all, so must use fallback
2752 if (scalar @params == 0)
2754 # simulate old behaviour
2755 $params[0] = $self->div_scale(); # and round to it as accuracy
2756 $params[1] = undef; # disable P
2757 $scale = $params[0]+4; # at least four more for proper round
2758 $params[2] = $r[2]; # round mode by caller or undef
2759 $fallback = 1; # to clear a/p afterwards
2763 # the 4 below is empirical, and there might be cases where it is not
2765 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
2768 # when user set globals, they would interfere with our calculation, so
2769 # disable them and later re-enable them
2771 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
2772 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
2773 # we also need to disable any set A or P on $x (_find_round_parameters took
2774 # them already into account), since these would interfere, too
2775 delete $x->{_a}; delete $x->{_p};
2776 # need to disable $upgrade in BigInt, to avoid deep recursion
2777 local $Math::BigInt::upgrade = undef;
2780 my $over = $x * $x; # X ^ 2
2781 my $x2 = $over->copy(); # X ^ 2; difference between terms
2782 $over->bmul($x); # X ^ 3 as starting value
2783 my $sign = 1; # start with -=
2784 my $below = $self->new(6); my $factorial = $self->new(4);
2785 delete $x->{a}; delete $x->{p};
2787 my $limit = $self->new("1E-". ($scale-1));
2791 # we calculate the next term, and add it to the last
2792 # when the next term is below our limit, it won't affect the outcome
2793 # anymore, so we stop:
2794 my $next = $over->copy()->bdiv($below,$scale);
2795 last if $next->bacmp($limit) <= 0;
2805 $sign = 1-$sign; # alternate
2806 # calculate things for the next term
2807 $over->bmul($x2); # $x*$x
2808 $below->bmul($factorial); $factorial->binc(); # n*(n+1)
2809 $below->bmul($factorial); $factorial->binc(); # n*(n+1)
2812 # shortcut to not run through _find_round_parameters again
2813 if (defined $params[0])
2815 $x->bround($params[0],$params[2]); # then round accordingly
2819 $x->bfround($params[1],$params[2]); # then round accordingly
2823 # clear a/p after round, since user did not request it
2824 delete $x->{_a}; delete $x->{_p};
2827 $$abr = $ab; $$pbr = $pb;
2833 # calculate arcus tangens of ($y/$x)
2836 my ($self,$y,$x,@r) = (ref($_[0]),@_);
2837 # objectify is costly, so avoid it
2838 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
2840 ($self,$y,$x,@r) = objectify(2,@_);
2843 return $y if $y->modify('batan2');
2845 return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan);
2847 return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade;
2852 return $y->bzero(@r) if $y->is_zero() && $x->{sign} eq '+';
2859 my $pi = $self->bpi(@r);
2860 # modify $x in place
2861 $y->{_m} = $pi->{_m};
2862 $y->{_e} = $pi->{_e};
2863 $y->{_es} = $pi->{_es};
2869 # +y 0 result is PI/2
2870 # -y 0 result is -PI/2
2871 if ($y->is_inf() || $x->is_zero())
2874 my $pi = $self->bpi(@r);
2875 # modify $x in place
2876 $y->{_m} = $pi->{_m};
2877 $y->{_e} = $pi->{_e};
2878 $y->{_es} = $pi->{_es};
2879 # -y => -PI/2, +y => PI/2
2880 $y->{sign} = substr($y->{sign},0,1); # +inf => +
2881 $MBI->_div($y->{_m}, $MBI->_new(2));
2885 # we need to limit the accuracy to protect against overflow
2887 my ($scale,@params);
2888 ($y,@params) = $y->_find_round_parameters(@r);
2890 # error in _find_round_parameters?
2891 return $y if $y->is_nan();
2893 # no rounding at all, so must use fallback
2894 if (scalar @params == 0)
2896 # simulate old behaviour
2897 $params[0] = $self->div_scale(); # and round to it as accuracy
2898 $params[1] = undef; # disable P
2899 $scale = $params[0]+4; # at least four more for proper round
2900 $params[2] = $r[2]; # round mode by caller or undef
2901 $fallback = 1; # to clear a/p afterwards
2905 # the 4 below is empirical, and there might be cases where it is not
2907 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
2910 # inlined is_one() && is_one('-')
2911 if ($MBI->_is_one($y->{_m}) && $MBI->_is_zero($y->{_e}))
2913 # shortcut: 1 1 result is PI/4
2914 # inlined is_one() && is_one('-')
2915 if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e}))
2918 my $pi_4 = $self->bpi( $scale - 3);
2919 # modify $x in place
2920 $y->{_m} = $pi_4->{_m};
2921 $y->{_e} = $pi_4->{_e};
2922 $y->{_es} = $pi_4->{_es};
2927 $y->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
2928 $MBI->_div($y->{_m}, $MBI->_new(4));
2931 # shortcut: 1 int(X) result is _atan_inv(X)
2934 if ($x->{_es} eq '+')
2936 my $x1 = $MBI->_copy($x->{_m});
2937 $MBI->_lsft($x1, $x->{_e},10) unless $MBI->_is_zero($x->{_e});
2939 my ($a,$b) = $self->_atan_inv($x1, $scale);
2940 my $y_sign = $y->{sign};
2942 $y->bone(); $y->{_m} = $a; my $y_d = $self->bone(); $y_d->{_m} = $b;
2944 $y->{sign} = $y_sign;
2949 # handle all other cases
2954 # -x -y -PI/2 to -PI
2956 my $y_sign = $y->{sign};
2959 $y->bdiv($x, $scale) unless $x->is_one();
2963 $y->{sign} = $y_sign;
2970 # Calculate a arcus tangens of x.
2974 # taylor: x^3 x^5 x^7 x^9
2975 # atan = x - --- + --- - --- + --- ...
2978 # we need to limit the accuracy to protect against overflow
2980 my ($scale,@params);
2981 ($x,@params) = $x->_find_round_parameters(@r);
2983 # constant object or error in _find_round_parameters?
2984 return $x if $x->modify('batan') || $x->is_nan();
2986 if ($x->{sign} =~ /^[+-]inf\z/)
2988 # +inf result is PI/2
2989 # -inf result is -PI/2
2991 my $pi = $self->bpi(@r);
2992 # modify $x in place
2993 $x->{_m} = $pi->{_m};
2994 $x->{_e} = $pi->{_e};
2995 $x->{_es} = $pi->{_es};
2996 # -y => -PI/2, +y => PI/2
2997 $x->{sign} = substr($x->{sign},0,1); # +inf => +
2998 $MBI->_div($x->{_m}, $MBI->_new(2));
3002 return $x->bzero(@r) if $x->is_zero();
3004 # no rounding at all, so must use fallback
3005 if (scalar @params == 0)
3007 # simulate old behaviour
3008 $params[0] = $self->div_scale(); # and round to it as accuracy
3009 $params[1] = undef; # disable P
3010 $scale = $params[0]+4; # at least four more for proper round
3011 $params[2] = $r[2]; # round mode by caller or undef
3012 $fallback = 1; # to clear a/p afterwards
3016 # the 4 below is empirical, and there might be cases where it is not
3018 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
3022 # inlined is_one() && is_one('-')
3023 if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e}))
3025 my $pi = $self->bpi($scale - 3);
3026 # modify $x in place
3027 $x->{_m} = $pi->{_m};
3028 $x->{_e} = $pi->{_e};
3029 $x->{_es} = $pi->{_es};
3030 # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4)
3031 $MBI->_div($x->{_m}, $MBI->_new(4));
3035 # This series is only valid if -1 < x < 1, so for other x we need to
3036 # to calculate PI/ - atan(1/x):
3037 my $one = $MBI->_new(1);
3039 if ($x->{_es} eq '+' && ($MBI->_acmp($x->{_m},$one) >= 0))
3042 $pi = $self->bpi($scale - 3);
3043 $MBI->_div($pi->{_m}, $MBI->_new(2));
3045 my $x_copy = $x->copy();
3046 # modify $x in place
3047 $x->bone(); $x->bdiv($x_copy,$scale);
3050 # when user set globals, they would interfere with our calculation, so
3051 # disable them and later re-enable them
3053 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
3054 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
3055 # we also need to disable any set A or P on $x (_find_round_parameters took
3056 # them already into account), since these would interfere, too
3057 delete $x->{_a}; delete $x->{_p};
3058 # need to disable $upgrade in BigInt, to avoid deep recursion
3059 local $Math::BigInt::upgrade = undef;
3062 my $over = $x * $x; # X ^ 2
3063 my $x2 = $over->copy(); # X ^ 2; difference between terms
3064 $over->bmul($x); # X ^ 3 as starting value
3065 my $sign = 1; # start with -=
3066 my $below = $self->new(3);
3067 my $two = $self->new(2);
3068 delete $x->{a}; delete $x->{p};
3070 my $limit = $self->new("1E-". ($scale-1));
3074 # we calculate the next term, and add it to the last
3075 # when the next term is below our limit, it won't affect the outcome
3076 # anymore, so we stop:
3077 my $next = $over->copy()->bdiv($below,$scale);
3078 last if $next->bacmp($limit) <= 0;
3088 $sign = 1-$sign; # alternate
3089 # calculate things for the next term
3090 $over->bmul($x2); # $x*$x
3091 $below->badd($two); # n += 2
3096 my $x_copy = $x->copy();
3097 # modify $x in place
3098 $x->{_m} = $pi->{_m};
3099 $x->{_e} = $pi->{_e};
3100 $x->{_es} = $pi->{_es};
3105 # shortcut to not run through _find_round_parameters again
3106 if (defined $params[0])
3108 $x->bround($params[0],$params[2]); # then round accordingly
3112 $x->bfround($params[1],$params[2]); # then round accordingly
3116 # clear a/p after round, since user did not request it
3117 delete $x->{_a}; delete $x->{_p};
3120 $$abr = $ab; $$pbr = $pb;
3124 ###############################################################################
3125 # rounding functions
3129 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
3130 # $n == 0 means round to integer
3131 # expects and returns normalized numbers!
3132 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
3134 my ($scale,$mode) = $x->_scale_p(@_);
3135 return $x if !defined $scale || $x->modify('bfround'); # no-op
3137 # never round a 0, +-inf, NaN
3140 $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2
3143 return $x if $x->{sign} !~ /^[+-]$/;
3145 # don't round if x already has lower precision
3146 return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p});
3148 $x->{_p} = $scale; # remember round in any case
3149 delete $x->{_a}; # and clear A
3152 # round right from the '.'
3154 return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round
3156 $scale = -$scale; # positive for simplicity
3157 my $len = $MBI->_len($x->{_m}); # length of mantissa
3159 # the following poses a restriction on _e, but if _e is bigger than a
3160 # scalar, you got other problems (memory etc) anyway
3161 my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot
3162 my $zad = 0; # zeros after dot
3163 $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style
3165 # p rint "scale $scale dad $dad zad $zad len $len\n";
3166 # number bsstr len zad dad
3167 # 0.123 123e-3 3 0 3
3168 # 0.0123 123e-4 3 1 4
3171 # 1.2345 12345e-4 5 0 4
3173 # do not round after/right of the $dad
3174 return $x if $scale > $dad; # 0.123, scale >= 3 => exit
3176 # round to zero if rounding inside the $zad, but not for last zero like:
3177 # 0.0065, scale -2, round last '0' with following '65' (scale == zad case)
3178 return $x->bzero() if $scale < $zad;
3179 if ($scale == $zad) # for 0.006, scale -3 and trunc
3185 # adjust round-point to be inside mantissa
3188 $scale = $scale-$zad;
3192 my $dbd = $len - $dad; $dbd = 0 if $dbd < 0; # digits before dot
3193 $scale = $dbd+$scale;
3199 # round left from the '.'
3201 # 123 => 100 means length(123) = 3 - $scale (2) => 1
3203 my $dbt = $MBI->_len($x->{_m});
3205 my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e}));
3206 # should be the same, so treat it as this
3207 $scale = 1 if $scale == 0;
3208 # shortcut if already integer
3209 return $x if $scale == 1 && $dbt <= $dbd;
3210 # maximum digits before dot
3215 # not enough digits before dot, so round to zero
3218 elsif ( $scale == $dbd )
3225 $scale = $dbd - $scale;
3228 # pass sign to bround for rounding modes '+inf' and '-inf'
3229 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
3230 $m->bround($scale,$mode);
3231 $x->{_m} = $m->{value}; # get our mantissa back
3237 # accuracy: preserve $N digits, and overwrite the rest with 0's
3238 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
3240 if (($_[0] || 0) < 0)
3242 require Carp; Carp::croak ('bround() needs positive accuracy');
3245 my ($scale,$mode) = $x->_scale_a(@_);
3246 return $x if !defined $scale || $x->modify('bround'); # no-op
3248 # scale is now either $x->{_a}, $accuracy, or the user parameter
3249 # test whether $x already has lower accuracy, do nothing in this case
3250 # but do round if the accuracy is the same, since a math operation might
3251 # want to round a number with A=5 to 5 digits afterwards again
3252 return $x if defined $x->{_a} && $x->{_a} < $scale;
3254 # scale < 0 makes no sense
3255 # scale == 0 => keep all digits
3256 # never round a +-inf, NaN
3257 return $x if ($scale <= 0) || $x->{sign} !~ /^[+-]$/;
3259 # 1: never round a 0
3260 # 2: if we should keep more digits than the mantissa has, do nothing
3261 if ($x->is_zero() || $MBI->_len($x->{_m}) <= $scale)
3263 $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale;
3267 # pass sign to bround for '+inf' and '-inf' rounding modes
3268 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
3270 $m->bround($scale,$mode); # round mantissa
3271 $x->{_m} = $m->{value}; # get our mantissa back
3272 $x->{_a} = $scale; # remember rounding
3273 delete $x->{_p}; # and clear P
3274 $x->bnorm(); # del trailing zeros gen. by bround()
3279 # return integer less or equal then $x
3280 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
3282 return $x if $x->modify('bfloor');
3284 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
3286 # if $x has digits after dot
3287 if ($x->{_es} eq '-')
3289 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
3290 $x->{_e} = $MBI->_zero(); # trunc/norm
3291 $x->{_es} = '+'; # abs e
3292 $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative
3294 $x->round($a,$p,$r);
3299 # return integer greater or equal then $x
3300 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
3302 return $x if $x->modify('bceil');
3303 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
3305 # if $x has digits after dot
3306 if ($x->{_es} eq '-')
3308 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
3309 $x->{_e} = $MBI->_zero(); # trunc/norm
3310 $x->{_es} = '+'; # abs e
3311 $MBI->_inc($x->{_m}) if $x->{sign} eq '+'; # increment if positive
3313 $x->round($a,$p,$r);
3318 # shift right by $y (divide by power of $n)
3321 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
3322 # objectify is costly, so avoid it
3323 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
3325 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
3328 return $x if $x->modify('brsft');
3329 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
3331 $n = 2 if !defined $n; $n = $self->new($n);
3334 return $x->blsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/;
3336 # the following call to bdiv() will return either quo or (quo,reminder):
3337 $x->bdiv($n->bpow($y),$a,$p,$r,$y);
3342 # shift left by $y (multiply by power of $n)
3345 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
3346 # objectify is costly, so avoid it
3347 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
3349 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
3352 return $x if $x->modify('blsft');
3353 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
3355 $n = 2 if !defined $n; $n = $self->new($n);
3358 return $x->brsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/;
3360 $x->bmul($n->bpow($y),$a,$p,$r,$y);
3363 ###############################################################################
3367 # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub
3372 # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx()
3373 # or falling back to MBI::bxxx()
3374 my $name = $AUTOLOAD;
3376 $name =~ s/(.*):://; # split package
3377 my $c = $1 || $class;
3379 $c->import() if $IMPORT == 0;
3380 if (!_method_alias($name))
3384 # delayed load of Carp and avoid recursion
3386 Carp::croak ("$c: Can't call a method without name");
3388 if (!_method_hand_up($name))
3390 # delayed load of Carp and avoid recursion
3392 Carp::croak ("Can't call $c\-\>$name, not a valid method");
3394 # try one level up, but subst. bxxx() for fxxx() since MBI only got bxxx()
3396 return &{"Math::BigInt"."::$name"}(@_);
3398 my $bname = $name; $bname =~ s/^f/b/;
3406 # return a copy of the exponent
3407 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3409 if ($x->{sign} !~ /^[+-]$/)
3411 my $s = $x->{sign}; $s =~ s/^[+-]//;
3412 return Math::BigInt->new($s); # -inf, +inf => +inf
3414 Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e}));
3419 # return a copy of the mantissa
3420 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3422 if ($x->{sign} !~ /^[+-]$/)
3424 my $s = $x->{sign}; $s =~ s/^[+]//;
3425 return Math::BigInt->new($s); # -inf, +inf => +inf
3427 my $m = Math::BigInt->new( $MBI->_str($x->{_m}));
3428 $m->bneg() if $x->{sign} eq '-';
3435 # return a copy of both the exponent and the mantissa
3436 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3438 if ($x->{sign} !~ /^[+-]$/)
3440 my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//;
3441 return ($self->new($s),$self->new($se)); # +inf => inf and -inf,+inf => inf
3443 my $m = Math::BigInt->bzero();
3444 $m->{value} = $MBI->_copy($x->{_m});
3445 $m->bneg() if $x->{sign} eq '-';
3446 ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) ));
3449 ##############################################################################
3450 # private stuff (internal use only)
3456 my $lib = ''; my @a;
3457 my $lib_kind = 'try';
3459 for ( my $i = 0; $i < $l ; $i++)
3461 if ( $_[$i] eq ':constant' )
3463 # This causes overlord er load to step in. 'binary' and 'integer'
3464 # are handled by BigInt.
3465 overload::constant float => sub { $self->new(shift); };
3467 elsif ($_[$i] eq 'upgrade')
3469 # this causes upgrading
3470 $upgrade = $_[$i+1]; # or undef to disable
3473 elsif ($_[$i] eq 'downgrade')
3475 # this causes downgrading
3476 $downgrade = $_[$i+1]; # or undef to disable
3479 elsif ($_[$i] =~ /^(lib|try|only)\z/)
3481 # alternative library
3482 $lib = $_[$i+1] || ''; # default Calc
3483 $lib_kind = $1; # lib, try or only
3486 elsif ($_[$i] eq 'with')
3488 # alternative class for our private parts()
3489 # XXX: no longer supported
3490 # $MBI = $_[$i+1] || 'Math::BigInt';
3499 $lib =~ tr/a-zA-Z0-9,://cd; # restrict to sane characters
3500 # let use Math::BigInt lib => 'GMP'; use Math::BigFloat; still work
3501 my $mbilib = eval { Math::BigInt->config()->{lib} };
3502 if ((defined $mbilib) && ($MBI eq 'Math::BigInt::Calc'))
3504 # MBI already loaded
3505 Math::BigInt->import( $lib_kind, "$lib,$mbilib", 'objectify');
3509 # MBI not loaded, or with ne "Math::BigInt::Calc"
3510 $lib .= ",$mbilib" if defined $mbilib;
3511 $lib =~ s/^,//; # don't leave empty
3513 # replacement library can handle lib statement, but also could ignore it
3515 # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is
3516 # used in the same script, or eval inside import(). So we require MBI:
3517 require Math::BigInt;
3518 Math::BigInt->import( $lib_kind => $lib, 'objectify' );
3522 require Carp; Carp::croak ("Couldn't load $lib: $! $@");
3524 # find out which one was actually loaded
3525 $MBI = Math::BigInt->config()->{lib};
3527 # register us with MBI to get notified of future lib changes
3528 Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } );
3530 $self->export_to_level(1,$self,@a); # export wanted functions
3535 # adjust m and e so that m is smallest possible
3536 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
3538 return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc
3540 my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros
3543 my $z = $MBI->_new($zeros);
3544 $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10);
3545 if ($x->{_es} eq '-')
3547 if ($MBI->_acmp($x->{_e},$z) >= 0)
3549 $x->{_e} = $MBI->_sub ($x->{_e}, $z);
3550 $x->{_es} = '+' if $MBI->_is_zero($x->{_e});
3554 $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e});
3560 $x->{_e} = $MBI->_add ($x->{_e}, $z);
3565 # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing
3566 # zeros). So, for something like 0Ey, set y to 1, and -0 => +0
3567 $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one()
3568 if $MBI->_is_zero($x->{_m});
3571 $x; # MBI bnorm is no-op, so dont call it
3574 ##############################################################################
3578 # return number as hexadecimal string (only for integers defined)
3579 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3581 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
3582 return '0x0' if $x->is_zero();
3584 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
3586 my $z = $MBI->_copy($x->{_m});
3587 if (! $MBI->_is_zero($x->{_e})) # > 0
3589 $MBI->_lsft( $z, $x->{_e},10);
3591 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
3597 # return number as binary digit string (only for integers defined)
3598 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3600 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
3601 return '0b0' if $x->is_zero();
3603 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
3605 my $z = $MBI->_copy($x->{_m});
3606 if (! $MBI->_is_zero($x->{_e})) # > 0
3608 $MBI->_lsft( $z, $x->{_e},10);
3610 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
3616 # return number as octal digit string (only for integers defined)
3617 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3619 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
3620 return '0' if $x->is_zero();
3622 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
3624 my $z = $MBI->_copy($x->{_m});
3625 if (! $MBI->_is_zero($x->{_e})) # > 0
3627 $MBI->_lsft( $z, $x->{_e},10);
3629 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
3635 # return copy as a bigint representation of this BigFloat number
3636 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
3638 return $x if $x->modify('as_number');
3640 my $z = $MBI->_copy($x->{_m});
3641 if ($x->{_es} eq '-') # < 0
3643 $MBI->_rsft( $z, $x->{_e},10);
3645 elsif (! $MBI->_is_zero($x->{_e})) # > 0
3647 $MBI->_lsft( $z, $x->{_e},10);
3649 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
3656 my $class = ref($x) || $x;
3657 $x = $class->new(shift) unless ref($x);
3659 return 1 if $MBI->_is_zero($x->{_m});
3661 my $len = $MBI->_len($x->{_m});
3662 $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+';
3666 $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-';
3677 Math::BigFloat - Arbitrary size floating point math package
3684 my $x = Math::BigFloat->new($str); # defaults to 0
3685 my $y = $x->copy(); # make a true copy
3686 my $nan = Math::BigFloat->bnan(); # create a NotANumber
3687 my $zero = Math::BigFloat->bzero(); # create a +0
3688 my $inf = Math::BigFloat->binf(); # create a +inf
3689 my $inf = Math::BigFloat->binf('-'); # create a -inf
3690 my $one = Math::BigFloat->bone(); # create a +1
3691 my $mone = Math::BigFloat->bone('-'); # create a -1
3693 my $pi = Math::BigFloat->bpi(100); # PI to 100 digits
3695 # the following examples compute their result to 100 digits accuracy:
3696 my $cos = Math::BigFloat->new(1)->bcos(100); # cosinus(1)
3697 my $sin = Math::BigFloat->new(1)->bsin(100); # sinus(1)
3698 my $atan = Math::BigFloat->new(1)->batan(100); # arcus tangens(1)
3700 my $atan2 = Math::BigFloat->new( 1 )->batan2( 1 ,100); # batan(1)
3701 my $atan2 = Math::BigFloat->new( 1 )->batan2( 8 ,100); # batan(1/8)
3702 my $atan2 = Math::BigFloat->new( -2 )->batan2( 1 ,100); # batan(-2)
3705 $x->is_zero(); # true if arg is +0
3706 $x->is_nan(); # true if arg is NaN
3707 $x->is_one(); # true if arg is +1
3708 $x->is_one('-'); # true if arg is -1
3709 $x->is_odd(); # true if odd, false for even
3710 $x->is_even(); # true if even, false for odd
3711 $x->is_pos(); # true if >= 0
3712 $x->is_neg(); # true if < 0
3713 $x->is_inf(sign); # true if +inf, or -inf (default is '+')
3715 $x->bcmp($y); # compare numbers (undef,<0,=0,>0)
3716 $x->bacmp($y); # compare absolutely (undef,<0,=0,>0)
3717 $x->sign(); # return the sign, either +,- or NaN
3718 $x->digit($n); # return the nth digit, counting from right
3719 $x->digit(-$n); # return the nth digit, counting from left
3721 # The following all modify their first argument. If you want to preserve
3722 # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is
3723 # necessary when mixing $a = $b assignments with non-overloaded math.
3726 $x->bzero(); # set $i to 0
3727 $x->bnan(); # set $i to NaN
3728 $x->bone(); # set $x to +1
3729 $x->bone('-'); # set $x to -1
3730 $x->binf(); # set $x to inf
3731 $x->binf('-'); # set $x to -inf
3733 $x->bneg(); # negation
3734 $x->babs(); # absolute value
3735 $x->bnorm(); # normalize (no-op)
3736 $x->bnot(); # two's complement (bit wise not)
3737 $x->binc(); # increment x by 1
3738 $x->bdec(); # decrement x by 1
3740 $x->badd($y); # addition (add $y to $x)
3741 $x->bsub($y); # subtraction (subtract $y from $x)
3742 $x->bmul($y); # multiplication (multiply $x by $y)
3743 $x->bdiv($y); # divide, set $x to quotient
3744 # return (quo,rem) or quo if scalar
3746 $x->bmod($y); # modulus ($x % $y)
3747 $x->bpow($y); # power of arguments ($x ** $y)
3748 $x->bmodpow($exp,$mod); # modular exponentation (($num**$exp) % $mod))
3749 $x->blsft($y, $n); # left shift by $y places in base $n
3750 $x->brsft($y, $n); # right shift by $y places in base $n
3751 # returns (quo,rem) or quo if in scalar context
3753 $x->blog(); # logarithm of $x to base e (Euler's number)
3754 $x->blog($base); # logarithm of $x to base $base (f.i. 2)
3755 $x->bexp(); # calculate e ** $x where e is Euler's number
3757 $x->band($y); # bit-wise and
3758 $x->bior($y); # bit-wise inclusive or
3759 $x->bxor($y); # bit-wise exclusive or
3760 $x->bnot(); # bit-wise not (two's complement)
3762 $x->bsqrt(); # calculate square-root
3763 $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root)
3764 $x->bfac(); # factorial of $x (1*2*3*4*..$x)
3766 $x->bround($N); # accuracy: preserve $N digits
3767 $x->bfround($N); # precision: round to the $Nth digit
3769 $x->bfloor(); # return integer less or equal than $x
3770 $x->bceil(); # return integer greater or equal than $x
3772 # The following do not modify their arguments:
3774 bgcd(@values); # greatest common divisor
3775 blcm(@values); # lowest common multiplicator
3777 $x->bstr(); # return string
3778 $x->bsstr(); # return string in scientific notation
3780 $x->as_int(); # return $x as BigInt
3781 $x->exponent(); # return exponent as BigInt
3782 $x->mantissa(); # return mantissa as BigInt
3783 $x->parts(); # return (mantissa,exponent) as BigInt
3785 $x->length(); # number of digits (w/o sign and '.')
3786 ($l,$f) = $x->length(); # number of digits, and length of fraction
3788 $x->precision(); # return P of $x (or global, if P of $x undef)
3789 $x->precision($n); # set P of $x to $n
3790 $x->accuracy(); # return A of $x (or global, if A of $x undef)
3791 $x->accuracy($n); # set A $x to $n
3793 # these get/set the appropriate global value for all BigFloat objects
3794 Math::BigFloat->precision(); # Precision
3795 Math::BigFloat->accuracy(); # Accuracy
3796 Math::BigFloat->round_mode(); # rounding mode
3800 All operators (including basic math operations) are overloaded if you
3801 declare your big floating point numbers as
3803 $i = new Math::BigFloat '12_3.456_789_123_456_789E-2';
3805 Operations with overloaded operators preserve the arguments, which is
3806 exactly what you expect.
3808 =head2 Canonical notation
3810 Input to these routines are either BigFloat objects, or strings of the
3811 following four forms:
3825 C</^[+-]\d+E[+-]?\d+$/>
3829 C</^[+-]\d*\.\d+E[+-]?\d+$/>
3833 all with optional leading and trailing zeros and/or spaces. Additionally,
3834 numbers are allowed to have an underscore between any two digits.
3836 Empty strings as well as other illegal numbers results in 'NaN'.
3838 bnorm() on a BigFloat object is now effectively a no-op, since the numbers
3839 are always stored in normalized form. On a string, it creates a BigFloat
3844 Output values are BigFloat objects (normalized), except for bstr() and bsstr().
3846 The string output will always have leading and trailing zeros stripped and drop
3847 a plus sign. C<bstr()> will give you always the form with a decimal point,
3848 while C<bsstr()> (s for scientific) gives you the scientific notation.
3850 Input bstr() bsstr()
3852 ' -123 123 123' '-123123123' '-123123123E0'
3853 '00.0123' '0.0123' '123E-4'
3854 '123.45E-2' '1.2345' '12345E-4'
3855 '10E+3' '10000' '1E4'
3857 Some routines (C<is_odd()>, C<is_even()>, C<is_zero()>, C<is_one()>,
3858 C<is_nan()>) return true or false, while others (C<bcmp()>, C<bacmp()>)
3859 return either undef, <0, 0 or >0 and are suited for sort.
3861 Actual math is done by using the class defined with C<with => Class;> (which
3862 defaults to BigInts) to represent the mantissa and exponent.
3864 The sign C</^[+-]$/> is stored separately. The string 'NaN' is used to
3865 represent the result when input arguments are not numbers, as well as
3866 the result of dividing by zero.
3868 =head2 C<mantissa()>, C<exponent()> and C<parts()>
3870 C<mantissa()> and C<exponent()> return the said parts of the BigFloat
3871 as BigInts such that:
3873 $m = $x->mantissa();
3874 $e = $x->exponent();
3875 $y = $m * ( 10 ** $e );
3876 print "ok\n" if $x == $y;
3878 C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them.
3880 A zero is represented and returned as C<0E1>, B<not> C<0E0> (after Knuth).
3882 Currently the mantissa is reduced as much as possible, favouring higher
3883 exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0).
3884 This might change in the future, so do not depend on it.
3886 =head2 Accuracy vs. Precision
3888 See also: L<Rounding|Rounding>.
3890 Math::BigFloat supports both precision (rounding to a certain place before or
3891 after the dot) and accuracy (rounding to a certain number of digits). For a
3892 full documentation, examples and tips on these topics please see the large
3893 section about rounding in L<Math::BigInt>.
3895 Since things like C<sqrt(2)> or C<1 / 3> must presented with a limited
3896 accuracy lest a operation consumes all resources, each operation produces
3897 no more than the requested number of digits.
3899 If there is no gloabl precision or accuracy set, B<and> the operation in
3900 question was not called with a requested precision or accuracy, B<and> the
3901 input $x has no accuracy or precision set, then a fallback parameter will
3902 be used. For historical reasons, it is called C<div_scale> and can be accessed
3905 $d = Math::BigFloat->div_scale(); # query
3906 Math::BigFloat->div_scale($n); # set to $n digits
3908 The default value for C<div_scale> is 40.
3910 In case the result of one operation has more digits than specified,
3911 it is rounded. The rounding mode taken is either the default mode, or the one
3912 supplied to the operation after the I<scale>:
3914 $x = Math::BigFloat->new(2);
3915 Math::BigFloat->accuracy(5); # 5 digits max
3916 $y = $x->copy()->bdiv(3); # will give 0.66667
3917 $y = $x->copy()->bdiv(3,6); # will give 0.666667
3918 $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667
3919 Math::BigFloat->round_mode('zero');
3920 $y = $x->copy()->bdiv(3,6); # will also give 0.666667
3922 Note that C<< Math::BigFloat->accuracy() >> and C<< Math::BigFloat->precision() >>
3923 set the global variables, and thus B<any> newly created number will be subject
3924 to the global rounding B<immediately>. This means that in the examples above, the
3925 C<3> as argument to C<bdiv()> will also get an accuracy of B<5>.
3927 It is less confusing to either calculate the result fully, and afterwards
3928 round it explicitly, or use the additional parameters to the math
3932 $x = Math::BigFloat->new(2);
3933 $y = $x->copy()->bdiv(3);
3934 print $y->bround(5),"\n"; # will give 0.66667
3939 $x = Math::BigFloat->new(2);
3940 $y = $x->copy()->bdiv(3,5); # will give 0.66667
3947 =item ffround ( +$scale )
3949 Rounds to the $scale'th place left from the '.', counting from the dot.
3950 The first digit is numbered 1.
3952 =item ffround ( -$scale )
3954 Rounds to the $scale'th place right from the '.', counting from the dot.
3958 Rounds to an integer.
3960 =item fround ( +$scale )
3962 Preserves accuracy to $scale digits from the left (aka significant digits)
3963 and pads the rest with zeros. If the number is between 1 and -1, the
3964 significant digits count from the first non-zero after the '.'
3966 =item fround ( -$scale ) and fround ( 0 )
3968 These are effectively no-ops.
3972 All rounding functions take as a second parameter a rounding mode from one of
3973 the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'.
3975 The default rounding mode is 'even'. By using
3976 C<< Math::BigFloat->round_mode($round_mode); >> you can get and set the default
3977 mode for subsequent rounding. The usage of C<$Math::BigFloat::$round_mode> is
3978 no longer supported.
3979 The second parameter to the round functions then overrides the default
3982 The C<as_number()> function returns a BigInt from a Math::BigFloat. It uses
3983 'trunc' as rounding mode to make it equivalent to:
3988 You can override this by passing the desired rounding mode as parameter to
3991 $x = Math::BigFloat->new(2.5);
3992 $y = $x->as_number('odd'); # $y = 3
3996 Math::BigFloat supports all methods that Math::BigInt supports, except it
3997 calculates non-integer results when possible. Please see L<Math::BigInt>
3998 for a full description of each method. Below are just the most important
4003 $x->accuracy(5); # local for $x
4004 CLASS->accuracy(5); # global for all members of CLASS
4005 # Note: This also applies to new()!
4007 $A = $x->accuracy(); # read out accuracy that affects $x
4008 $A = CLASS->accuracy(); # read out global accuracy
4010 Set or get the global or local accuracy, aka how many significant digits the
4011 results have. If you set a global accuracy, then this also applies to new()!
4013 Warning! The accuracy I<sticks>, e.g. once you created a number under the
4014 influence of C<< CLASS->accuracy($A) >>, all results from math operations with
4015 that number will also be rounded.
4017 In most cases, you should probably round the results explicitly using one of
4018 L<round()>, L<bround()> or L<bfround()> or by passing the desired accuracy
4019 to the math operation as additional parameter:
4021 my $x = Math::BigInt->new(30000);
4022 my $y = Math::BigInt->new(7);
4023 print scalar $x->copy()->bdiv($y, 2); # print 4300
4024 print scalar $x->copy()->bdiv($y)->bround(2); # print 4300
4028 $x->precision(-2); # local for $x, round at the second digit right of the dot
4029 $x->precision(2); # ditto, round at the second digit left of the dot
4031 CLASS->precision(5); # Global for all members of CLASS
4032 # This also applies to new()!
4033 CLASS->precision(-5); # ditto
4035 $P = CLASS->precision(); # read out global precision
4036 $P = $x->precision(); # read out precision that affects $x
4038 Note: You probably want to use L<accuracy()> instead. With L<accuracy> you
4039 set the number of digits each result should have, with L<precision> you
4040 set the place where to round!
4044 $x->bexp($accuracy); # calculate e ** X
4046 Calculates the expression C<e ** $x> where C<e> is Euler's number.
4048 This method was added in v1.82 of Math::BigInt (April 2007).
4052 $x->bnok($y); # x over y (binomial coefficient n over k)
4054 Calculates the binomial coefficient n over k, also called the "choose"
4055 function. The result is equivalent to:
4061 This method was added in v1.84 of Math::BigInt (April 2007).
4065 print Math::BigFloat->bpi(100), "\n";
4067 Calculate PI to N digits (including the 3 before the dot). The result is
4068 rounded according to the current rounding mode, which defaults to "even".
4070 This method was added in v1.87 of Math::BigInt (June 2007).
4074 my $x = Math::BigFloat->new(1);
4075 print $x->bcos(100), "\n";
4077 Calculate the cosinus of $x, modifying $x in place.
4079 This method was added in v1.87 of Math::BigInt (June 2007).
4083 my $x = Math::BigFloat->new(1);
4084 print $x->bsin(100), "\n";
4086 Calculate the sinus of $x, modifying $x in place.
4088 This method was added in v1.87 of Math::BigInt (June 2007).
4092 my $y = Math::BigFloat->new(2);
4093 my $x = Math::BigFloat->new(3);
4094 print $y->batan2($x), "\n";
4096 Calculate the arcus tanges of C<$y> divided by C<$x>, modifying $y in place.
4097 See also L<batan()>.
4099 This method was added in v1.87 of Math::BigInt (June 2007).
4103 my $x = Math::BigFloat->new(1);
4104 print $x->batan(100), "\n";
4106 Calculate the arcus tanges of $x, modifying $x in place. See also L<batan2()>.
4108 This method was added in v1.87 of Math::BigInt (June 2007).
4114 Multiply $x by $y, and then add $z to the result.
4116 This method was added in v1.87 of Math::BigInt (June 2007).
4118 =head1 Autocreating constants
4120 After C<use Math::BigFloat ':constant'> all the floating point constants
4121 in the given scope are converted to C<Math::BigFloat>. This conversion
4122 happens at compile time.
4126 perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"'
4128 prints the value of C<2E-100>. Note that without conversion of
4129 constants the expression 2E-100 will be calculated as normal floating point
4132 Please note that ':constant' does not affect integer constants, nor binary
4133 nor hexadecimal constants. Use L<bignum> or L<Math::BigInt> to get this to
4138 Math with the numbers is done (by default) by a module called
4139 Math::BigInt::Calc. This is equivalent to saying:
4141 use Math::BigFloat lib => 'Calc';
4143 You can change this by using:
4145 use Math::BigFloat lib => 'GMP';
4147 Note: The keyword 'lib' will warn when the requested library could not be
4148 loaded. To suppress the warning use 'try' instead:
4150 use Math::BigFloat try => 'GMP';
4152 To turn the warning into a die(), use 'only' instead:
4154 use Math::BigFloat only => 'GMP';
4156 The following would first try to find Math::BigInt::Foo, then
4157 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
4159 use Math::BigFloat lib => 'Foo,Math::BigInt::Bar';
4161 See the respective low-level library documentation for further details.
4163 Please note that Math::BigFloat does B<not> use the denoted library itself,
4164 but it merely passes the lib argument to Math::BigInt. So, instead of the need
4167 use Math::BigInt lib => 'GMP';
4170 you can roll it all into one line:
4172 use Math::BigFloat lib => 'GMP';
4174 It is also possible to just require Math::BigFloat:
4176 require Math::BigFloat;
4178 This will load the necessary things (like BigInt) when they are needed, and
4181 See L<Math::BigInt> for more details than you ever wanted to know about using
4182 a different low-level library.
4184 =head2 Using Math::BigInt::Lite
4186 For backwards compatibility reasons it is still possible to
4187 request a different storage class for use with Math::BigFloat:
4189 use Math::BigFloat with => 'Math::BigInt::Lite';
4191 However, this request is ignored, as the current code now uses the low-level
4192 math libary for directly storing the number parts.
4196 C<Math::BigFloat> exports nothing by default, but can export the C<bpi()> method:
4198 use Math::BigFloat qw/bpi/;
4200 print bpi(10), "\n";
4204 Please see the file BUGS in the CPAN distribution Math::BigInt for known bugs.
4208 Do not try to be clever to insert some operations in between switching
4211 require Math::BigFloat;
4212 my $matter = Math::BigFloat->bone() + 4; # load BigInt and Calc
4213 Math::BigFloat->import( lib => 'Pari' ); # load Pari, too
4214 my $anti_matter = Math::BigFloat->bone()+4; # now use Pari
4216 This will create objects with numbers stored in two different backend libraries,
4217 and B<VERY BAD THINGS> will happen when you use these together:
4219 my $flash_and_bang = $matter + $anti_matter; # Don't do this!
4223 =item stringify, bstr()
4225 Both stringify and bstr() now drop the leading '+'. The old code would return
4226 '+1.23', the new returns '1.23'. See the documentation in L<Math::BigInt> for
4227 reasoning and details.
4231 The following will probably not print what you expect:
4233 print $c->bdiv(123.456),"\n";
4235 It prints both quotient and reminder since print works in list context. Also,
4236 bdiv() will modify $c, so be careful. You probably want to use
4238 print $c / 123.456,"\n";
4239 print scalar $c->bdiv(123.456),"\n"; # or if you want to modify $c
4245 The following will probably not print what you expect:
4247 my $c = Math::BigFloat->new('3.14159');
4248 print $c->brsft(3,10),"\n"; # prints 0.00314153.1415
4250 It prints both quotient and remainder, since print calls C<brsft()> in list
4251 context. Also, C<< $c->brsft() >> will modify $c, so be careful.
4252 You probably want to use
4254 print scalar $c->copy()->brsft(3,10),"\n";
4255 # or if you really want to modify $c
4256 print scalar $c->brsft(3,10),"\n";
4260 =item Modifying and =
4264 $x = Math::BigFloat->new(5);
4267 It will not do what you think, e.g. making a copy of $x. Instead it just makes
4268 a second reference to the B<same> object and stores it in $y. Thus anything
4269 that modifies $x will modify $y (except overloaded math operators), and vice
4270 versa. See L<Math::BigInt> for details and how to avoid that.
4274 C<bpow()> now modifies the first argument, unlike the old code which left
4275 it alone and only returned the result. This is to be consistent with
4276 C<badd()> etc. The first will modify $x, the second one won't:
4278 print bpow($x,$i),"\n"; # modify $x
4279 print $x->bpow($i),"\n"; # ditto
4280 print $x ** $i,"\n"; # leave $x alone
4282 =item precision() vs. accuracy()
4284 A common pitfall is to use L<precision()> when you want to round a result to
4285 a certain number of digits:
4289 Math::BigFloat->precision(4); # does not do what you think it does
4290 my $x = Math::BigFloat->new(12345); # rounds $x to "12000"!
4291 print "$x\n"; # print "12000"
4292 my $y = Math::BigFloat->new(3); # rounds $y to "0"!
4293 print "$y\n"; # print "0"
4294 $z = $x / $y; # 12000 / 0 => NaN!
4296 print $z->precision(),"\n"; # 4
4298 Replacing L<precision> with L<accuracy> is probably not what you want, either:
4302 Math::BigFloat->accuracy(4); # enables global rounding:
4303 my $x = Math::BigFloat->new(123456); # rounded immediately to "12350"
4304 print "$x\n"; # print "123500"
4305 my $y = Math::BigFloat->new(3); # rounded to "3
4306 print "$y\n"; # print "3"
4307 print $z = $x->copy()->bdiv($y),"\n"; # 41170
4308 print $z->accuracy(),"\n"; # 4
4310 What you want to use instead is:
4314 my $x = Math::BigFloat->new(123456); # no rounding
4315 print "$x\n"; # print "123456"
4316 my $y = Math::BigFloat->new(3); # no rounding
4317 print "$y\n"; # print "3"
4318 print $z = $x->copy()->bdiv($y,4),"\n"; # 41150
4319 print $z->accuracy(),"\n"; # undef
4321 In addition to computing what you expected, the last example also does B<not>
4322 "taint" the result with an accuracy or precision setting, which would
4323 influence any further operation.
4329 L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well as
4330 L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
4332 The pragmas L<bignum>, L<bigint> and L<bigrat> might also be of interest
4333 because they solve the autoupgrading/downgrading issue, at least partly.
4335 The package at L<http://search.cpan.org/~tels/Math-BigInt> contains
4336 more documentation including a full version history, testcases, empty
4337 subclass files and benchmarks.
4341 This program is free software; you may redistribute it and/or modify it under
4342 the same terms as Perl itself.
4346 Mark Biggar, overloaded interface by Ilya Zakharevich.
4347 Completely rewritten by Tels L<http://bloodgate.com> in 2001 - 2006, and still