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(Exporter Math::BigInt);
22 # $_trap_inf/$_trap_nan are internal and should never be accessed from outside
23 use vars qw/$AUTOLOAD $accuracy $precision $div_scale $round_mode $rnd_mode
24 $upgrade $downgrade $_trap_nan $_trap_inf/;
25 my $class = "Math::BigFloat";
28 '<=>' => sub { $_[2] ?
29 ref($_[0])->bcmp($_[1],$_[0]) :
30 ref($_[0])->bcmp($_[0],$_[1])},
31 'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint
34 ##############################################################################
35 # global constants, flags and assorted stuff
37 # the following are public, but their usage is not recommended. Use the
38 # accessor methods instead.
40 # class constants, use Class->constant_name() to access
41 $round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'
48 # the package we are using for our private parts, defaults to:
49 # Math::BigInt->config()->{lib}
50 my $MBI = 'Math::BigInt::FastCalc';
52 # are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config()
54 # the same for infinity
57 # constant for easier life
60 my $IMPORT = 0; # was import() called yet? used to make require work
62 # some digits of accuracy for blog(undef,10); which we use in blog() for speed
64 '2.3025850929940456840179914546843642076011014886287729760333279009675726097';
65 my $LOG_10_A = length($LOG_10)-1;
68 '0.6931471805599453094172321214581765680755001343602552541206800094933936220';
69 my $LOG_2_A = length($LOG_2)-1;
70 my $HALF = '0.5'; # made into an object if necc.
72 ##############################################################################
73 # the old code had $rnd_mode, so we need to support it, too
75 sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; }
76 sub FETCH { return $round_mode; }
77 sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); }
81 # when someone set's $rnd_mode, we catch this and check the value to see
82 # whether it is valid or not.
83 $rnd_mode = 'even'; tie $rnd_mode, 'Math::BigFloat';
86 ##############################################################################
89 # valid method aliases for AUTOLOAD
90 my %methods = map { $_ => 1 }
91 qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm
92 fint facmp fcmp fzero fnan finf finc fdec flog ffac fneg
93 fceil ffloor frsft flsft fone flog froot
95 # valid method's that can be hand-ed up (for AUTOLOAD)
96 my %hand_ups = map { $_ => 1 }
97 qw / is_nan is_inf is_negative is_positive is_pos is_neg
98 accuracy precision div_scale round_mode fabs fnot
99 objectify upgrade downgrade
103 sub method_alias { exists $methods{$_[0]||''}; }
104 sub method_hand_up { exists $hand_ups{$_[0]||''}; }
107 ##############################################################################
112 # create a new BigFloat object from a string or another bigfloat object.
115 # sign => sign (+/-), or "NaN"
117 my ($class,$wanted,@r) = @_;
119 # avoid numify-calls by not using || on $wanted!
120 return $class->bzero() if !defined $wanted; # default to 0
121 return $wanted->copy() if UNIVERSAL::isa($wanted,'Math::BigFloat');
123 $class->import() if $IMPORT == 0; # make require work
125 my $self = {}; bless $self, $class;
126 # shortcut for bigints and its subclasses
127 if ((ref($wanted)) && (ref($wanted) ne $class))
129 $self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy
130 $self->{_e} = $MBI->_zero();
132 $self->{sign} = $wanted->sign();
133 return $self->bnorm();
137 # handle '+inf', '-inf' first
138 if ($wanted =~ /^[+-]?inf\z/)
140 return $downgrade->new($wanted) if $downgrade;
142 $self->{sign} = $wanted; # set a default sign for bstr()
143 return $self->binf($wanted);
146 # shortcut for simple forms like '12' that neither have trailing nor leading
148 if ($wanted =~ /^([+-]?)([1-9][0-9]*[1-9])$/)
150 $self->{_e} = $MBI->_zero();
152 $self->{sign} = $1 || '+';
153 $self->{_m} = $MBI->_new($2);
154 return $self->round(@r) if !$downgrade;
157 my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($wanted);
163 Carp::croak ("$wanted is not a number initialized to $class");
166 return $downgrade->bnan() if $downgrade;
168 $self->{_e} = $MBI->_zero();
170 $self->{_m} = $MBI->_zero();
171 $self->{sign} = $nan;
175 # make integer from mantissa by adjusting exp, then convert to int
176 $self->{_e} = $MBI->_new($$ev); # exponent
177 $self->{_es} = $$es || '+';
178 my $mantissa = "$$miv$$mfv"; # create mant.
179 $mantissa =~ s/^0+(\d)/$1/; # strip leading zeros
180 $self->{_m} = $MBI->_new($mantissa); # create mant.
182 # 3.123E0 = 3123E-3, and 3.123E-2 => 3123E-5
183 if (CORE::length($$mfv) != 0)
185 my $len = $MBI->_new( CORE::length($$mfv));
186 ($self->{_e}, $self->{_es}) =
187 _e_sub ($self->{_e}, $len, $self->{_es}, '+');
189 # we can only have trailing zeros on the mantissa if $$mfv eq ''
192 # Use a regexp to count the trailing zeros in $$miv instead of _zeros()
193 # because that is faster, especially when _m is not stored in base 10.
194 my $zeros = 0; $zeros = CORE::length($1) if $$miv =~ /[1-9](0*)$/;
197 my $z = $MBI->_new($zeros);
198 # turn '120e2' into '12e3'
199 $MBI->_rsft ( $self->{_m}, $z, 10);
200 ($self->{_e}, $self->{_es}) =
201 _e_add ( $self->{_e}, $z, $self->{_es}, '+');
204 $self->{sign} = $$mis;
206 # for something like 0Ey, set y to 1, and -0 => +0
207 # Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not
208 # have become 0. That's faster than to call $MBI->_is_zero().
209 $self->{sign} = '+', $self->{_e} = $MBI->_one()
210 if $$miv eq '0' and $$mfv eq '';
212 return $self->round(@r) if !$downgrade;
214 # if downgrade, inf, NaN or integers go down
216 if ($downgrade && $self->{_es} eq '+')
218 if ($MBI->_is_zero( $self->{_e} ))
220 return $downgrade->new($$mis . $MBI->_str( $self->{_m} ));
222 return $downgrade->new($self->bsstr());
224 $self->bnorm()->round(@r); # first normalize, then round
232 # if two arguments, the first one is the class to "swallow" subclasses
240 return unless ref($x); # only for objects
242 my $self = {}; bless $self,$c;
244 $self->{sign} = $x->{sign};
245 $self->{_es} = $x->{_es};
246 $self->{_m} = $MBI->_copy($x->{_m});
247 $self->{_e} = $MBI->_copy($x->{_e});
248 $self->{_a} = $x->{_a} if defined $x->{_a};
249 $self->{_p} = $x->{_p} if defined $x->{_p};
255 # used by parent class bone() to initialize number to NaN
261 my $class = ref($self);
262 Carp::croak ("Tried to set $self to NaN in $class\::_bnan()");
265 $IMPORT=1; # call our import only once
266 $self->{_m} = $MBI->_zero();
267 $self->{_e} = $MBI->_zero();
273 # used by parent class bone() to initialize number to +-inf
279 my $class = ref($self);
280 Carp::croak ("Tried to set $self to +-inf in $class\::_binf()");
283 $IMPORT=1; # call our import only once
284 $self->{_m} = $MBI->_zero();
285 $self->{_e} = $MBI->_zero();
291 # used by parent class bone() to initialize number to 1
293 $IMPORT=1; # call our import only once
294 $self->{_m} = $MBI->_one();
295 $self->{_e} = $MBI->_zero();
301 # used by parent class bone() to initialize number to 0
303 $IMPORT=1; # call our import only once
304 $self->{_m} = $MBI->_zero();
305 $self->{_e} = $MBI->_one();
311 my ($self,$class) = @_;
312 return if $class =~ /^Math::BigInt/; # we aren't one of these
313 UNIVERSAL::isa($self,$class);
318 # return (later set?) configuration data as hash ref
319 my $class = shift || 'Math::BigFloat';
321 my $cfg = $class->SUPER::config(@_);
323 # now we need only to override the ones that are different from our parent
324 $cfg->{class} = $class;
329 ##############################################################################
330 # string conversation
334 # (ref to BFLOAT or num_str ) return num_str
335 # Convert number from internal format to (non-scientific) string format.
336 # internal format is always normalized (no leading zeros, "-0" => "+0")
337 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
339 if ($x->{sign} !~ /^[+-]$/)
341 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
345 my $es = '0'; my $len = 1; my $cad = 0; my $dot = '.';
348 my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m}));
351 $es = $MBI->_str($x->{_m});
352 $len = CORE::length($es);
353 my $e = $MBI->_num($x->{_e});
354 $e = -$e if $x->{_es} eq '-';
358 # if _e is bigger than a scalar, the following will blow your memory
361 my $r = abs($e) - $len;
362 $es = '0.'. ('0' x $r) . $es; $cad = -($len+$r);
366 substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e});
367 $cad = -$cad if $x->{_es} eq '-';
373 $es .= '0' x $e; $len += $e; $cad = 0;
377 $es = '-'.$es if $x->{sign} eq '-';
378 # if set accuracy or precision, pad with zeros on the right side
379 if ((defined $x->{_a}) && ($not_zero))
381 # 123400 => 6, 0.1234 => 4, 0.001234 => 4
382 my $zeros = $x->{_a} - $cad; # cad == 0 => 12340
383 $zeros = $x->{_a} - $len if $cad != $len;
384 $es .= $dot.'0' x $zeros if $zeros > 0;
386 elsif ((($x->{_p} || 0) < 0))
388 # 123400 => 6, 0.1234 => 4, 0.001234 => 6
389 my $zeros = -$x->{_p} + $cad;
390 $es .= $dot.'0' x $zeros if $zeros > 0;
397 # (ref to BFLOAT or num_str ) return num_str
398 # Convert number from internal format to scientific string format.
399 # internal format is always normalized (no leading zeros, "-0E0" => "+0E0")
400 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
402 if ($x->{sign} !~ /^[+-]$/)
404 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
407 my $sep = 'e'.$x->{_es};
408 my $sign = $x->{sign}; $sign = '' if $sign eq '+';
409 $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e});
414 # Make a number from a BigFloat object
415 # simple return a string and let Perl's atoi()/atof() handle the rest
416 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
420 ##############################################################################
421 # public stuff (usually prefixed with "b")
425 # (BINT or num_str) return BINT
426 # negate number or make a negated number from string
427 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
429 return $x if $x->modify('bneg');
431 # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN'
432 $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m}));
437 # XXX TODO this must be overwritten and return NaN for non-integer values
438 # band(), bior(), bxor(), too
441 # $class->SUPER::bnot($class,@_);
446 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
449 my ($self,$x,$y) = (ref($_[0]),@_);
450 # objectify is costly, so avoid it
451 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
453 ($self,$x,$y) = objectify(2,@_);
456 return $upgrade->bcmp($x,$y) if defined $upgrade &&
457 ((!$x->isa($self)) || (!$y->isa($self)));
459 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
461 # handle +-inf and NaN
462 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
463 return 0 if ($x->{sign} eq $y->{sign}) && ($x->{sign} =~ /^[+-]inf$/);
464 return +1 if $x->{sign} eq '+inf';
465 return -1 if $x->{sign} eq '-inf';
466 return -1 if $y->{sign} eq '+inf';
470 # check sign for speed first
471 return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y
472 return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0
475 my $xz = $x->is_zero();
476 my $yz = $y->is_zero();
477 return 0 if $xz && $yz; # 0 <=> 0
478 return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
479 return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
481 # adjust so that exponents are equal
482 my $lxm = $MBI->_len($x->{_m});
483 my $lym = $MBI->_len($y->{_m});
484 # the numify somewhat limits our length, but makes it much faster
485 my ($xes,$yes) = (1,1);
486 $xes = -1 if $x->{_es} ne '+';
487 $yes = -1 if $y->{_es} ne '+';
488 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
489 my $ly = $lym + $yes * $MBI->_num($y->{_e});
490 my $l = $lx - $ly; $l = -$l if $x->{sign} eq '-';
491 return $l <=> 0 if $l != 0;
493 # lengths (corrected by exponent) are equal
494 # so make mantissa equal length by padding with zero (shift left)
495 my $diff = $lxm - $lym;
496 my $xm = $x->{_m}; # not yet copy it
500 $ym = $MBI->_copy($y->{_m});
501 $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10);
505 $xm = $MBI->_copy($x->{_m});
506 $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10);
508 my $rc = $MBI->_acmp($xm,$ym);
509 $rc = -$rc if $x->{sign} eq '-'; # -124 < -123
515 # Compares 2 values, ignoring their signs.
516 # Returns one of undef, <0, =0, >0. (suitable for sort)
519 my ($self,$x,$y) = (ref($_[0]),@_);
520 # objectify is costly, so avoid it
521 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
523 ($self,$x,$y) = objectify(2,@_);
526 return $upgrade->bacmp($x,$y) if defined $upgrade &&
527 ((!$x->isa($self)) || (!$y->isa($self)));
529 # handle +-inf and NaN's
530 if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/)
532 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
533 return 0 if ($x->is_inf() && $y->is_inf());
534 return 1 if ($x->is_inf() && !$y->is_inf());
539 my $xz = $x->is_zero();
540 my $yz = $y->is_zero();
541 return 0 if $xz && $yz; # 0 <=> 0
542 return -1 if $xz && !$yz; # 0 <=> +y
543 return 1 if $yz && !$xz; # +x <=> 0
545 # adjust so that exponents are equal
546 my $lxm = $MBI->_len($x->{_m});
547 my $lym = $MBI->_len($y->{_m});
548 my ($xes,$yes) = (1,1);
549 $xes = -1 if $x->{_es} ne '+';
550 $yes = -1 if $y->{_es} ne '+';
551 # the numify somewhat limits our length, but makes it much faster
552 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
553 my $ly = $lym + $yes * $MBI->_num($y->{_e});
555 return $l <=> 0 if $l != 0;
557 # lengths (corrected by exponent) are equal
558 # so make mantissa equal-length by padding with zero (shift left)
559 my $diff = $lxm - $lym;
560 my $xm = $x->{_m}; # not yet copy it
564 $ym = $MBI->_copy($y->{_m});
565 $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10);
569 $xm = $MBI->_copy($x->{_m});
570 $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10);
572 $MBI->_acmp($xm,$ym);
577 # add second arg (BFLOAT or string) to first (BFLOAT) (modifies first)
578 # return result as BFLOAT
581 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
582 # objectify is costly, so avoid it
583 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
585 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
588 # inf and NaN handling
589 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
592 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
594 if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/))
596 # +inf++inf or -inf+-inf => same, rest is NaN
597 return $x if $x->{sign} eq $y->{sign};
600 # +-inf + something => +inf; something +-inf => +-inf
601 $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/;
605 return $upgrade->badd($x,$y,$a,$p,$r) if defined $upgrade &&
606 ((!$x->isa($self)) || (!$y->isa($self)));
608 # speed: no add for 0+y or x+0
609 return $x->bround($a,$p,$r) if $y->is_zero(); # x+0
610 if ($x->is_zero()) # 0+y
612 # make copy, clobbering up x (modify in place!)
613 $x->{_e} = $MBI->_copy($y->{_e});
614 $x->{_es} = $y->{_es};
615 $x->{_m} = $MBI->_copy($y->{_m});
616 $x->{sign} = $y->{sign} || $nan;
617 return $x->round($a,$p,$r,$y);
620 # take lower of the two e's and adapt m1 to it to match m2
622 $e = $MBI->_zero() if !defined $e; # if no BFLOAT?
623 $e = $MBI->_copy($e); # make copy (didn't do it yet)
627 ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es});
629 my $add = $MBI->_copy($y->{_m});
631 if ($es eq '-') # < 0
633 $MBI->_lsft( $x->{_m}, $e, 10);
634 ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es);
636 elsif (!$MBI->_is_zero($e)) # > 0
638 $MBI->_lsft($add, $e, 10);
640 # else: both e are the same, so just leave them
642 if ($x->{sign} eq $y->{sign})
645 $x->{_m} = $MBI->_add($x->{_m}, $add);
649 ($x->{_m}, $x->{sign}) =
650 _e_add($x->{_m}, $add, $x->{sign}, $y->{sign});
653 # delete trailing zeros, then round
654 $x->bnorm()->round($a,$p,$r,$y);
657 # sub bsub is inherited from Math::BigInt!
661 # increment arg by one
662 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
664 if ($x->{_es} eq '-')
666 return $x->badd($self->bone(),@r); # digits after dot
669 if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf
671 # 1e2 => 100, so after the shift below _m has a '0' as last digit
672 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
673 $x->{_e} = $MBI->_zero(); # normalize
675 # we know that the last digit of $x will be '1' or '9', depending on the
679 if ($x->{sign} eq '+')
681 $MBI->_inc($x->{_m});
682 return $x->bnorm()->bround(@r);
684 elsif ($x->{sign} eq '-')
686 $MBI->_dec($x->{_m});
687 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
688 return $x->bnorm()->bround(@r);
690 # inf, nan handling etc
691 $x->badd($self->bone(),@r); # badd() does round
696 # decrement arg by one
697 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
699 if ($x->{_es} eq '-')
701 return $x->badd($self->bone('-'),@r); # digits after dot
704 if (!$MBI->_is_zero($x->{_e}))
706 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
707 $x->{_e} = $MBI->_zero(); # normalize
711 my $zero = $x->is_zero();
713 if (($x->{sign} eq '-') || $zero)
715 $MBI->_inc($x->{_m});
716 $x->{sign} = '-' if $zero; # 0 => 1 => -1
717 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
718 return $x->bnorm()->round(@r);
721 elsif ($x->{sign} eq '+')
723 $MBI->_dec($x->{_m});
724 return $x->bnorm()->round(@r);
726 # inf, nan handling etc
727 $x->badd($self->bone('-'),@r); # does round
734 my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
736 # $base > 0, $base != 1; if $base == undef default to $base == e
739 # we need to limit the accuracy to protect against overflow
742 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
744 # also takes care of the "error in _find_round_parameters?" case
745 return $x->bnan() if $x->{sign} ne '+' || $x->is_zero();
748 # no rounding at all, so must use fallback
749 if (scalar @params == 0)
751 # simulate old behaviour
752 $params[0] = $self->div_scale(); # and round to it as accuracy
753 $params[1] = undef; # P = undef
754 $scale = $params[0]+4; # at least four more for proper round
755 $params[2] = $r; # round mode by caller or undef
756 $fallback = 1; # to clear a/p afterwards
760 # the 4 below is empirical, and there might be cases where it is not
762 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
765 return $x->bzero(@params) if $x->is_one();
766 # base not defined => base == Euler's constant e
769 # make object, since we don't feed it through objectify() to still get the
770 # case of $base == undef
771 $base = $self->new($base) unless ref($base);
772 # $base > 0; $base != 1
773 return $x->bnan() if $base->is_zero() || $base->is_one() ||
774 $base->{sign} ne '+';
775 # if $x == $base, we know the result must be 1.0
776 if ($x->bcmp($base) == 0)
778 $x->bone('+',@params);
781 # clear a/p after round, since user did not request it
782 delete $x->{_a}; delete $x->{_p};
788 # when user set globals, they would interfere with our calculation, so
789 # disable them and later re-enable them
791 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
792 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
793 # we also need to disable any set A or P on $x (_find_round_parameters took
794 # them already into account), since these would interfere, too
795 delete $x->{_a}; delete $x->{_p};
796 # need to disable $upgrade in BigInt, to avoid deep recursion
797 local $Math::BigInt::upgrade = undef;
798 local $Math::BigFloat::downgrade = undef;
800 # upgrade $x if $x is not a BigFloat (handle BigInt input)
801 if (!$x->isa('Math::BigFloat'))
803 $x = Math::BigFloat->new($x);
809 # If the base is defined and an integer, try to calculate integer result
810 # first. This is very fast, and in case the real result was found, we can
812 if (defined $base && $base->is_int() && $x->is_int())
814 my $i = $MBI->_copy( $x->{_m} );
815 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
816 my $int = Math::BigInt->bzero();
818 $int->blog($base->as_number());
820 if ($base->as_number()->bpow($int) == $x)
822 # found result, return it
823 $x->{_m} = $int->{value};
824 $x->{_e} = $MBI->_zero();
833 # first calculate the log to base e (using reduction by 10 (and probably 2))
834 $self->_log_10($x,$scale);
836 # and if a different base was requested, convert it
839 $base = Math::BigFloat->new($base) unless $base->isa('Math::BigFloat');
840 # not ln, but some other base (don't modify $base)
841 $x->bdiv( $base->copy()->blog(undef,$scale), $scale );
845 # shortcut to not run through _find_round_parameters again
846 if (defined $params[0])
848 $x->bround($params[0],$params[2]); # then round accordingly
852 $x->bfround($params[1],$params[2]); # then round accordingly
856 # clear a/p after round, since user did not request it
857 delete $x->{_a}; delete $x->{_p};
860 $$abr = $ab; $$pbr = $pb;
867 # internal log function to calculate ln() based on Taylor series.
868 # Modifies $x in place.
869 my ($self,$x,$scale) = @_;
871 # in case of $x == 1, result is 0
872 return $x->bzero() if $x->is_one();
874 # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log
878 # Taylor: | u 1 u^3 1 u^5 |
879 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0
880 # |_ v 3 v^3 5 v^5 _|
882 # This takes much more steps to calculate the result and is thus not used
885 # Taylor: | u 1 u^2 1 u^3 |
886 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2
887 # |_ x 2 x^2 3 x^3 _|
889 my ($limit,$v,$u,$below,$factor,$two,$next,$over,$f);
891 $v = $x->copy(); $v->binc(); # v = x+1
892 $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1
893 $x->bdiv($v,$scale); # first term: u/v
896 $u *= $u; $v *= $v; # u^2, v^2
897 $below->bmul($v); # u^3, v^3
899 $factor = $self->new(3); $f = $self->new(2);
901 my $steps = 0 if DEBUG;
902 $limit = $self->new("1E-". ($scale-1));
905 # we calculate the next term, and add it to the last
906 # when the next term is below our limit, it won't affect the outcome
907 # anymore, so we stop
909 # calculating the next term simple from over/below will result in quite
910 # a time hog if the input has many digits, since over and below will
911 # accumulate more and more digits, and the result will also have many
912 # digits, but in the end it is rounded to $scale digits anyway. So if we
913 # round $over and $below first, we save a lot of time for the division
914 # (not with log(1.2345), but try log (123**123) to see what I mean. This
915 # can introduce a rounding error if the division result would be f.i.
916 # 0.1234500000001 and we round it to 5 digits it would become 0.12346, but
917 # if we truncated $over and $below we might get 0.12345. Does this matter
918 # for the end result? So we give $over and $below 4 more digits to be
919 # on the safe side (unscientific error handling as usual... :+D
921 $next = $over->copy->bround($scale+4)->bdiv(
922 $below->copy->bmul($factor)->bround($scale+4),
926 ## $next = $over->copy()->bdiv($below->copy()->bmul($factor),$scale);
928 last if $next->bacmp($limit) <= 0;
930 delete $next->{_a}; delete $next->{_p};
932 # calculate things for the next term
933 $over *= $u; $below *= $v; $factor->badd($f);
936 $steps++; print "step $steps = $x\n" if $steps % 10 == 0;
939 $x->bmul($f); # $x *= 2
940 print "took $steps steps\n" if DEBUG;
945 # Internal log function based on reducing input to the range of 0.1 .. 9.99
946 # and then "correcting" the result to the proper one. Modifies $x in place.
947 my ($self,$x,$scale) = @_;
949 # taking blog() from numbers greater than 10 takes a *very long* time, so we
950 # break the computation down into parts based on the observation that:
951 # blog(x*y) = blog(x) + blog(y)
952 # We set $y here to multiples of 10 so that $x is below 1 (the smaller $x is
953 # the faster it get's, especially because 2*$x takes about 10 times as long,
954 # so by dividing $x by 10 we make it at least factor 100 faster...)
956 # The same observation is valid for numbers smaller than 0.1 (e.g. computing
957 # log(1) is fastest, and the farther away we get from 1, the longer it takes)
958 # so we also 'break' this down by multiplying $x with 10 and subtract the
959 # log(10) afterwards to get the correct result.
961 # calculate nr of digits before dot
962 my $dbd = $MBI->_num($x->{_e});
963 $dbd = -$dbd if $x->{_es} eq '-';
964 $dbd += $MBI->_len($x->{_m});
966 # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid
969 my $calc = 1; # do some calculation?
971 # disable the shortcut for 10, since we need log(10) and this would recurse
973 if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m}))
975 $dbd = 0; # disable shortcut
976 # we can use the cached value in these cases
977 if ($scale <= $LOG_10_A)
979 $x->bzero(); $x->badd($LOG_10);
980 $calc = 0; # no need to calc, but round
985 # disable the shortcut for 2, since we maybe have it cached
986 if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m})))
988 $dbd = 0; # disable shortcut
989 # we can use the cached value in these cases
990 if ($scale <= $LOG_2_A)
992 $x->bzero(); $x->badd($LOG_2);
993 $calc = 0; # no need to calc, but round
998 # if $x = 0.1, we know the result must be 0-log(10)
999 if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) &&
1000 $MBI->_is_one($x->{_m}))
1002 $dbd = 0; # disable shortcut
1003 # we can use the cached value in these cases
1004 if ($scale <= $LOG_10_A)
1006 $x->bzero(); $x->bsub($LOG_10);
1007 $calc = 0; # no need to calc, but round
1011 return if $calc == 0; # already have the result
1013 # default: these correction factors are undef and thus not used
1014 my $l_10; # value of ln(10) to A of $scale
1015 my $l_2; # value of ln(2) to A of $scale
1017 # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1
1018 # so don't do this shortcut for 1 or 0
1019 if (($dbd > 1) || ($dbd < 0))
1021 # convert our cached value to an object if not already (avoid doing this
1022 # at import() time, since not everybody needs this)
1023 $LOG_10 = $self->new($LOG_10,undef,undef) unless ref $LOG_10;
1025 #print "x = $x, dbd = $dbd, calc = $calc\n";
1026 # got more than one digit before the dot, or more than one zero after the
1028 # log(123) == log(1.23) + log(10) * 2
1029 # log(0.0123) == log(1.23) - log(10) * 2
1031 if ($scale <= $LOG_10_A)
1034 $l_10 = $LOG_10->copy(); # copy for mul
1038 # else: slower, compute it (but don't cache it, because it could be big)
1039 # also disable downgrade for this code path
1040 local $Math::BigFloat::downgrade = undef;
1041 $l_10 = $self->new(10)->blog(undef,$scale); # scale+4, actually
1043 $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1
1044 $l_10->bmul( $self->new($dbd)); # log(10) * (digits_before_dot-1)
1051 ($x->{_e}, $x->{_es}) =
1052 _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23
1056 # Now: 0.1 <= $x < 10 (and possible correction in l_10)
1058 ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div
1059 ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1)
1061 $HALF = $self->new($HALF) unless ref($HALF);
1063 my $twos = 0; # default: none (0 times)
1064 my $two = $self->new(2);
1065 while ($x->bacmp($HALF) <= 0)
1067 $twos--; $x->bmul($two);
1069 while ($x->bacmp($two) >= 0)
1071 $twos++; $x->bdiv($two,$scale+4); # keep all digits
1073 # $twos > 0 => did mul 2, < 0 => did div 2 (never both)
1074 # calculate correction factor based on ln(2)
1077 $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2;
1078 if ($scale <= $LOG_2_A)
1081 $l_2 = $LOG_2->copy(); # copy for mul
1085 # else: slower, compute it (but don't cache it, because it could be big)
1086 # also disable downgrade for this code path
1087 local $Math::BigFloat::downgrade = undef;
1088 $l_2 = $two->blog(undef,$scale); # scale+4, actually
1090 $l_2->bmul($twos); # * -2 => subtract, * 2 => add
1093 $self->_log($x,$scale); # need to do the "normal" way
1094 $x->badd($l_10) if defined $l_10; # correct it by ln(10)
1095 $x->badd($l_2) if defined $l_2; # and maybe by ln(2)
1096 # all done, $x contains now the result
1101 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
1102 # does not modify arguments, but returns new object
1103 # Lowest Common Multiplicator
1105 my ($self,@arg) = objectify(0,@_);
1106 my $x = $self->new(shift @arg);
1107 while (@arg) { $x = Math::BigInt::__lcm($x,shift @arg); }
1113 # (BINT or num_str, BINT or num_str) return BINT
1114 # does not modify arguments, but returns new object
1117 $y = __PACKAGE__->new($y) if !ref($y);
1119 my $x = $y->copy()->babs(); # keep arguments
1121 return $x->bnan() if $x->{sign} !~ /^[+-]$/ # x NaN?
1122 || !$x->is_int(); # only for integers now
1126 my $t = shift; $t = $self->new($t) if !ref($t);
1127 $y = $t->copy()->babs();
1129 return $x->bnan() if $y->{sign} !~ /^[+-]$/ # y NaN?
1130 || !$y->is_int(); # only for integers now
1132 # greatest common divisor
1133 while (! $y->is_zero())
1135 ($x,$y) = ($y->copy(), $x->copy()->bmod($y));
1138 last if $x->is_one();
1143 ##############################################################################
1147 # Internal helper sub to take two positive integers and their signs and
1148 # then add them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')),
1149 # output ($CALC,('+'|'-'))
1150 my ($x,$y,$xs,$ys) = @_;
1152 # if the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8)
1155 $x = $MBI->_add ($x, $y ); # a+b
1156 # the sign follows $xs
1160 my $a = $MBI->_acmp($x,$y);
1163 $x = $MBI->_sub ($x , $y); # abs sub
1167 $x = $MBI->_zero(); # result is 0
1172 $x = $MBI->_sub ( $y, $x, 1 ); # abs sub
1180 # Internal helper sub to take two positive integers and their signs and
1181 # then subtract them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')),
1182 # output ($CALC,('+'|'-'))
1183 my ($x,$y,$xs,$ys) = @_;
1187 _e_add($x,$y,$xs,$ys); # call add (does subtract now)
1190 ###############################################################################
1191 # is_foo methods (is_negative, is_positive are inherited from BigInt)
1195 # return true if arg (BFLOAT or num_str) is an integer
1196 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1198 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
1199 $x->{_es} eq '+'; # 1e-1 => no integer
1205 # return true if arg (BFLOAT or num_str) is zero
1206 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1208 return 1 if $x->{sign} eq '+' && $MBI->_is_zero($x->{_m});
1214 # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given
1215 my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_);
1217 $sign = '+' if !defined $sign || $sign ne '-';
1219 if ($x->{sign} eq $sign &&
1220 $MBI->_is_zero($x->{_e}) && $MBI->_is_one($x->{_m}));
1226 # return true if arg (BFLOAT or num_str) is odd or false if even
1227 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1229 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1230 ($MBI->_is_zero($x->{_e}) && $MBI->_is_odd($x->{_m}));
1236 # return true if arg (BINT or num_str) is even or false if odd
1237 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1239 return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
1240 return 1 if ($x->{_es} eq '+' # 123.45 is never
1241 && $MBI->_is_even($x->{_m})); # but 1200 is
1247 # multiply two numbers -- stolen from Knuth Vol 2 pg 233
1248 # (BINT or num_str, BINT or num_str) return BINT
1251 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1252 # objectify is costly, so avoid it
1253 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1255 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1258 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
1261 if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/))
1263 return $x->bnan() if $x->is_zero() || $y->is_zero();
1264 # result will always be +-inf:
1265 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1266 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1267 return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
1268 return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
1269 return $x->binf('-');
1272 return $x->bzero() if $x->is_zero() || $y->is_zero();
1274 return $upgrade->bmul($x,$y,$a,$p,$r) if defined $upgrade &&
1275 ((!$x->isa($self)) || (!$y->isa($self)));
1277 # aEb * cEd = (a*c)E(b+d)
1278 $MBI->_mul($x->{_m},$y->{_m});
1279 ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1282 $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
1283 return $x->bnorm()->round($a,$p,$r,$y);
1288 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return
1289 # (BFLOAT,BFLOAT) (quo,rem) or BFLOAT (only rem)
1292 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1293 # objectify is costly, so avoid it
1294 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1296 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1299 return $self->_div_inf($x,$y)
1300 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
1302 # x== 0 # also: or y == 1 or y == -1
1303 return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero();
1306 return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade;
1308 # we need to limit the accuracy to protect against overflow
1310 my (@params,$scale);
1311 ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y);
1313 return $x if $x->is_nan(); # error in _find_round_parameters?
1315 # no rounding at all, so must use fallback
1316 if (scalar @params == 0)
1318 # simulate old behaviour
1319 $params[0] = $self->div_scale(); # and round to it as accuracy
1320 $scale = $params[0]+4; # at least four more for proper round
1321 $params[2] = $r; # round mode by caller or undef
1322 $fallback = 1; # to clear a/p afterwards
1326 # the 4 below is empirical, and there might be cases where it is not
1328 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1331 my $rem; $rem = $self->bzero() if wantarray;
1333 $y = $self->new($y) unless $y->isa('Math::BigFloat');
1335 my $lx = $MBI->_len($x->{_m}); my $ly = $MBI->_len($y->{_m});
1336 $scale = $lx if $lx > $scale;
1337 $scale = $ly if $ly > $scale;
1338 my $diff = $ly - $lx;
1339 $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx!
1341 # already handled inf/NaN/-inf above:
1343 # check that $y is not 1 nor -1 and cache the result:
1344 my $y_not_one = !($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m}));
1346 # flipping the sign of $y will also flip the sign of $x for the special
1347 # case of $x->bsub($x); so we can catch it below:
1348 my $xsign = $x->{sign};
1349 $y->{sign} =~ tr/+-/-+/;
1351 if ($xsign ne $x->{sign})
1353 # special case of $x /= $x results in 1
1354 $x->bone(); # "fixes" also sign of $y, since $x is $y
1358 # correct $y's sign again
1359 $y->{sign} =~ tr/+-/-+/;
1360 # continue with normal div code:
1362 # make copy of $x in case of list context for later reminder calculation
1363 if (wantarray && $y_not_one)
1368 $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+';
1370 # check for / +-1 ( +/- 1E0)
1373 # promote BigInts and it's subclasses (except when already a BigFloat)
1374 $y = $self->new($y) unless $y->isa('Math::BigFloat');
1376 # calculate the result to $scale digits and then round it
1377 # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d)
1378 $MBI->_lsft($x->{_m},$MBI->_new($scale),10);
1379 $MBI->_div ($x->{_m},$y->{_m}); # a/c
1381 # correct exponent of $x
1382 ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1383 # correct for 10**scale
1384 ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+');
1385 $x->bnorm(); # remove trailing 0's
1387 } # ende else $x != $y
1389 # shortcut to not run through _find_round_parameters again
1390 if (defined $params[0])
1392 delete $x->{_a}; # clear before round
1393 $x->bround($params[0],$params[2]); # then round accordingly
1397 delete $x->{_p}; # clear before round
1398 $x->bfround($params[1],$params[2]); # then round accordingly
1402 # clear a/p after round, since user did not request it
1403 delete $x->{_a}; delete $x->{_p};
1410 $rem->bmod($y,@params); # copy already done
1414 # clear a/p after round, since user did not request it
1415 delete $rem->{_a}; delete $rem->{_p};
1424 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return reminder
1427 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1428 # objectify is costly, so avoid it
1429 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1431 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1434 # handle NaN, inf, -inf
1435 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
1437 my ($d,$re) = $self->SUPER::_div_inf($x,$y);
1438 $x->{sign} = $re->{sign};
1439 $x->{_e} = $re->{_e};
1440 $x->{_m} = $re->{_m};
1441 return $x->round($a,$p,$r,$y);
1445 return $x->bnan() if $x->is_zero();
1449 return $x->bzero() if $x->is_zero()
1451 # check that $y == +1 or $y == -1:
1452 ($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m})));
1454 my $cmp = $x->bacmp($y); # equal or $x < $y?
1455 return $x->bzero($a,$p) if $cmp == 0; # $x == $y => result 0
1457 # only $y of the operands negative?
1458 my $neg = 0; $neg = 1 if $x->{sign} ne $y->{sign};
1460 $x->{sign} = $y->{sign}; # calc sign first
1461 return $x->round($a,$p,$r) if $cmp < 0 && $neg == 0; # $x < $y => result $x
1463 my $ym = $MBI->_copy($y->{_m});
1466 $MBI->_lsft( $ym, $y->{_e}, 10)
1467 if $y->{_es} eq '+' && !$MBI->_is_zero($y->{_e});
1469 # if $y has digits after dot
1470 my $shifty = 0; # correct _e of $x by this
1471 if ($y->{_es} eq '-') # has digits after dot
1473 # 123 % 2.5 => 1230 % 25 => 5 => 0.5
1474 $shifty = $MBI->_num($y->{_e}); # no more digits after dot
1475 $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25
1477 # $ym is now mantissa of $y based on exponent 0
1479 my $shiftx = 0; # correct _e of $x by this
1480 if ($x->{_es} eq '-') # has digits after dot
1482 # 123.4 % 20 => 1234 % 200
1483 $shiftx = $MBI->_num($x->{_e}); # no more digits after dot
1484 $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230
1486 # 123e1 % 20 => 1230 % 20
1487 if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e}))
1489 $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here
1492 $x->{_e} = $MBI->_new($shiftx);
1494 $x->{_es} = '-' if $shiftx != 0 || $shifty != 0;
1495 $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0;
1497 # now mantissas are equalized, exponent of $x is adjusted, so calc result
1499 $x->{_m} = $MBI->_mod( $x->{_m}, $ym);
1501 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1504 if ($neg != 0) # one of them negative => correct in place
1507 $x->{_m} = $r->{_m};
1508 $x->{_e} = $r->{_e};
1509 $x->{_es} = $r->{_es};
1510 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1514 $x->round($a,$p,$r,$y); # round and return
1519 # calculate $y'th root of $x
1522 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1523 # objectify is costly, so avoid it
1524 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1526 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1529 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
1530 return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() ||
1531 $y->{sign} !~ /^\+$/;
1533 return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one();
1535 # we need to limit the accuracy to protect against overflow
1537 my (@params,$scale);
1538 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1540 return $x if $x->is_nan(); # error in _find_round_parameters?
1542 # no rounding at all, so must use fallback
1543 if (scalar @params == 0)
1545 # simulate old behaviour
1546 $params[0] = $self->div_scale(); # and round to it as accuracy
1547 $scale = $params[0]+4; # at least four more for proper round
1548 $params[2] = $r; # iound mode by caller or undef
1549 $fallback = 1; # to clear a/p afterwards
1553 # the 4 below is empirical, and there might be cases where it is not
1555 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1558 # when user set globals, they would interfere with our calculation, so
1559 # disable them and later re-enable them
1561 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
1562 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
1563 # we also need to disable any set A or P on $x (_find_round_parameters took
1564 # them already into account), since these would interfere, too
1565 delete $x->{_a}; delete $x->{_p};
1566 # need to disable $upgrade in BigInt, to avoid deep recursion
1567 local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI
1569 # remember sign and make $x positive, since -4 ** (1/2) => -2
1570 my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+';
1573 if ($y->isa('Math::BigFloat'))
1575 $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e}));
1579 $is_two = ($y == 2);
1582 # normal square root if $y == 2:
1585 $x->bsqrt($scale+4);
1587 elsif ($y->is_one('-'))
1590 my $u = $self->bone()->bdiv($x,$scale);
1591 # copy private parts over
1592 $x->{_m} = $u->{_m};
1593 $x->{_e} = $u->{_e};
1594 $x->{_es} = $u->{_es};
1598 # calculate the broot() as integer result first, and if it fits, return
1599 # it rightaway (but only if $x and $y are integer):
1601 my $done = 0; # not yet
1602 if ($y->is_int() && $x->is_int())
1604 my $i = $MBI->_copy( $x->{_m} );
1605 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
1606 my $int = Math::BigInt->bzero();
1608 $int->broot($y->as_number());
1610 if ($int->copy()->bpow($y) == $x)
1612 # found result, return it
1613 $x->{_m} = $int->{value};
1614 $x->{_e} = $MBI->_zero();
1622 my $u = $self->bone()->bdiv($y,$scale+4);
1623 delete $u->{_a}; delete $u->{_p}; # otherwise it conflicts
1624 $x->bpow($u,$scale+4); # el cheapo
1627 $x->bneg() if $sign == 1;
1629 # shortcut to not run through _find_round_parameters again
1630 if (defined $params[0])
1632 $x->bround($params[0],$params[2]); # then round accordingly
1636 $x->bfround($params[1],$params[2]); # then round accordingly
1640 # clear a/p after round, since user did not request it
1641 delete $x->{_a}; delete $x->{_p};
1644 $$abr = $ab; $$pbr = $pb;
1650 # calculate square root
1651 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
1653 return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0
1654 return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf
1655 return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one();
1657 # we need to limit the accuracy to protect against overflow
1659 my (@params,$scale);
1660 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1662 return $x if $x->is_nan(); # error in _find_round_parameters?
1664 # no rounding at all, so must use fallback
1665 if (scalar @params == 0)
1667 # simulate old behaviour
1668 $params[0] = $self->div_scale(); # and round to it as accuracy
1669 $scale = $params[0]+4; # at least four more for proper round
1670 $params[2] = $r; # round mode by caller or undef
1671 $fallback = 1; # to clear a/p afterwards
1675 # the 4 below is empirical, and there might be cases where it is not
1677 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1680 # when user set globals, they would interfere with our calculation, so
1681 # disable them and later re-enable them
1683 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
1684 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
1685 # we also need to disable any set A or P on $x (_find_round_parameters took
1686 # them already into account), since these would interfere, too
1687 delete $x->{_a}; delete $x->{_p};
1688 # need to disable $upgrade in BigInt, to avoid deep recursion
1689 local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI
1691 my $i = $MBI->_copy( $x->{_m} );
1692 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
1693 my $xas = Math::BigInt->bzero();
1696 my $gs = $xas->copy()->bsqrt(); # some guess
1698 if (($x->{_es} ne '-') # guess can't be accurate if there are
1699 # digits after the dot
1700 && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head?
1702 # exact result, copy result over to keep $x
1703 $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+';
1705 # shortcut to not run through _find_round_parameters again
1706 if (defined $params[0])
1708 $x->bround($params[0],$params[2]); # then round accordingly
1712 $x->bfround($params[1],$params[2]); # then round accordingly
1716 # clear a/p after round, since user did not request it
1717 delete $x->{_a}; delete $x->{_p};
1719 # re-enable A and P, upgrade is taken care of by "local"
1720 ${"$self\::accuracy"} = $ab; ${"$self\::precision"} = $pb;
1724 # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the accuracy
1725 # of the result by multipyling the input by 100 and then divide the integer
1726 # result of sqrt(input) by 10. Rounding afterwards returns the real result.
1728 # The following steps will transform 123.456 (in $x) into 123456 (in $y1)
1729 my $y1 = $MBI->_copy($x->{_m});
1731 my $length = $MBI->_len($y1);
1733 # Now calculate how many digits the result of sqrt(y1) would have
1734 my $digits = int($length / 2);
1736 # But we need at least $scale digits, so calculate how many are missing
1737 my $shift = $scale - $digits;
1739 # That should never happen (we take care of integer guesses above)
1740 # $shift = 0 if $shift < 0;
1742 # Multiply in steps of 100, by shifting left two times the "missing" digits
1743 my $s2 = $shift * 2;
1745 # We now make sure that $y1 has the same odd or even number of digits than
1746 # $x had. So when _e of $x is odd, we must shift $y1 by one digit left,
1747 # because we always must multiply by steps of 100 (sqrt(100) is 10) and not
1748 # steps of 10. The length of $x does not count, since an even or odd number
1749 # of digits before the dot is not changed by adding an even number of digits
1750 # after the dot (the result is still odd or even digits long).
1751 $s2++ if $MBI->_is_odd($x->{_e});
1753 $MBI->_lsft( $y1, $MBI->_new($s2), 10);
1755 # now take the square root and truncate to integer
1756 $y1 = $MBI->_sqrt($y1);
1758 # By "shifting" $y1 right (by creating a negative _e) we calculate the final
1759 # result, which is than later rounded to the desired scale.
1761 # calculate how many zeros $x had after the '.' (or before it, depending
1762 # on sign of $dat, the result should have half as many:
1763 my $dat = $MBI->_num($x->{_e});
1764 $dat = -$dat if $x->{_es} eq '-';
1769 # no zeros after the dot (e.g. 1.23, 0.49 etc)
1770 # preserve half as many digits before the dot than the input had
1771 # (but round this "up")
1772 $dat = int(($dat+1)/2);
1776 $dat = int(($dat)/2);
1778 $dat -= $MBI->_len($y1);
1782 $x->{_e} = $MBI->_new( $dat );
1787 $x->{_e} = $MBI->_new( $dat );
1793 # shortcut to not run through _find_round_parameters again
1794 if (defined $params[0])
1796 $x->bround($params[0],$params[2]); # then round accordingly
1800 $x->bfround($params[1],$params[2]); # then round accordingly
1804 # clear a/p after round, since user did not request it
1805 delete $x->{_a}; delete $x->{_p};
1808 $$abr = $ab; $$pbr = $pb;
1814 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
1815 # compute factorial number, modifies first argument
1818 my ($self,$x,@r) = (ref($_[0]),@_);
1819 # objectify is costly, so avoid it
1820 ($self,$x,@r) = objectify(1,@_) if !ref($x);
1822 return $x if $x->{sign} eq '+inf'; # inf => inf
1824 if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN
1825 ($x->{_es} ne '+')); # digits after dot?
1827 # use BigInt's bfac() for faster calc
1828 if (! $MBI->_is_zero($x->{_e}))
1830 $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0
1831 $x->{_e} = $MBI->_zero(); # normalize
1834 $MBI->_fac($x->{_m}); # calculate factorial
1835 $x->bnorm()->round(@r); # norm again and round result
1840 # Calculate a power where $y is a non-integer, like 2 ** 0.5
1841 my ($x,$y,$a,$p,$r) = @_;
1844 # if $y == 0.5, it is sqrt($x)
1845 $HALF = $self->new($HALF) unless ref($HALF);
1846 return $x->bsqrt($a,$p,$r,$y) if $y->bcmp($HALF) == 0;
1849 # a ** x == e ** (x * ln a)
1853 # Taylor: | u u^2 u^3 |
1854 # x ** y = 1 + | --- + --- + ----- + ... |
1857 # we need to limit the accuracy to protect against overflow
1859 my ($scale,@params);
1860 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1862 return $x if $x->is_nan(); # error in _find_round_parameters?
1864 # no rounding at all, so must use fallback
1865 if (scalar @params == 0)
1867 # simulate old behaviour
1868 $params[0] = $self->div_scale(); # and round to it as accuracy
1869 $params[1] = undef; # disable P
1870 $scale = $params[0]+4; # at least four more for proper round
1871 $params[2] = $r; # round mode by caller or undef
1872 $fallback = 1; # to clear a/p afterwards
1876 # the 4 below is empirical, and there might be cases where it is not
1878 $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
1881 # when user set globals, they would interfere with our calculation, so
1882 # disable them and later re-enable them
1884 my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
1885 my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
1886 # we also need to disable any set A or P on $x (_find_round_parameters took
1887 # them already into account), since these would interfere, too
1888 delete $x->{_a}; delete $x->{_p};
1889 # need to disable $upgrade in BigInt, to avoid deep recursion
1890 local $Math::BigInt::upgrade = undef;
1892 my ($limit,$v,$u,$below,$factor,$next,$over);
1894 $u = $x->copy()->blog(undef,$scale)->bmul($y);
1895 $v = $self->bone(); # 1
1896 $factor = $self->new(2); # 2
1897 $x->bone(); # first term: 1
1899 $below = $v->copy();
1902 $limit = $self->new("1E-". ($scale-1));
1906 # we calculate the next term, and add it to the last
1907 # when the next term is below our limit, it won't affect the outcome
1908 # anymore, so we stop
1909 $next = $over->copy()->bdiv($below,$scale);
1910 last if $next->bacmp($limit) <= 0;
1912 # calculate things for the next term
1913 $over *= $u; $below *= $factor; $factor->binc();
1915 last if $x->{sign} !~ /^[-+]$/;
1920 # shortcut to not run through _find_round_parameters again
1921 if (defined $params[0])
1923 $x->bround($params[0],$params[2]); # then round accordingly
1927 $x->bfround($params[1],$params[2]); # then round accordingly
1931 # clear a/p after round, since user did not request it
1932 delete $x->{_a}; delete $x->{_p};
1935 $$abr = $ab; $$pbr = $pb;
1941 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
1942 # compute power of two numbers, second arg is used as integer
1943 # modifies first argument
1946 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1947 # objectify is costly, so avoid it
1948 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1950 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1953 return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
1954 return $x if $x->{sign} =~ /^[+-]inf$/;
1957 return $x->bnan() if $x->{sign} eq '-' && $y->{sign} eq '-';
1959 # cache the result of is_zero
1960 my $y_is_zero = $y->is_zero();
1961 return $x->bone() if $y_is_zero;
1962 return $x if $x->is_one() || $y->is_one();
1964 my $x_is_zero = $x->is_zero();
1965 return $x->_pow($y,$a,$p,$r) if !$x_is_zero && !$y->is_int(); # non-integer power
1967 my $y1 = $y->as_number()->{value}; # make MBI part
1970 if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e}))
1972 # if $x == -1 and odd/even y => +1/-1 because +-1 ^ (+-1) => +-1
1973 return $MBI->_is_odd($y1) ? $x : $x->babs(1);
1977 return $x->bone() if $y_is_zero;
1978 return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0)
1979 # 0 ** -y => 1 / (0 ** y) => 1 / 0! (1 / 0 => +inf)
1984 $new_sign = $MBI->_is_odd($y1) ? '-' : '+' if $x->{sign} ne '+';
1986 # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster)
1987 $x->{_m} = $MBI->_pow( $x->{_m}, $y1);
1988 $x->{_e} = $MBI->_mul ($x->{_e}, $y1);
1990 $x->{sign} = $new_sign;
1992 if ($y->{sign} eq '-')
1994 # modify $x in place!
1995 my $z = $x->copy(); $x->bone();
1996 return $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!)
1998 $x->round($a,$p,$r,$y);
2001 ###############################################################################
2002 # rounding functions
2006 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
2007 # $n == 0 means round to integer
2008 # expects and returns normalized numbers!
2009 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
2011 my ($scale,$mode) = $x->_scale_p(@_);
2012 return $x if !defined $scale || $x->modify('bfround'); # no-op
2014 # never round a 0, +-inf, NaN
2017 $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2
2020 return $x if $x->{sign} !~ /^[+-]$/;
2022 # don't round if x already has lower precision
2023 return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p});
2025 $x->{_p} = $scale; # remember round in any case
2026 delete $x->{_a}; # and clear A
2029 # round right from the '.'
2031 return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round
2033 $scale = -$scale; # positive for simplicity
2034 my $len = $MBI->_len($x->{_m}); # length of mantissa
2036 # the following poses a restriction on _e, but if _e is bigger than a
2037 # scalar, you got other problems (memory etc) anyway
2038 my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot
2039 my $zad = 0; # zeros after dot
2040 $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style
2042 # p rint "scale $scale dad $dad zad $zad len $len\n";
2043 # number bsstr len zad dad
2044 # 0.123 123e-3 3 0 3
2045 # 0.0123 123e-4 3 1 4
2048 # 1.2345 12345e-4 5 0 4
2050 # do not round after/right of the $dad
2051 return $x if $scale > $dad; # 0.123, scale >= 3 => exit
2053 # round to zero if rounding inside the $zad, but not for last zero like:
2054 # 0.0065, scale -2, round last '0' with following '65' (scale == zad case)
2055 return $x->bzero() if $scale < $zad;
2056 if ($scale == $zad) # for 0.006, scale -3 and trunc
2062 # adjust round-point to be inside mantissa
2065 $scale = $scale-$zad;
2069 my $dbd = $len - $dad; $dbd = 0 if $dbd < 0; # digits before dot
2070 $scale = $dbd+$scale;
2076 # round left from the '.'
2078 # 123 => 100 means length(123) = 3 - $scale (2) => 1
2080 my $dbt = $MBI->_len($x->{_m});
2082 my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e}));
2083 # should be the same, so treat it as this
2084 $scale = 1 if $scale == 0;
2085 # shortcut if already integer
2086 return $x if $scale == 1 && $dbt <= $dbd;
2087 # maximum digits before dot
2092 # not enough digits before dot, so round to zero
2095 elsif ( $scale == $dbd )
2102 $scale = $dbd - $scale;
2105 # pass sign to bround for rounding modes '+inf' and '-inf'
2106 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
2107 $m->bround($scale,$mode);
2108 $x->{_m} = $m->{value}; # get our mantissa back
2114 # accuracy: preserve $N digits, and overwrite the rest with 0's
2115 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
2117 if (($_[0] || 0) < 0)
2119 require Carp; Carp::croak ('bround() needs positive accuracy');
2122 my ($scale,$mode) = $x->_scale_a(@_);
2123 return $x if !defined $scale || $x->modify('bround'); # no-op
2125 # scale is now either $x->{_a}, $accuracy, or the user parameter
2126 # test whether $x already has lower accuracy, do nothing in this case
2127 # but do round if the accuracy is the same, since a math operation might
2128 # want to round a number with A=5 to 5 digits afterwards again
2129 return $x if defined $x->{_a} && $x->{_a} < $scale;
2131 # scale < 0 makes no sense
2132 # scale == 0 => keep all digits
2133 # never round a +-inf, NaN
2134 return $x if ($scale <= 0) || $x->{sign} !~ /^[+-]$/;
2136 # 1: never round a 0
2137 # 2: if we should keep more digits than the mantissa has, do nothing
2138 if ($x->is_zero() || $MBI->_len($x->{_m}) <= $scale)
2140 $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale;
2144 # pass sign to bround for '+inf' and '-inf' rounding modes
2145 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
2147 $m->bround($scale,$mode); # round mantissa
2148 $x->{_m} = $m->{value}; # get our mantissa back
2149 $x->{_a} = $scale; # remember rounding
2150 delete $x->{_p}; # and clear P
2151 $x->bnorm(); # del trailing zeros gen. by bround()
2156 # return integer less or equal then $x
2157 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2159 return $x if $x->modify('bfloor');
2161 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2163 # if $x has digits after dot
2164 if ($x->{_es} eq '-')
2166 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
2167 $x->{_e} = $MBI->_zero(); # trunc/norm
2168 $x->{_es} = '+'; # abs e
2169 $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative
2171 $x->round($a,$p,$r);
2176 # return integer greater or equal then $x
2177 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2179 return $x if $x->modify('bceil');
2180 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2182 # if $x has digits after dot
2183 if ($x->{_es} eq '-')
2185 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
2186 $x->{_e} = $MBI->_zero(); # trunc/norm
2187 $x->{_es} = '+'; # abs e
2188 $MBI->_inc($x->{_m}) if $x->{sign} eq '+'; # increment if positive
2190 $x->round($a,$p,$r);
2195 # shift right by $y (divide by power of $n)
2198 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
2199 # objectify is costly, so avoid it
2200 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
2202 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
2205 return $x if $x->modify('brsft');
2206 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2208 $n = 2 if !defined $n; $n = $self->new($n);
2209 $x->bdiv($n->bpow($y),$a,$p,$r,$y);
2214 # shift left by $y (multiply by power of $n)
2217 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
2218 # objectify is costly, so avoid it
2219 if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
2221 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
2224 return $x if $x->modify('blsft');
2225 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2227 $n = 2 if !defined $n; $n = $self->new($n);
2228 $x->bmul($n->bpow($y),$a,$p,$r,$y);
2231 ###############################################################################
2235 # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub
2240 # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx()
2241 # or falling back to MBI::bxxx()
2242 my $name = $AUTOLOAD;
2244 $name =~ s/(.*):://; # split package
2245 my $c = $1 || $class;
2247 $c->import() if $IMPORT == 0;
2248 if (!method_alias($name))
2252 # delayed load of Carp and avoid recursion
2254 Carp::croak ("$c: Can't call a method without name");
2256 if (!method_hand_up($name))
2258 # delayed load of Carp and avoid recursion
2260 Carp::croak ("Can't call $c\-\>$name, not a valid method");
2262 # try one level up, but subst. bxxx() for fxxx() since MBI only got bxxx()
2264 return &{"Math::BigInt"."::$name"}(@_);
2266 my $bname = $name; $bname =~ s/^f/b/;
2274 # return a copy of the exponent
2275 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2277 if ($x->{sign} !~ /^[+-]$/)
2279 my $s = $x->{sign}; $s =~ s/^[+-]//;
2280 return Math::BigInt->new($s); # -inf, +inf => +inf
2282 Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e}));
2287 # return a copy of the mantissa
2288 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2290 if ($x->{sign} !~ /^[+-]$/)
2292 my $s = $x->{sign}; $s =~ s/^[+]//;
2293 return Math::BigInt->new($s); # -inf, +inf => +inf
2295 my $m = Math::BigInt->new( $MBI->_str($x->{_m}));
2296 $m->bneg() if $x->{sign} eq '-';
2303 # return a copy of both the exponent and the mantissa
2304 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2306 if ($x->{sign} !~ /^[+-]$/)
2308 my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//;
2309 return ($self->new($s),$self->new($se)); # +inf => inf and -inf,+inf => inf
2311 my $m = Math::BigInt->bzero();
2312 $m->{value} = $MBI->_copy($x->{_m});
2313 $m->bneg() if $x->{sign} eq '-';
2314 ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) ));
2317 ##############################################################################
2318 # private stuff (internal use only)
2324 my $lib = ''; my @a;
2326 for ( my $i = 0; $i < $l ; $i++)
2328 if ( $_[$i] eq ':constant' )
2330 # This causes overlord er load to step in. 'binary' and 'integer'
2331 # are handled by BigInt.
2332 overload::constant float => sub { $self->new(shift); };
2334 elsif ($_[$i] eq 'upgrade')
2336 # this causes upgrading
2337 $upgrade = $_[$i+1]; # or undef to disable
2340 elsif ($_[$i] eq 'downgrade')
2342 # this causes downgrading
2343 $downgrade = $_[$i+1]; # or undef to disable
2346 elsif ($_[$i] eq 'lib')
2348 # alternative library
2349 $lib = $_[$i+1] || ''; # default Calc
2352 elsif ($_[$i] eq 'with')
2354 # alternative class for our private parts()
2355 # XXX: no longer supported
2356 # $MBI = $_[$i+1] || 'Math::BigInt';
2365 $lib =~ tr/a-zA-Z0-9,://cd; # restrict to sane characters
2366 # let use Math::BigInt lib => 'GMP'; use Math::BigFloat; still work
2367 my $mbilib = eval { Math::BigInt->config()->{lib} };
2368 if ((defined $mbilib) && ($MBI eq 'Math::BigInt::Calc'))
2370 # MBI already loaded
2371 Math::BigInt->import('lib',"$lib,$mbilib", 'objectify');
2375 # MBI not loaded, or with ne "Math::BigInt::Calc"
2376 $lib .= ",$mbilib" if defined $mbilib;
2377 $lib =~ s/^,//; # don't leave empty
2379 # replacement library can handle lib statement, but also could ignore it
2381 # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is
2382 # used in the same script, or eval inside import(). So we require MBI:
2383 require Math::BigInt;
2384 Math::BigInt->import( lib => $lib, 'objectify' );
2388 require Carp; Carp::croak ("Couldn't load $lib: $! $@");
2390 # find out which one was actually loaded
2391 $MBI = Math::BigInt->config()->{lib};
2393 # register us with MBI to get notified of future lib changes
2394 Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } );
2396 # any non :constant stuff is handled by our parent, Exporter
2397 # even if @_ is empty, to give it a chance
2398 $self->SUPER::import(@a); # for subclasses
2399 $self->export_to_level(1,$self,@a); # need this, too
2404 # adjust m and e so that m is smallest possible
2405 # round number according to accuracy and precision settings
2406 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
2408 return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2410 my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros
2413 my $z = $MBI->_new($zeros);
2414 $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10);
2415 if ($x->{_es} eq '-')
2417 if ($MBI->_acmp($x->{_e},$z) >= 0)
2419 $x->{_e} = $MBI->_sub ($x->{_e}, $z);
2420 $x->{_es} = '+' if $MBI->_is_zero($x->{_e});
2424 $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e});
2430 $x->{_e} = $MBI->_add ($x->{_e}, $z);
2435 # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing
2436 # zeros). So, for something like 0Ey, set y to 1, and -0 => +0
2437 $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one()
2438 if $MBI->_is_zero($x->{_m});
2441 $x; # MBI bnorm is no-op, so dont call it
2444 ##############################################################################
2448 # return number as hexadecimal string (only for integers defined)
2449 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2451 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2452 return '0x0' if $x->is_zero();
2454 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
2456 my $z = $MBI->_copy($x->{_m});
2457 if (! $MBI->_is_zero($x->{_e})) # > 0
2459 $MBI->_lsft( $z, $x->{_e},10);
2461 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2467 # return number as binary digit string (only for integers defined)
2468 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2470 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2471 return '0b0' if $x->is_zero();
2473 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
2475 my $z = $MBI->_copy($x->{_m});
2476 if (! $MBI->_is_zero($x->{_e})) # > 0
2478 $MBI->_lsft( $z, $x->{_e},10);
2480 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2486 # return copy as a bigint representation of this BigFloat number
2487 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2489 my $z = $MBI->_copy($x->{_m});
2490 if ($x->{_es} eq '-') # < 0
2492 $MBI->_rsft( $z, $x->{_e},10);
2494 elsif (! $MBI->_is_zero($x->{_e})) # > 0
2496 $MBI->_lsft( $z, $x->{_e},10);
2498 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2505 my $class = ref($x) || $x;
2506 $x = $class->new(shift) unless ref($x);
2508 return 1 if $MBI->_is_zero($x->{_m});
2510 my $len = $MBI->_len($x->{_m});
2511 $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+';
2515 $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-';
2526 Math::BigFloat - Arbitrary size floating point math package
2533 $x = Math::BigFloat->new($str); # defaults to 0
2534 $nan = Math::BigFloat->bnan(); # create a NotANumber
2535 $zero = Math::BigFloat->bzero(); # create a +0
2536 $inf = Math::BigFloat->binf(); # create a +inf
2537 $inf = Math::BigFloat->binf('-'); # create a -inf
2538 $one = Math::BigFloat->bone(); # create a +1
2539 $one = Math::BigFloat->bone('-'); # create a -1
2542 $x->is_zero(); # true if arg is +0
2543 $x->is_nan(); # true if arg is NaN
2544 $x->is_one(); # true if arg is +1
2545 $x->is_one('-'); # true if arg is -1
2546 $x->is_odd(); # true if odd, false for even
2547 $x->is_even(); # true if even, false for odd
2548 $x->is_pos(); # true if >= 0
2549 $x->is_neg(); # true if < 0
2550 $x->is_inf(sign); # true if +inf, or -inf (default is '+')
2552 $x->bcmp($y); # compare numbers (undef,<0,=0,>0)
2553 $x->bacmp($y); # compare absolutely (undef,<0,=0,>0)
2554 $x->sign(); # return the sign, either +,- or NaN
2555 $x->digit($n); # return the nth digit, counting from right
2556 $x->digit(-$n); # return the nth digit, counting from left
2558 # The following all modify their first argument. If you want to preserve
2559 # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is
2560 # necessary when mixing $a = $b assignments with non-overloaded math.
2563 $x->bzero(); # set $i to 0
2564 $x->bnan(); # set $i to NaN
2565 $x->bone(); # set $x to +1
2566 $x->bone('-'); # set $x to -1
2567 $x->binf(); # set $x to inf
2568 $x->binf('-'); # set $x to -inf
2570 $x->bneg(); # negation
2571 $x->babs(); # absolute value
2572 $x->bnorm(); # normalize (no-op)
2573 $x->bnot(); # two's complement (bit wise not)
2574 $x->binc(); # increment x by 1
2575 $x->bdec(); # decrement x by 1
2577 $x->badd($y); # addition (add $y to $x)
2578 $x->bsub($y); # subtraction (subtract $y from $x)
2579 $x->bmul($y); # multiplication (multiply $x by $y)
2580 $x->bdiv($y); # divide, set $x to quotient
2581 # return (quo,rem) or quo if scalar
2583 $x->bmod($y); # modulus ($x % $y)
2584 $x->bpow($y); # power of arguments ($x ** $y)
2585 $x->blsft($y); # left shift
2586 $x->brsft($y); # right shift
2587 # return (quo,rem) or quo if scalar
2589 $x->blog(); # logarithm of $x to base e (Euler's number)
2590 $x->blog($base); # logarithm of $x to base $base (f.i. 2)
2592 $x->band($y); # bit-wise and
2593 $x->bior($y); # bit-wise inclusive or
2594 $x->bxor($y); # bit-wise exclusive or
2595 $x->bnot(); # bit-wise not (two's complement)
2597 $x->bsqrt(); # calculate square-root
2598 $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root)
2599 $x->bfac(); # factorial of $x (1*2*3*4*..$x)
2601 $x->bround($N); # accuracy: preserve $N digits
2602 $x->bfround($N); # precision: round to the $Nth digit
2604 $x->bfloor(); # return integer less or equal than $x
2605 $x->bceil(); # return integer greater or equal than $x
2607 # The following do not modify their arguments:
2609 bgcd(@values); # greatest common divisor
2610 blcm(@values); # lowest common multiplicator
2612 $x->bstr(); # return string
2613 $x->bsstr(); # return string in scientific notation
2615 $x->as_int(); # return $x as BigInt
2616 $x->exponent(); # return exponent as BigInt
2617 $x->mantissa(); # return mantissa as BigInt
2618 $x->parts(); # return (mantissa,exponent) as BigInt
2620 $x->length(); # number of digits (w/o sign and '.')
2621 ($l,$f) = $x->length(); # number of digits, and length of fraction
2623 $x->precision(); # return P of $x (or global, if P of $x undef)
2624 $x->precision($n); # set P of $x to $n
2625 $x->accuracy(); # return A of $x (or global, if A of $x undef)
2626 $x->accuracy($n); # set A $x to $n
2628 # these get/set the appropriate global value for all BigFloat objects
2629 Math::BigFloat->precision(); # Precision
2630 Math::BigFloat->accuracy(); # Accuracy
2631 Math::BigFloat->round_mode(); # rounding mode
2635 All operators (including basic math operations) are overloaded if you
2636 declare your big floating point numbers as
2638 $i = new Math::BigFloat '12_3.456_789_123_456_789E-2';
2640 Operations with overloaded operators preserve the arguments, which is
2641 exactly what you expect.
2643 =head2 Canonical notation
2645 Input to these routines are either BigFloat objects, or strings of the
2646 following four forms:
2660 C</^[+-]\d+E[+-]?\d+$/>
2664 C</^[+-]\d*\.\d+E[+-]?\d+$/>
2668 all with optional leading and trailing zeros and/or spaces. Additionally,
2669 numbers are allowed to have an underscore between any two digits.
2671 Empty strings as well as other illegal numbers results in 'NaN'.
2673 bnorm() on a BigFloat object is now effectively a no-op, since the numbers
2674 are always stored in normalized form. On a string, it creates a BigFloat
2679 Output values are BigFloat objects (normalized), except for bstr() and bsstr().
2681 The string output will always have leading and trailing zeros stripped and drop
2682 a plus sign. C<bstr()> will give you always the form with a decimal point,
2683 while C<bsstr()> (s for scientific) gives you the scientific notation.
2685 Input bstr() bsstr()
2687 ' -123 123 123' '-123123123' '-123123123E0'
2688 '00.0123' '0.0123' '123E-4'
2689 '123.45E-2' '1.2345' '12345E-4'
2690 '10E+3' '10000' '1E4'
2692 Some routines (C<is_odd()>, C<is_even()>, C<is_zero()>, C<is_one()>,
2693 C<is_nan()>) return true or false, while others (C<bcmp()>, C<bacmp()>)
2694 return either undef, <0, 0 or >0 and are suited for sort.
2696 Actual math is done by using the class defined with C<with => Class;> (which
2697 defaults to BigInts) to represent the mantissa and exponent.
2699 The sign C</^[+-]$/> is stored separately. The string 'NaN' is used to
2700 represent the result when input arguments are not numbers, as well as
2701 the result of dividing by zero.
2703 =head2 C<mantissa()>, C<exponent()> and C<parts()>
2705 C<mantissa()> and C<exponent()> return the said parts of the BigFloat
2706 as BigInts such that:
2708 $m = $x->mantissa();
2709 $e = $x->exponent();
2710 $y = $m * ( 10 ** $e );
2711 print "ok\n" if $x == $y;
2713 C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them.
2715 A zero is represented and returned as C<0E1>, B<not> C<0E0> (after Knuth).
2717 Currently the mantissa is reduced as much as possible, favouring higher
2718 exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0).
2719 This might change in the future, so do not depend on it.
2721 =head2 Accuracy vs. Precision
2723 See also: L<Rounding|Rounding>.
2725 Math::BigFloat supports both precision (rounding to a certain place before or
2726 after the dot) and accuracy (rounding to a certain number of digits). For a
2727 full documentation, examples and tips on these topics please see the large
2728 section about rounding in L<Math::BigInt>.
2730 Since things like C<sqrt(2)> or C<1 / 3> must presented with a limited
2731 accuracy lest a operation consumes all resources, each operation produces
2732 no more than the requested number of digits.
2734 If there is no gloabl precision or accuracy set, B<and> the operation in
2735 question was not called with a requested precision or accuracy, B<and> the
2736 input $x has no accuracy or precision set, then a fallback parameter will
2737 be used. For historical reasons, it is called C<div_scale> and can be accessed
2740 $d = Math::BigFloat->div_scale(); # query
2741 Math::BigFloat->div_scale($n); # set to $n digits
2743 The default value for C<div_scale> is 40.
2745 In case the result of one operation has more digits than specified,
2746 it is rounded. The rounding mode taken is either the default mode, or the one
2747 supplied to the operation after the I<scale>:
2749 $x = Math::BigFloat->new(2);
2750 Math::BigFloat->accuracy(5); # 5 digits max
2751 $y = $x->copy()->bdiv(3); # will give 0.66667
2752 $y = $x->copy()->bdiv(3,6); # will give 0.666667
2753 $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667
2754 Math::BigFloat->round_mode('zero');
2755 $y = $x->copy()->bdiv(3,6); # will also give 0.666667
2757 Note that C<< Math::BigFloat->accuracy() >> and C<< Math::BigFloat->precision() >>
2758 set the global variables, and thus B<any> newly created number will be subject
2759 to the global rounding B<immediately>. This means that in the examples above, the
2760 C<3> as argument to C<bdiv()> will also get an accuracy of B<5>.
2762 It is less confusing to either calculate the result fully, and afterwards
2763 round it explicitly, or use the additional parameters to the math
2767 $x = Math::BigFloat->new(2);
2768 $y = $x->copy()->bdiv(3);
2769 print $y->bround(5),"\n"; # will give 0.66667
2774 $x = Math::BigFloat->new(2);
2775 $y = $x->copy()->bdiv(3,5); # will give 0.66667
2782 =item ffround ( +$scale )
2784 Rounds to the $scale'th place left from the '.', counting from the dot.
2785 The first digit is numbered 1.
2787 =item ffround ( -$scale )
2789 Rounds to the $scale'th place right from the '.', counting from the dot.
2793 Rounds to an integer.
2795 =item fround ( +$scale )
2797 Preserves accuracy to $scale digits from the left (aka significant digits)
2798 and pads the rest with zeros. If the number is between 1 and -1, the
2799 significant digits count from the first non-zero after the '.'
2801 =item fround ( -$scale ) and fround ( 0 )
2803 These are effectively no-ops.
2807 All rounding functions take as a second parameter a rounding mode from one of
2808 the following: 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
2810 The default rounding mode is 'even'. By using
2811 C<< Math::BigFloat->round_mode($round_mode); >> you can get and set the default
2812 mode for subsequent rounding. The usage of C<$Math::BigFloat::$round_mode> is
2813 no longer supported.
2814 The second parameter to the round functions then overrides the default
2817 The C<as_number()> function returns a BigInt from a Math::BigFloat. It uses
2818 'trunc' as rounding mode to make it equivalent to:
2823 You can override this by passing the desired rounding mode as parameter to
2826 $x = Math::BigFloat->new(2.5);
2827 $y = $x->as_number('odd'); # $y = 3
2833 $x->accuracy(5); # local for $x
2834 CLASS->accuracy(5); # global for all members of CLASS
2835 # Note: This also applies to new()!
2837 $A = $x->accuracy(); # read out accuracy that affects $x
2838 $A = CLASS->accuracy(); # read out global accuracy
2840 Set or get the global or local accuracy, aka how many significant digits the
2841 results have. If you set a global accuracy, then this also applies to new()!
2843 Warning! The accuracy I<sticks>, e.g. once you created a number under the
2844 influence of C<< CLASS->accuracy($A) >>, all results from math operations with
2845 that number will also be rounded.
2847 In most cases, you should probably round the results explicitly using one of
2848 L<round()>, L<bround()> or L<bfround()> or by passing the desired accuracy
2849 to the math operation as additional parameter:
2851 my $x = Math::BigInt->new(30000);
2852 my $y = Math::BigInt->new(7);
2853 print scalar $x->copy()->bdiv($y, 2); # print 4300
2854 print scalar $x->copy()->bdiv($y)->bround(2); # print 4300
2858 $x->precision(-2); # local for $x, round at the second digit right of the dot
2859 $x->precision(2); # ditto, round at the second digit left of the dot
2861 CLASS->precision(5); # Global for all members of CLASS
2862 # This also applies to new()!
2863 CLASS->precision(-5); # ditto
2865 $P = CLASS->precision(); # read out global precision
2866 $P = $x->precision(); # read out precision that affects $x
2868 Note: You probably want to use L<accuracy()> instead. With L<accuracy> you
2869 set the number of digits each result should have, with L<precision> you
2870 set the place where to round!
2872 =head1 Autocreating constants
2874 After C<use Math::BigFloat ':constant'> all the floating point constants
2875 in the given scope are converted to C<Math::BigFloat>. This conversion
2876 happens at compile time.
2880 perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"'
2882 prints the value of C<2E-100>. Note that without conversion of
2883 constants the expression 2E-100 will be calculated as normal floating point
2886 Please note that ':constant' does not affect integer constants, nor binary
2887 nor hexadecimal constants. Use L<bignum> or L<Math::BigInt> to get this to
2892 Math with the numbers is done (by default) by a module called
2893 Math::BigInt::Calc. This is equivalent to saying:
2895 use Math::BigFloat lib => 'Calc';
2897 You can change this by using:
2899 use Math::BigFloat lib => 'BitVect';
2901 The following would first try to find Math::BigInt::Foo, then
2902 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
2904 use Math::BigFloat lib => 'Foo,Math::BigInt::Bar';
2906 Calc.pm uses as internal format an array of elements of some decimal base
2907 (usually 1e7, but this might be different for some systems) with the least
2908 significant digit first, while BitVect.pm uses a bit vector of base 2, most
2909 significant bit first. Other modules might use even different means of
2910 representing the numbers. See the respective module documentation for further
2913 Please note that Math::BigFloat does B<not> use the denoted library itself,
2914 but it merely passes the lib argument to Math::BigInt. So, instead of the need
2917 use Math::BigInt lib => 'GMP';
2920 you can roll it all into one line:
2922 use Math::BigFloat lib => 'GMP';
2924 It is also possible to just require Math::BigFloat:
2926 require Math::BigFloat;
2928 This will load the necessary things (like BigInt) when they are needed, and
2931 Use the lib, Luke! And see L<Using Math::BigInt::Lite> for more details than
2932 you ever wanted to know about loading a different library.
2934 =head2 Using Math::BigInt::Lite
2936 It is possible to use L<Math::BigInt::Lite> with Math::BigFloat:
2939 use Math::BigFloat with => 'Math::BigInt::Lite';
2941 There is no need to "use Math::BigInt" or "use Math::BigInt::Lite", but you
2942 can combine these if you want. For instance, you may want to use
2943 Math::BigInt objects in your main script, too.
2947 use Math::BigFloat with => 'Math::BigInt::Lite';
2949 Of course, you can combine this with the C<lib> parameter.
2952 use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari';
2954 There is no need for a "use Math::BigInt;" statement, even if you want to
2955 use Math::BigInt's, since Math::BigFloat will needs Math::BigInt and thus
2956 always loads it. But if you add it, add it B<before>:
2960 use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'GMP,Pari';
2962 Notice that the module with the last C<lib> will "win" and thus
2963 it's lib will be used if the lib is available:
2966 use Math::BigInt lib => 'Bar,Baz';
2967 use Math::BigFloat with => 'Math::BigInt::Lite', lib => 'Foo';
2969 That would try to load Foo, Bar, Baz and Calc (in that order). Or in other
2970 words, Math::BigFloat will try to retain previously loaded libs when you
2971 don't specify it onem but if you specify one, it will try to load them.
2973 Actually, the lib loading order would be "Bar,Baz,Calc", and then
2974 "Foo,Bar,Baz,Calc", but independent of which lib exists, the result is the
2975 same as trying the latter load alone, except for the fact that one of Bar or
2976 Baz might be loaded needlessly in an intermediate step (and thus hang around
2977 and waste memory). If neither Bar nor Baz exist (or don't work/compile), they
2978 will still be tried to be loaded, but this is not as time/memory consuming as
2979 actually loading one of them. Still, this type of usage is not recommended due
2982 The old way (loading the lib only in BigInt) still works though:
2985 use Math::BigInt lib => 'Bar,Baz';
2988 You can even load Math::BigInt afterwards:
2992 use Math::BigInt lib => 'Bar,Baz';
2994 But this has the same problems like #5, it will first load Calc
2995 (Math::BigFloat needs Math::BigInt and thus loads it) and then later Bar or
2996 Baz, depending on which of them works and is usable/loadable. Since this
2997 loads Calc unnecc., it is not recommended.
2999 Since it also possible to just require Math::BigFloat, this poses the question
3000 about what libary this will use:
3002 require Math::BigFloat;
3003 my $x = Math::BigFloat->new(123); $x += 123;
3005 It will use Calc. Please note that the call to import() is still done, but
3006 only when you use for the first time some Math::BigFloat math (it is triggered
3007 via any constructor, so the first time you create a Math::BigFloat, the load
3008 will happen in the background). This means:
3010 require Math::BigFloat;
3011 Math::BigFloat->import ( lib => 'Foo,Bar' );
3013 would be the same as:
3015 use Math::BigFloat lib => 'Foo, Bar';
3017 But don't try to be clever to insert some operations in between:
3019 require Math::BigFloat;
3020 my $x = Math::BigFloat->bone() + 4; # load BigInt and Calc
3021 Math::BigFloat->import( lib => 'Pari' ); # load Pari, too
3022 $x = Math::BigFloat->bone()+4; # now use Pari
3024 While this works, it loads Calc needlessly. But maybe you just wanted that?
3026 B<Examples #3 is highly recommended> for daily usage.
3030 Please see the file BUGS in the CPAN distribution Math::BigInt for known bugs.
3036 =item stringify, bstr()
3038 Both stringify and bstr() now drop the leading '+'. The old code would return
3039 '+1.23', the new returns '1.23'. See the documentation in L<Math::BigInt> for
3040 reasoning and details.
3044 The following will probably not do what you expect:
3046 print $c->bdiv(123.456),"\n";
3048 It prints both quotient and reminder since print works in list context. Also,
3049 bdiv() will modify $c, so be careful. You probably want to use
3051 print $c / 123.456,"\n";
3052 print scalar $c->bdiv(123.456),"\n"; # or if you want to modify $c
3056 =item Modifying and =
3060 $x = Math::BigFloat->new(5);
3063 It will not do what you think, e.g. making a copy of $x. Instead it just makes
3064 a second reference to the B<same> object and stores it in $y. Thus anything
3065 that modifies $x will modify $y (except overloaded math operators), and vice
3066 versa. See L<Math::BigInt> for details and how to avoid that.
3070 C<bpow()> now modifies the first argument, unlike the old code which left
3071 it alone and only returned the result. This is to be consistent with
3072 C<badd()> etc. The first will modify $x, the second one won't:
3074 print bpow($x,$i),"\n"; # modify $x
3075 print $x->bpow($i),"\n"; # ditto
3076 print $x ** $i,"\n"; # leave $x alone
3078 =item precision() vs. accuracy()
3080 A common pitfall is to use L<precision()> when you want to round a result to
3081 a certain number of digits:
3085 Math::BigFloat->precision(4); # does not do what you think it does
3086 my $x = Math::BigFloat->new(12345); # rounds $x to "12000"!
3087 print "$x\n"; # print "12000"
3088 my $y = Math::BigFloat->new(3); # rounds $y to "0"!
3089 print "$y\n"; # print "0"
3090 $z = $x / $y; # 12000 / 0 => NaN!
3092 print $z->precision(),"\n"; # 4
3094 Replacing L<precision> with L<accuracy> is probably not what you want, either:
3098 Math::BigFloat->accuracy(4); # enables global rounding:
3099 my $x = Math::BigFloat->new(123456); # rounded immediately to "12350"
3100 print "$x\n"; # print "123500"
3101 my $y = Math::BigFloat->new(3); # rounded to "3
3102 print "$y\n"; # print "3"
3103 print $z = $x->copy()->bdiv($y),"\n"; # 41170
3104 print $z->accuracy(),"\n"; # 4
3106 What you want to use instead is:
3110 my $x = Math::BigFloat->new(123456); # no rounding
3111 print "$x\n"; # print "123456"
3112 my $y = Math::BigFloat->new(3); # no rounding
3113 print "$y\n"; # print "3"
3114 print $z = $x->copy()->bdiv($y,4),"\n"; # 41150
3115 print $z->accuracy(),"\n"; # undef
3117 In addition to computing what you expected, the last example also does B<not>
3118 "taint" the result with an accuracy or precision setting, which would
3119 influence any further operation.
3125 L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well as
3126 L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
3128 The pragmas L<bignum>, L<bigint> and L<bigrat> might also be of interest
3129 because they solve the autoupgrading/downgrading issue, at least partly.
3132 L<http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains
3133 more documentation including a full version history, testcases, empty
3134 subclass files and benchmarks.
3138 This program is free software; you may redistribute it and/or modify it under
3139 the same terms as Perl itself.
3143 Mark Biggar, overloaded interface by Ilya Zakharevich.
3144 Completely rewritten by Tels L<http://bloodgate.com> in 2001 - 2004, and still