Lines Matching refs:x
214 my ($c,$x);
218 ($c,$x) = @_;
222 $x = shift;
223 $c = ref($x);
225 return unless ref($x); # only for objects
229 $self->{sign} = $x->{sign};
230 $self->{_es} = $x->{_es};
231 $self->{_m} = $MBI->_copy($x->{_m});
232 $self->{_e} = $MBI->_copy($x->{_e});
233 $self->{_a} = $x->{_a} if defined $x->{_a};
234 $self->{_p} = $x->{_p} if defined $x->{_p};
322 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
324 if ($x->{sign} !~ /^[+-]$/)
326 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
333 my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m}));
336 $es = $MBI->_str($x->{_m});
338 my $e = $MBI->_num($x->{_e});
339 $e = -$e if $x->{_es} eq '-';
351 substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e});
352 $cad = -$cad if $x->{_es} eq '-';
362 $es = '-'.$es if $x->{sign} eq '-';
364 if ((defined $x->{_a}) && ($not_zero))
367 my $zeros = $x->{_a} - $cad; # cad == 0 => 12340
368 $zeros = $x->{_a} - $len if $cad != $len;
371 elsif ((($x->{_p} || 0) < 0))
374 my $zeros = -$x->{_p} + $cad;
385 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
387 if ($x->{sign} !~ /^[+-]$/)
389 return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
392 my $sep = 'e'.$x->{_es};
393 my $sign = $x->{sign}; $sign = '' if $sign eq '+';
394 $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e});
401 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
402 $x->bsstr();
421 my ($self,$x,$y) = (ref($_[0]),@_);
425 ($self,$x,$y) = objectify(2,@_);
428 return $upgrade->bcmp($x,$y) if defined $upgrade &&
429 ((!$x->isa($self)) || (!$y->isa($self)));
431 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
434 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
435 return 0 if ($x->{sign} eq $y->{sign}) && ($x->{sign} =~ /^[+-]inf$/);
436 return +1 if $x->{sign} eq '+inf';
437 return -1 if $x->{sign} eq '-inf';
443 return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y
444 return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0
447 my $xz = $x->is_zero();
451 return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
454 my $lxm = $MBI->_len($x->{_m});
458 $xes = -1 if $x->{_es} ne '+';
460 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
462 my $l = $lx - $ly; $l = -$l if $x->{sign} eq '-';
468 my $xm = $x->{_m}; # not yet copy it
477 $xm = $MBI->_copy($x->{_m});
481 $rc = -$rc if $x->{sign} eq '-'; # -124 < -123
491 my ($self,$x,$y) = (ref($_[0]),@_);
495 ($self,$x,$y) = objectify(2,@_);
498 return $upgrade->bacmp($x,$y) if defined $upgrade &&
499 ((!$x->isa($self)) || (!$y->isa($self)));
502 if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/)
504 return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
505 return 0 if ($x->is_inf() && $y->is_inf());
506 return 1 if ($x->is_inf() && !$y->is_inf());
511 my $xz = $x->is_zero();
518 my $lxm = $MBI->_len($x->{_m});
521 $xes = -1 if $x->{_es} ne '+';
524 my $lx = $lxm + $xes * $MBI->_num($x->{_e});
532 my $xm = $x->{_m}; # not yet copy it
541 $xm = $MBI->_copy($x->{_m});
553 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
557 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
561 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
564 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
566 if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/))
569 return $x if $x->{sign} eq $y->{sign};
570 return $x->bnan();
573 $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/;
574 return $x;
577 return $upgrade->badd($x,$y,$a,$p,$r) if defined $upgrade &&
578 ((!$x->isa($self)) || (!$y->isa($self)));
581 return $x->bround($a,$p,$r) if $y->is_zero(); # x+0
582 if ($x->is_zero()) # 0+y
585 $x->{_e} = $MBI->_copy($y->{_e});
586 $x->{_es} = $y->{_es};
587 $x->{_m} = $MBI->_copy($y->{_m});
588 $x->{sign} = $y->{sign} || $nan;
589 return $x->round($a,$p,$r,$y);
599 ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es});
605 $MBI->_lsft( $x->{_m}, $e, 10);
606 ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es);
614 if ($x->{sign} eq $y->{sign})
617 $x->{_m} = $MBI->_add($x->{_m}, $add);
621 ($x->{_m}, $x->{sign}) =
622 _e_add($x->{_m}, $add, $x->{sign}, $y->{sign});
626 $x->bnorm()->round($a,$p,$r,$y);
635 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
639 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
644 return $x->round($a,$p,$r);
649 $x->badd($y,$a,$p,$r); # badd does not leave internal zeros
651 $x; # already rounded by badd()
657 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
659 if ($x->{_es} eq '-')
661 return $x->badd($self->bone(),@r); # digits after dot
664 if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf
667 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
668 $x->{_e} = $MBI->_zero(); # normalize
669 $x->{_es} = '+';
674 if ($x->{sign} eq '+')
676 $MBI->_inc($x->{_m});
677 return $x->bnorm()->bround(@r);
679 elsif ($x->{sign} eq '-')
681 $MBI->_dec($x->{_m});
682 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
683 return $x->bnorm()->bround(@r);
686 $x->badd($self->bone(),@r); # badd() does round
692 my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
694 if ($x->{_es} eq '-')
696 return $x->badd($self->bone('-'),@r); # digits after dot
699 if (!$MBI->_is_zero($x->{_e}))
701 $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100
702 $x->{_e} = $MBI->_zero(); # normalize
703 $x->{_es} = '+';
706 my $zero = $x->is_zero();
708 if (($x->{sign} eq '-') || $zero)
710 $MBI->_inc($x->{_m});
711 $x->{sign} = '-' if $zero; # 0 => 1 => -1
712 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0
713 return $x->bnorm()->round(@r);
716 elsif ($x->{sign} eq '+')
718 $MBI->_dec($x->{_m});
719 return $x->bnorm()->round(@r);
722 $x->badd($self->bone('-'),@r); # does round
729 my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
737 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
740 return $x->bnan() if $x->{sign} ne '+' || $x->is_zero();
760 return $x->bzero(@params) if $x->is_one();
768 return $x->bnan() if $base->is_zero() || $base->is_one() ||
771 return $x->bone('+',@params) if $x->bcmp($base) == 0;
781 delete $x->{_a}; delete $x->{_p};
787 if (!$x->isa('Math::BigFloat'))
789 $x = Math::BigFloat->new($x);
790 $self = ref($x);
798 if (defined $base && $base->is_int() && $x->is_int())
800 my $i = $MBI->_copy( $x->{_m} );
801 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
806 if ($base->as_number()->bpow($int) == $x)
809 $x->{_m} = $int->{value};
810 $x->{_e} = $MBI->_zero();
811 $x->{_es} = '+';
812 $x->bnorm();
820 $self->_log_10($x,$scale);
827 $x->bdiv( $base->copy()->blog(undef,$scale), $scale );
834 $x->bround($params[0],$params[2]); # then round accordingly
838 $x->bfround($params[1],$params[2]); # then round accordingly
843 delete $x->{_a}; delete $x->{_p};
848 $x;
855 my ($self,$x,$scale) = @_;
858 return $x->bzero() if $x->is_one();
877 $v = $x->copy(); $v->binc(); # v = x+1
878 $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1
879 $x->bdiv($v,$scale); # first term: u/v
917 $x->badd($next);
922 $steps++; print "step $steps = $x\n" if $steps % 10 == 0;
925 $x->bmul($f); # $x *= 2
933 my ($self,$x,$scale) = @_;
948 my $dbd = $MBI->_num($x->{_e});
949 $dbd = -$dbd if $x->{_es} eq '-';
950 $dbd += $MBI->_len($x->{_m});
959 if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m}))
965 $x->bzero(); $x->badd($LOG_10);
972 if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m})))
978 $x->bzero(); $x->badd($LOG_2);
985 if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) &&
986 $MBI->_is_one($x->{_m}))
992 $x->bzero(); $x->bsub($LOG_10);
1037 ($x->{_e}, $x->{_es}) =
1038 _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23
1051 while ($x->bacmp($HALF) <= 0)
1053 $twos--; $x->bmul($two);
1055 while ($x->bacmp($two) >= 0)
1057 $twos++; $x->bdiv($two,$scale+4); # keep all digits
1079 $self->_log($x,$scale); # need to do the "normal" way
1080 $x->badd($l_10) if defined $l_10; # correct it by ln(10)
1081 $x->badd($l_2) if defined $l_2; # and maybe by ln(2)
1092 my $x = $self->new(shift @arg);
1093 while (@arg) { $x = _lcm($x,shift @arg); }
1094 $x;
1104 my $x = $self->new(shift @arg);
1105 while (@arg) { $x = _gcd($x,shift @arg); }
1106 $x;
1116 my ($x,$y,$xs,$ys) = @_;
1121 $x = $MBI->_add ($x, $y ); # a+b
1123 return ($x, $xs);
1126 my $a = $MBI->_acmp($x,$y);
1129 $x = $MBI->_sub ($x , $y); # abs sub
1133 $x = $MBI->_zero(); # result is 0
1138 $x = $MBI->_sub ( $y, $x, 1 ); # abs sub
1141 ($x,$xs);
1149 my ($x,$y,$xs,$ys) = @_;
1153 _e_add($x,$y,$xs,$ys); # call add (does subtract now)
1162 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1164 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
1165 $x->{_es} eq '+'; # 1e-1 => no integer
1172 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1174 return 1 if $x->{sign} eq '+' && $MBI->_is_zero($x->{_m});
1181 my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_);
1185 if ($x->{sign} eq $sign &&
1186 $MBI->_is_zero($x->{_e}) && $MBI->_is_one($x->{_m}));
1193 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1195 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1196 ($MBI->_is_zero($x->{_e}) && $MBI->_is_odd($x->{_m}));
1203 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1205 return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
1206 return 1 if ($x->{_es} eq '+' # 123.45 is never
1207 && $MBI->_is_even($x->{_m})); # but 1200 is
1217 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1221 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1224 return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
1227 if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/))
1229 return $x->bnan() if $x->is_zero() || $y->is_zero();
1233 return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
1234 return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
1235 return $x->binf('-');
1238 return $x->bzero() if $x->is_zero() || $y->is_zero();
1240 return $upgrade->bmul($x,$y,$a,$p,$r) if defined $upgrade &&
1241 ((!$x->isa($self)) || (!$y->isa($self)));
1244 $MBI->_mul($x->{_m},$y->{_m});
1245 ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1248 $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
1249 return $x->bnorm()->round($a,$p,$r,$y);
1258 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1262 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1265 return $self->_div_inf($x,$y)
1266 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
1269 return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero();
1272 return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade;
1277 ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y);
1279 return $x if $x->is_nan(); # error in _find_round_parameters?
1296 my $lx = $MBI->_len($x->{_m}); my $ly = $MBI->_len($y->{_m});
1306 $rem = $x->copy();
1309 $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+';
1319 $MBI->_lsft($x->{_m},$MBI->_new($scale),10);
1320 $MBI->_div ($x->{_m},$y->{_m} ); # a/c
1322 ($x->{_e},$x->{_es}) =
1323 _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es});
1325 ($x->{_e},$x->{_es}) =
1326 _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+');
1327 $x->bnorm(); # remove trailing 0's
1333 delete $x->{_a}; # clear before round
1334 $x->bround($params[0],$params[2]); # then round accordingly
1338 delete $x->{_p}; # clear before round
1339 $x->bfround($params[1],$params[2]); # then round accordingly
1344 delete $x->{_a}; delete $x->{_p};
1362 return ($x,$rem);
1364 $x;
1372 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1376 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1380 if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
1382 my ($d,$re) = $self->SUPER::_div_inf($x,$y);
1383 $x->{sign} = $re->{sign};
1384 $x->{_e} = $re->{_e};
1385 $x->{_m} = $re->{_m};
1386 return $x->round($a,$p,$r,$y);
1390 return $x->bnan() if $x->is_zero();
1391 return $x;
1393 return $x->bzero() if $y->is_one() || $x->is_zero();
1395 my $cmp = $x->bacmp($y); # equal or $x < $y?
1396 return $x->bzero($a,$p) if $cmp == 0; # $x == $y => result 0
1399 my $neg = 0; $neg = 1 if $x->{sign} ne $y->{sign};
1401 $x->{sign} = $y->{sign}; # calc sign first
1402 return $x->round($a,$p,$r) if $cmp < 0 && $neg == 0; # $x < $y => result $x
1416 $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25
1421 if ($x->{_es} eq '-') # has digits after dot
1424 $shiftx = $MBI->_num($x->{_e}); # no more digits after dot
1425 $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230
1428 if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e}))
1430 $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here
1433 $x->{_e} = $MBI->_new($shiftx);
1434 $x->{_es} = '+';
1435 $x->{_es} = '-' if $shiftx != 0 || $shifty != 0;
1436 $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0;
1440 $x->{_m} = $MBI->_mod( $x->{_m}, $ym);
1442 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1443 $x->bnorm();
1447 my $r = $y - $x;
1448 $x->{_m} = $r->{_m};
1449 $x->{_e} = $r->{_e};
1450 $x->{_es} = $r->{_es};
1451 $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0
1452 $x->bnorm();
1455 $x->round($a,$p,$r,$y); # round and return
1463 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1467 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1471 return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() ||
1474 return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one();
1479 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1481 return $x if $x->is_nan(); # error in _find_round_parameters?
1506 delete $x->{_a}; delete $x->{_p};
1511 my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+';
1526 $x->bsqrt($scale+4);
1531 my $u = $self->bone()->bdiv($x,$scale);
1533 $x->{_m} = $u->{_m};
1534 $x->{_e} = $u->{_e};
1535 $x->{_es} = $u->{_es};
1543 if ($y->is_int() && $x->is_int())
1545 my $i = $MBI->_copy( $x->{_m} );
1546 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
1551 if ($int->copy()->bpow($y) == $x)
1554 $x->{_m} = $int->{value};
1555 $x->{_e} = $MBI->_zero();
1556 $x->{_es} = '+';
1557 $x->bnorm();
1565 $x->bpow($u,$scale+4); # el cheapo
1568 $x->bneg() if $sign == 1;
1573 $x->bround($params[0],$params[2]); # then round accordingly
1577 $x->bfround($params[1],$params[2]); # then round accordingly
1582 delete $x->{_a}; delete $x->{_p};
1586 $x;
1592 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
1594 return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0
1595 return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf
1596 return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one();
1601 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1603 return $x if $x->is_nan(); # error in _find_round_parameters?
1628 delete $x->{_a}; delete $x->{_p};
1632 my $i = $MBI->_copy( $x->{_m} );
1633 $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e});
1639 if (($x->{_es} ne '-') # guess can't be accurate if there are
1644 $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+';
1645 $x->bnorm();
1649 $x->bround($params[0],$params[2]); # then round accordingly
1653 $x->bfround($params[1],$params[2]); # then round accordingly
1658 delete $x->{_a}; delete $x->{_p};
1662 return $x;
1670 my $y1 = $MBI->_copy($x->{_m});
1692 $s2++ if $MBI->_is_odd($x->{_e});
1704 my $dat = $MBI->_num($x->{_e});
1705 $dat = -$dat if $x->{_es} eq '-';
1723 $x->{_e} = $MBI->_new( $dat );
1724 $x->{_es} = '-';
1728 $x->{_e} = $MBI->_new( $dat );
1729 $x->{_es} = '+';
1731 $x->{_m} = $y1;
1732 $x->bnorm();
1737 $x->bround($params[0],$params[2]); # then round accordingly
1741 $x->bfround($params[1],$params[2]); # then round accordingly
1746 delete $x->{_a}; delete $x->{_p};
1750 $x;
1759 my ($self,$x,@r) = (ref($_[0]),@_);
1761 ($self,$x,@r) = objectify(1,@_) if !ref($x);
1763 return $x if $x->{sign} eq '+inf'; # inf => inf
1764 return $x->bnan()
1765 if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN
1766 ($x->{_es} ne '+')); # digits after dot?
1769 if (! $MBI->_is_zero($x->{_e}))
1771 $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0
1772 $x->{_e} = $MBI->_zero(); # normalize
1773 $x->{_es} = '+';
1775 $MBI->_fac($x->{_m}); # calculate factorial
1776 $x->bnorm()->round(@r); # norm again and round result
1782 my ($x,$y,$a,$p,$r) = @_;
1783 my $self = ref($x);
1787 return $x->bsqrt($a,$p,$r,$y) if $y->bcmp($HALF) == 0;
1801 ($x,@params) = $x->_find_round_parameters($a,$p,$r);
1803 return $x if $x->is_nan(); # error in _find_round_parameters?
1829 delete $x->{_a}; delete $x->{_p};
1835 $u = $x->copy()->blog(undef,$scale)->bmul($y);
1838 $x->bone(); # first term: 1
1852 $x->badd($next);
1856 last if $x->{sign} !~ /^[-+]$/;
1864 $x->bround($params[0],$params[2]); # then round accordingly
1868 $x->bfround($params[1],$params[2]); # then round accordingly
1873 delete $x->{_a}; delete $x->{_p};
1877 $x;
1887 my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_);
1891 ($self,$x,$y,$a,$p,$r) = objectify(2,@_);
1894 return $x if $x->{sign} =~ /^[+-]inf$/;
1895 return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
1896 return $x->bone() if $y->is_zero();
1897 return $x if $x->is_one() || $y->is_one();
1899 return $x->_pow($y,$a,$p,$r) if !$y->is_int(); # non-integer power
1904 if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e}))
1907 return $MBI->_is_odd($y1) ? $x : $x->babs(1);
1909 if ($x->is_zero())
1911 return $x->bone() if $y->is_zero();
1912 return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0)
1914 return $x->binf();
1918 $new_sign = $y->is_odd() ? '-' : '+' if ($x->{sign} ne '+');
1921 $x->{_m} = $MBI->_pow( $x->{_m}, $y1);
1922 $MBI->_mul ($x->{_e}, $y1);
1924 $x->{sign} = $new_sign;
1925 $x->bnorm();
1929 my $z = $x->copy(); $x->bzero()->binc();
1930 return $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!)
1932 $x->round($a,$p,$r,$y);
1943 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
1945 return $x if $x->modify('bfround');
1947 my ($scale,$mode) = $x->_scale_p($self->precision(),$self->round_mode(),@_);
1948 return $x if !defined $scale; # no-op
1951 if ($x->is_zero())
1953 $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2
1954 return $x;
1956 return $x if $x->{sign} !~ /^[+-]$/;
1959 return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p});
1961 $x->{_p} = $scale; # remember round in any case
1962 delete $x->{_a}; # and clear A
1967 return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round
1970 my $len = $MBI->_len($x->{_m}); # length of mantissa
1974 my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot
1987 return $x if $scale > $dad; # 0.123, scale >= 3 => exit
1991 return $x->bzero() if $scale < $zad;
2016 my $dbt = $MBI->_len($x->{_m});
2018 my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e}));
2022 return $x if $scale == 1 && $dbt <= $dbd;
2029 return $x->bzero;
2042 my $m = Math::BigInt->new( $x->{sign} . $MBI->_str($x->{_m}));
2044 $x->{_m} = $m->{value}; # get our mantissa back
2045 $x->bnorm();
2051 my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x);
2058 my ($scale,$mode) = $x->_scale_a($self->accuracy(),$self->round_mode(),@_);
2059 return $x if !defined $scale; # no-op
2061 return $x if $x->modify('bround');
2067 return $x if defined $_[0] && defined $x->{_a} && $x->{_a} < $_[0];
2071 return $x if ($scale < 0) || $x->{sign} !~ /^[+-]$/;
2076 if ($scale == 0 || $x->is_zero() || $MBI->_len($x->{_m}) <= $scale)
2078 $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale;
2079 return $x;
2083 my $m = Math::BigInt->new( $x->{sign} . $MBI->_str($x->{_m}));
2086 $x->{_m} = $m->{value}; # get our mantissa back
2087 $x->{_a} = $scale; # remember rounding
2088 delete $x->{_p}; # and clear P
2089 $x->bnorm(); # del trailing zeros gen. by bround()
2095 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2097 return $x if $x->modify('bfloor');
2099 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2102 if ($x->{_es} eq '-')
2104 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
2105 $x->{_e} = $MBI->_zero(); # trunc/norm
2106 $x->{_es} = '+'; # abs e
2107 $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative
2109 $x->round($a,$p,$r);
2115 my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
2117 return $x if $x->modify('bceil');
2118 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2121 if ($x->{_es} eq '-')
2123 $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot
2124 $x->{_e} = $MBI->_zero(); # trunc/norm
2125 $x->{_es} = '+'; # abs e
2126 $MBI->_inc($x->{_m}) if $x->{sign} eq '+'; # increment if positive
2128 $x->round($a,$p,$r);
2136 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
2140 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
2143 return $x if $x->modify('brsft');
2144 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2147 $x->bdiv($n->bpow($y),$a,$p,$r,$y);
2155 my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_);
2159 ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_);
2162 return $x if $x->modify('blsft');
2163 return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf
2166 $x->bmul($n->bpow($y),$a,$p,$r,$y);
2213 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2215 if ($x->{sign} !~ /^[+-]$/)
2217 my $s = $x->{sign}; $s =~ s/^[+-]//;
2220 Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e}));
2226 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2228 if ($x->{sign} !~ /^[+-]$/)
2230 my $s = $x->{sign}; $s =~ s/^[+]//;
2233 my $m = Math::BigInt->new( $MBI->_str($x->{_m}));
2234 $m->bneg() if $x->{sign} eq '-';
2242 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2244 if ($x->{sign} !~ /^[+-]$/)
2246 my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//;
2250 $m->{value} = $MBI->_copy($x->{_m});
2251 $m->bneg() if $x->{sign} eq '-';
2252 ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) ));
2345 my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
2347 return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2349 my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros
2353 $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10);
2354 if ($x->{_es} eq '-')
2356 if ($MBI->_acmp($x->{_e},$z) >= 0)
2358 $x->{_e} = $MBI->_sub ($x->{_e}, $z);
2359 $x->{_es} = '+' if $MBI->_is_zero($x->{_e});
2363 $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e});
2364 $x->{_es} = '+';
2369 $x->{_e} = $MBI->_add ($x->{_e}, $z);
2376 $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one()
2377 if $MBI->_is_zero($x->{_m});
2380 $x; # MBI bnorm is no-op, so dont call it
2388 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2390 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2391 return '0x0' if $x->is_zero();
2393 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
2395 my $z = $MBI->_copy($x->{_m});
2396 if (! $MBI->_is_zero($x->{_e})) # > 0
2398 $MBI->_lsft( $z, $x->{_e},10);
2400 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2407 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2409 return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
2410 return '0b0' if $x->is_zero();
2412 return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!?
2414 my $z = $MBI->_copy($x->{_m});
2415 if (! $MBI->_is_zero($x->{_e})) # > 0
2417 $MBI->_lsft( $z, $x->{_e},10);
2419 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2426 my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
2428 my $z = $MBI->_copy($x->{_m});
2429 if ($x->{_es} eq '-') # < 0
2431 $MBI->_rsft( $z, $x->{_e},10);
2433 elsif (! $MBI->_is_zero($x->{_e})) # > 0
2435 $MBI->_lsft( $z, $x->{_e},10);
2437 $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z));
2443 my $x = shift;
2444 my $class = ref($x) || $x;
2445 $x = $class->new(shift) unless ref($x);
2447 return 1 if $MBI->_is_zero($x->{_m});
2449 my $len = $MBI->_len($x->{_m});
2450 $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+';
2454 $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-';