cpan/Math-BigInt/t/const_mbf.t Test Math::BigInt
cpan/Math-BigInt/t/downgrade.t Test if use Math::BigInt(); under downgrade works
cpan/Math-BigInt/t/_e_math.t Helper routine in BigFloat for _e math
+cpan/Math-BigInt/t/from_hex-mbf.t Test Math::BigInt
cpan/Math-BigInt/t/inf_nan.t Special tests for inf and *NaN* handling
cpan/Math-BigInt/t/isa.t Test for Math::BigInt inheritance
cpan/Math-BigInt/t/lib_load.t Test sane lib names
},
'Math::BigInt' => {
- 'DISTRIBUTION' => 'PJACKLAM/Math-BigInt-1.999707.tar.gz',
+ 'DISTRIBUTION' => 'PJACKLAM/Math-BigInt-1.999710.tar.gz',
'FILES' => q[cpan/Math-BigInt],
'EXCLUDED' => [
qr{^inc/},
use strict;
use warnings;
-our $VERSION = '1.999707';
+our $VERSION = '1.999710';
require Exporter;
our @ISA = qw/Math::BigInt/;
}
$self->{sign} = $$mis;
- # for something like 0Ey, set y to 1, and -0 => +0
+ # for something like 0Ey, set y to 0, and -0 => +0
# Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not
# have become 0. That's faster than to call $MBI->_is_zero().
- $self->{sign} = '+', $self->{_e} = $MBI->_one()
+ $self->{sign} = '+', $self->{_e} = $MBI->_zero()
if $$miv eq '0' and $$mfv eq '';
return $self->round(@r) if !$downgrade;
sub _bzero
{
- # used by parent class bone() to initialize number to 0
+ # used by parent class bzero() to initialize number to 0
my $self = shift;
$IMPORT=1; # call our import only once
$self->{_m} = $MBI->_zero();
- $self->{_e} = $MBI->_one();
+ $self->{_e} = $MBI->_zero();
$self->{_es} = '+';
}
$len;
}
+sub from_hex {
+ my $self = shift;
+ my $selfref = ref $self;
+ my $class = $selfref || $self;
+
+ my $str = shift;
+
+ $self = $class -> bzero() unless $selfref;
+
+ if ($str =~ s/
+ ^
+
+ # sign
+ ( [+-]? )
+
+ # optional "hex marker"
+ (?: 0? x )?
+
+ # significand using the hex digits 0..9 and a..f
+ (
+ [0-9a-fA-F]+ (?: _ [0-9a-fA-F]+ )*
+ (?:
+ \.
+ (?: [0-9a-fA-F]+ (?: _ [0-9a-fA-F]+ )* )?
+ )?
+ |
+ \.
+ [0-9a-fA-F]+ (?: _ [0-9a-fA-F]+ )*
+ )
+
+ # exponent (power of 2) using decimal digits
+ (?:
+ [Pp]
+ ( [+-]? )
+ ( \d+ (?: _ \d+ )* )
+ )?
+
+ $
+ //x)
+ {
+ my $s_sign = $1 || '+';
+ my $s_value = $2;
+ my $e_sign = $3 || '+';
+ my $e_value = $4 || '0';
+ $s_value =~ tr/_//d;
+ $e_value =~ tr/_//d;
+
+ # The significand must be multiplied by 2 raised to this exponent.
+
+ my $two_expon = $class -> new($e_value);
+ $two_expon -> bneg() if $e_sign eq '-';
+
+ # If there is a dot in the significand, remove it and adjust the
+ # exponent according to the number of digits in the fraction part of
+ # the significand. Multiply the exponent adjustment value by 4 since
+ # the digits in the significand are in base 16, but the exponent is
+ # only in base 2.
+
+ my $idx = index($s_value, '.');
+ if ($idx >= 0) {
+ substr($s_value, $idx, 1) = '';
+ $two_expon -= $class -> new(CORE::length($s_value))
+ -> bsub($idx)
+ -> bmul("4");
+ }
+
+ $self -> {sign} = $s_sign;
+ $self -> {_m} = $MBI -> _from_hex('0x' . $s_value);
+
+ if ($two_expon > 0) {
+ my $factor = $class -> new("2") -> bpow($two_expon);
+ $self -> bmul($factor);
+ } elsif ($two_expon < 0) {
+ my $factor = $class -> new("0.5") -> bpow(-$two_expon);
+ $self -> bmul($factor);
+ }
+
+ return $self;
+ }
+
+ return $self->bnan();
+}
+
1;
__END__
my $inf = Math::BigFloat->binf('-'); # create a -inf
my $one = Math::BigFloat->bone(); # create a +1
my $mone = Math::BigFloat->bone('-'); # create a -1
+ my $x = Math::BigFloat->bone('-'); #
+
+ my $h = Math::BigFloat->from_hex('0xc.afep+3'); # from hexadecimal
my $pi = Math::BigFloat->bpi(100); # PI to 100 digits
C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them.
-A zero is represented and returned as C<0E1>, B<not> C<0E0> (after Knuth).
-
Currently the mantissa is reduced as much as possible, favouring higher
exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0).
This might change in the future, so do not depend on it.
=over
-=item ffround ( +$scale )
+=item bfround ( +$scale )
Rounds to the $scale'th place left from the '.', counting from the dot.
The first digit is numbered 1.
-=item ffround ( -$scale )
+=item bfround ( -$scale )
Rounds to the $scale'th place right from the '.', counting from the dot.
-=item ffround ( 0 )
+=item bfround ( 0 )
Rounds to an integer.
-=item fround ( +$scale )
+=item bround ( +$scale )
Preserves accuracy to $scale digits from the left (aka significant digits)
and pads the rest with zeros. If the number is between 1 and -1, the
significant digits count from the first non-zero after the '.'
-=item fround ( -$scale ) and fround ( 0 )
+=item bround ( -$scale ) and bround ( 0 )
These are effectively no-ops.
In Math::BigFloat, C<as_float()> has the same effect as C<copy()>.
+=item from_hex()
+
+ $x -> from_hex("0x1.921fb54442d18p+1");
+ $x = Math::BigFloat -> from_hex("0x1.921fb54442d18p+1");
+
+Interpret input as a hexadecimal string. A "0x" or "x" prefix is optional. A
+single underscore character may be placed between any two digits. If the input
+is invalid, a NaN is returned. The exponent is in base 2 using decimal digits.
+
+If called as an instance method, the value is assigned to the invocand.
+
=back
=head1 Autocreating constants
use strict;
use warnings;
-our $VERSION = '1.999707';
+our $VERSION = '1.999710';
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(objectify bgcd blcm);
# This procedure finds the round parameters, but it is for speed reasons
# duplicated in round. Otherwise, it is tested by the testsuite and used
- # by fdiv().
+ # by bdiv().
# returns ($self) or ($self,$a,$p,$r) - sets $self to NaN of both A and P
# were requested/defined (locally or globally or both)
require Carp; Carp::croak ("Unknown round mode '$r'");
}
- # now round, by calling either fround or ffround:
+ # now round, by calling either bround or bfround:
if (defined $a)
{
$self->bround(int($a),$r) if !defined $self->{_a} || $self->{_a} >= $a;
my $s = $x->{sign}; $s =~ s/^[+-]//; # NaN, -inf,+inf => NaN or inf
return $self->new($s);
}
- return $self->bone() if $x->is_zero();
+ return $self->bzero() if $x->is_zero();
# 12300 => 2 trailing zeros => exponent is 2
$self->new( $CALC->_zeros($x->{value}) );
$h = Math::BigInt->new('0x123'); # from hexadecimal
$b = Math::BigInt->new('0b101'); # from binary
$o = Math::BigInt->from_oct('0101'); # from octal
+ $h = Math::BigInt->from_hex('cafe'); # from hexadecimal
+ $b = Math::BigInt->from_bin('0101'); # from binary
# Testing (don't modify their arguments)
# (return true if the condition is met, otherwise false)
=item Precision
- * ffround($p) is able to round to $p number of digits after the decimal
+ * bfround($p) is able to round to $p number of digits after the decimal
point
* otherwise P is unused
=item Accuracy (significant digits)
- * fround($a) rounds to $a significant digits
- * only fdiv() and fsqrt() take A as (optional) parameter
- + other operations simply create the same number (fneg etc), or
- more (fmul) of digits
+ * bround($a) rounds to $a significant digits
+ * only bdiv() and bsqrt() take A as (optional) parameter
+ + other operations simply create the same number (bneg etc), or
+ more (bmul) of digits
+ rounding/truncating is only done when explicitly calling one
- of fround or ffround, and never for BigInt (not implemented)
- * fsqrt() simply hands its accuracy argument over to fdiv.
+ of bround or bfround, and never for BigInt (not implemented)
+ * bsqrt() simply hands its accuracy argument over to bdiv.
* the documentation and the comment in the code indicate two
- different ways on how fdiv() determines the maximum number
+ different ways on how bdiv() determines the maximum number
of digits it should calculate, and the actual code does yet
another thing
POD:
effect, and the other P, this results in an error (NaN).
* A takes precedence over P (Hint: A comes before P).
If neither of them is defined, nothing is used, i.e. the result will have
- as many digits as it can (with an exception for fdiv/fsqrt) and will not
+ as many digits as it can (with an exception for bdiv/bsqrt) and will not
be rounded.
- * There is another setting for fdiv() (and thus for fsqrt()). If neither of
- A or P is defined, fdiv() will use a fallback (F) of $div_scale digits.
+ * There is another setting for bdiv() (and thus for bsqrt()). If neither of
+ A or P is defined, bdiv() will use a fallback (F) of $div_scale digits.
If either the dividend's or the divisor's mantissa has more digits than
the value of F, the higher value will be used instead of F.
This is to limit the digits (A) of the result (just consider what would
happen with unlimited A and P in the case of 1/3 :-)
- * fdiv will calculate (at least) 4 more digits than required (determined by
+ * bdiv will calculate (at least) 4 more digits than required (determined by
A, P or F), and, if F is not used, round the result
(this will still fail in the case of a result like 0.12345000000001 with A
or P of 5, but this can not be helped - or can it?)
* Thus you can have the math done by on Math::Big* class in two modi:
+ never round (this is the default):
This is done by setting A and P to undef. No math operation
- will round the result, with fdiv() and fsqrt() as exceptions to guard
+ will round the result, with bdiv() and bsqrt() as exceptions to guard
against overflows. You must explicitly call bround(), bfround() or
round() (the latter with parameters).
Note: Once you have rounded a number, the settings will 'stick' on it
$x = Math::BigFloat->new(12.34);
$y = Math::BigFloat->new(98.76);
$z = $x * $y; # 1218.6984
- print $x->copy()->fround(3); # 12.3 (but A is now 3!)
+ print $x->copy()->bround(3); # 12.3 (but A is now 3!)
$z = $x * $y; # still 1218.6984, without
# copy would have been 1210!
+ global A
+ global P
+ global F
- * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two
+ * bsqrt() will hand its arguments to bdiv(), as it used to, only now for two
arguments (A and P) instead of one
=item Local settings
=item Rounding
* the rounding routines will use the respective global or local settings.
- fround()/bround() is for accuracy rounding, while ffround()/bfround()
- is for precision
+ bround() is for accuracy rounding, while bfround() is for precision
* the two rounding functions take as the second parameter one of the
following rounding modes (R):
'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common'
=item Remarks
* The defaults are set up so that the new code gives the same results as
- the old code (except in a few cases on fdiv):
+ the old code (except in a few cases on bdiv):
+ Both A and P are undefined and thus will not be used for rounding
after each operation.
+ round() is thus a no-op, unless given extra parameters A and P
$y = Math::BigFloat->new(123.456789);
Math::BigFloat->accuracy(4); # no more A than 4
- is ($x->copy()->fround(),123.4); # even rounding
- print $x->copy()->fround(),"\n"; # 123.4
+ is ($x->copy()->bround(),123.4); # even rounding
+ print $x->copy()->bround(),"\n"; # 123.4
Math::BigFloat->round_mode('odd'); # round to odd
- print $x->copy()->fround(),"\n"; # 123.5
+ print $x->copy()->bround(),"\n"; # 123.5
Math::BigFloat->accuracy(5); # no more A than 5
Math::BigFloat->round_mode('odd'); # round to odd
- print $x->copy()->fround(),"\n"; # 123.46
- $y = $x->copy()->fround(4),"\n"; # A = 4: 123.4
+ print $x->copy()->bround(),"\n"; # 123.46
+ $y = $x->copy()->bround(4),"\n"; # A = 4: 123.4
print "$y, ",$y->accuracy(),"\n"; # 123.4, 4
Math::BigFloat->accuracy(undef); # A not important now
Math::BigFloat->precision(2); # P important
print $x->copy()->bnorm(),"\n"; # 123.46
- print $x->copy()->fround(),"\n"; # 123.46
+ print $x->copy()->bround(),"\n"; # 123.46
Examples for converting:
=item *
-Completely rewritten by Tels L<http://bloodgate.com> in 2001-2008.
+Completely rewritten by Tels L<http://bloodgate.com>, 2001-2008.
=item *
-Florian Ragwitz L<flora@cpan.org>, 2010.
+Florian Ragwitz E<lt>flora@cpan.orgE<gt>, 2010.
=item *
-Peter John Acklam, L<pjacklam@online.no>, 2011-.
+Peter John Acklam E<lt>pjacklam@online.noE<gt>, 2011-.
=back
use strict;
use warnings;
-our $VERSION = '1.999707';
+our $VERSION = '1.999710';
# Package to store unsigned big integers in decimal and do math with them
use strict;
use warnings;
-our $VERSION = '1.999707';
+our $VERSION = '1.999710';
package Math::BigInt;
@args = split(/:/,$_,99); $ans = pop(@args);
}
$try = "\$x = $class->new(\"$args[0]\");";
- if ($f eq "fnorm")
+ if ($f eq "bnorm")
{
$try .= "\$x;";
- } elsif ($f eq "finf") {
- $try .= "\$x->finf('$args[1]');";
+ } elsif ($f eq "binf") {
+ $try .= "\$x->binf('$args[1]');";
} elsif ($f eq "is_inf") {
$try .= "\$x->is_inf('$args[1]');";
- } elsif ($f eq "fone") {
+ } elsif ($f eq "bone") {
$try .= "\$x->bone('$args[1]');";
- } elsif ($f eq "fstr") {
+ } elsif ($f eq "bstr") {
$try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);";
- $try .= '$x->fstr();';
+ $try .= '$x->bstr();';
} elsif ($f eq "parts") {
# ->bstr() to see if an object is returned
$try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();';
$try .= '$x->mantissa()->bstr();';
} elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) {
$try .= "\$x->$f();";
- # some unary ops (test the fxxx form, since that is done by AUTOLOAD)
- } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|int|abs)$/) {
- $try .= "\$x->f$1();";
+ # some unary ops
+ } elsif ($f =~ /^b(nan|sstr|neg|floor|ceil|int|abs)$/) {
+ $try .= "\$x->$f();";
# overloaded functions
} elsif ($f =~ /^(log|exp|sin|cos|atan2|int|neg|abs|sqrt)$/) {
$try .= "\$x = $f(\$x);";
$try .= "\$x->$f();";
} elsif ($f eq "bpi") {
$try .= '$class->bpi($x);';
- } elsif ($f eq "finc") {
+ } elsif ($f eq "binc") {
$try .= '++$x;';
- } elsif ($f eq "fdec") {
+ } elsif ($f eq "bdec") {
$try .= '--$x;';
- }elsif ($f eq "fround") {
- $try .= "$setup; \$x->fround($args[1]);";
- } elsif ($f eq "ffround") {
- $try .= "$setup; \$x->ffround($args[1]);";
- } elsif ($f eq "fsqrt") {
- $try .= "$setup; \$x->fsqrt();";
- } elsif ($f eq "ffac") {
- $try .= "$setup; \$x->ffac();";
- } elsif ($f eq "flog") {
+ }elsif ($f eq "bround") {
+ $try .= "$setup; \$x->bround($args[1]);";
+ } elsif ($f eq "bfround") {
+ $try .= "$setup; \$x->bfround($args[1]);";
+ } elsif ($f eq "bsqrt") {
+ $try .= "$setup; \$x->bsqrt();";
+ } elsif ($f eq "bfac") {
+ $try .= "$setup; \$x->bfac();";
+ } elsif ($f eq "blog") {
if (defined $args[1] && $args[1] ne '')
{
$try .= "\$y = $class->new($args[1]);";
- $try .= "$setup; \$x->flog(\$y);";
+ $try .= "$setup; \$x->blog(\$y);";
}
else
{
- $try .= "$setup; \$x->flog();";
+ $try .= "$setup; \$x->blog();";
}
}
else
$try .= "$class\::blcm(\$x, \$y";
$try .= ", \$z" if (defined $args[2]);
$try .= " );";
- } elsif ($f eq "fcmp") {
- $try .= '$x->fcmp($y);';
- } elsif ($f eq "facmp") {
- $try .= '$x->facmp($y);';
- } elsif ($f eq "fpow") {
+ } elsif ($f eq "bcmp") {
+ $try .= '$x->bcmp($y);';
+ } elsif ($f eq "bacmp") {
+ $try .= '$x->bacmp($y);';
+ } elsif ($f eq "bpow") {
$try .= '$x ** $y;';
} elsif ($f eq "bnok") {
$try .= '$x->bnok($y);';
$try .= '$x->bsin($y);';
} elsif ($f eq "batan") {
$try .= '$x->batan($y);';
- } elsif ($f eq "froot") {
- $try .= "$setup; \$x->froot(\$y);";
- } elsif ($f eq "fadd") {
+ } elsif ($f eq "broot") {
+ $try .= "$setup; \$x->broot(\$y);";
+ } elsif ($f eq "badd") {
$try .= '$x + $y;';
- } elsif ($f eq "fsub") {
+ } elsif ($f eq "bsub") {
$try .= '$x - $y;';
- } elsif ($f eq "fmul") {
+ } elsif ($f eq "bmul") {
$try .= '$x * $y;';
- } elsif ($f eq "fdiv") {
+ } elsif ($f eq "bdiv") {
$try .= "$setup; \$x / \$y;";
- } elsif ($f eq "fdiv-list") {
- $try .= "$setup; join(',',\$x->fdiv(\$y));";
- } elsif ($f eq "frsft") {
+ } elsif ($f eq "bdiv-list") {
+ $try .= "$setup; join(',',\$x->bdiv(\$y));";
+ } elsif ($f eq "brsft") {
$try .= '$x >> $y;';
- } elsif ($f eq "flsft") {
+ } elsif ($f eq "blsft") {
$try .= '$x << $y;';
- } elsif ($f eq "fmod") {
+ } elsif ($f eq "bmod") {
$try .= '$x % $y;';
} else {
# Functions with three arguments
###############################################################################
# zero,inf,one,nan
-$x = $class->new(2); $x->fzero(); is ($x->{_a}, undef); is ($x->{_p}, undef);
-$x = $class->new(2); $x->finf(); is ($x->{_a}, undef); is ($x->{_p}, undef);
-$x = $class->new(2); $x->fone(); is ($x->{_a}, undef); is ($x->{_p}, undef);
-$x = $class->new(2); $x->fnan(); is ($x->{_a}, undef); is ($x->{_p}, undef);
+$x = $class->new(2); $x->bzero(); is ($x->{_a}, undef); is ($x->{_p}, undef);
+$x = $class->new(2); $x->binf(); is ($x->{_a}, undef); is ($x->{_p}, undef);
+$x = $class->new(2); $x->bone(); is ($x->{_a}, undef); is ($x->{_p}, undef);
+$x = $class->new(2); $x->bnan(); is ($x->{_a}, undef); is ($x->{_p}, undef);
###############################################################################
# bone/binf etc as plain calls (Lite failed them)
-is ($class->fzero(),0);
-is ($class->fone(),1);
-is ($class->fone('+'),1);
-is ($class->fone('-'),-1);
-is ($class->fnan(),'NaN');
-is ($class->finf(),'inf');
-is ($class->finf('+'),'inf');
-is ($class->finf('-'),'-inf');
-is ($class->finf('-inf'),'-inf');
+is ($class->bzero(),0);
+is ($class->bone(),1);
+is ($class->bone('+'),1);
+is ($class->bone('-'),-1);
+is ($class->bnan(),'NaN');
+is ($class->binf(),'inf');
+is ($class->binf('+'),'inf');
+is ($class->binf('-'),'-inf');
+is ($class->binf('-inf'),'-inf');
$class->accuracy(undef); $class->precision(undef); # reset
# Check numify on non-finite objects.
{
- my $inf = 1e99 ** 1e99;
+ require Math::Complex;
+ my $inf = Math::Complex::Inf();
my $nan = $inf - $inf;
is($class -> binf("+") -> numify(), $inf, "numify of +Inf");
is($class -> binf("-") -> numify(), -$inf, "numify of -Inf");
}
###############################################################################
-# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt()
+# bsqrt() with set global A/P or A/P enabled on $x, also a test whether bsqrt()
# correctly modifies $x
-$x = $class->new(12); $class->precision(-2); $x->fsqrt(); is ($x,'3.46');
+$x = $class->new(12); $class->precision(-2); $x->bsqrt(); is ($x,'3.46');
$class->precision(undef);
-$x = $class->new(12); $class->precision(0); $x->fsqrt(); is ($x,'3');
+$x = $class->new(12); $class->precision(0); $x->bsqrt(); is ($x,'3');
-$class->precision(-3); $x = $class->new(12); $x->fsqrt(); is ($x,'3.464');
+$class->precision(-3); $x = $class->new(12); $x->bsqrt(); is ($x,'3.464');
{
no strict 'refs';
# A and P set => NaN
${${class}.'::accuracy'} = 4; $x = $class->new(12);
- $x->fsqrt(3); is ($x,'NaN');
+ $x->bsqrt(3); is ($x,'NaN');
# supplied arg overrides set global
- $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); is ($x,'3.46');
+ $class->precision(undef); $x = $class->new(12); $x->bsqrt(3); is ($x,'3.46');
$class->accuracy(undef); $class->precision(undef); # reset for further tests
}
#############################################################################
# bug 1/0.5 leaving 2e-0 instead of 2e0
-is ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0');
+is ($class->new(1)->bdiv('0.5')->bsstr(),'2e+0');
###############################################################################
# [perl #30609] bug with $x -= $x not being 0, but 2*$x
2:0:1
7:0:1
2:1:2
-&flog
+&blog
0::-inf
-1::NaN
-2::NaN
# reset for further tests
$div_scale = 40;
1::0
-&frsft
-NaNfrsft:2:NaN
+&brsft
+NaNbrsft:2:NaN
0:2:0
1:1:0.5
2:1:1
4:1:2
123:1:61.5
32:3:4
-&flsft
-NaNflsft:0:NaN
+&blsft
+NaNblsft:0:NaN
2:1:4
4:3:32
5:3:40
1:2:4
0:5:0
-&fnorm
+&bnorm
1:1
-0:0
-fnormNaN:NaN
+bnormNaN:NaN
+inf:inf
-inf:-inf
123:123
0.1234567:0
0.12345678:0
0.123456789:0
-&finf
+&binf
1:+:inf
2:-:-inf
3:abc:inf
-5:-5
100:100
-100:-100
-&fnan
+&bnan
abc:NaN
2:NaN
-2:NaN
0:NaN
-&fone
+&bone
2:+:1
-2:-:-1
-2:+:1
-2::1
abc::1
2:abc:1
-&fsstr
+&bsstr
+inf:inf
-inf:-inf
abcfsstr:NaN
123:123e+0
-5:-5e+0
-100:-1e+2
-&fstr
+&bstr
+inf:::inf
-inf:::-inf
abcfstr:::NaN
0.001234::-8:0.00123400
0:4::0
0::-4:0.0000
-&fnorm
+&bnorm
inf:inf
+inf:inf
-inf:-inf
1.1e1:11
-3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004
-&fpow
+&bpow
NaN:1:NaN
1:NaN:NaN
NaN:-1:NaN
$div_scale = 20;
#62.5:12.5:26447206647554886213592.3959144
$div_scale = 40;
-&fneg
-fnegNaN:NaN
+&bneg
+bnegNaN:NaN
+inf:-inf
-inf:inf
+0:0
-123456789:123456789
+123.456789:-123.456789
-123456.789:123456.789
-&fabs
-fabsNaN:NaN
+&babs
+babsNaN:NaN
+inf:inf
-inf:inf
+0:0
-123456789:123456789
+123.456789:123.456789
-123456.789:123456.789
-&fround
+&bround
$round_mode = "trunc"
+inf:5:inf
-inf:5:-inf
+601234300:6:601234000
+60123456789.0123:5:60123000000
-60123456789.0123:5:-60123000000
-&ffround
+&bfround
$round_mode = "trunc"
+inf:5:inf
-inf:5:-inf
0.01234567:-8:0.01234567
0.01234567:-9:0.012345670
0.01234567:-12:0.012345670000
-&fcmp
-fcmpNaN:fcmpNaN:
-fcmpNaN:+0:
-+0:fcmpNaN:
+&bcmp
+bcmpNaN:bcmpNaN:
+bcmpNaN:+0:
++0:bcmpNaN:
+0:+0:0
-1:+0:-1
+0:-1:1
NaN:inf:
-inf:NaN:
NaN:-inf:
-&facmp
-fcmpNaN:fcmpNaN:
-fcmpNaN:+0:
-+0:fcmpNaN:
+&bacmp
+bcmpNaN:bcmpNaN:
+bcmpNaN:+0:
++0:bcmpNaN:
+0:+0:0
-1:+0:1
+0:-1:-1
5:-inf:-1
-1:-inf:-1
# return undef
-+inf:facmpNaN:
-facmpNaN:inf:
--inf:facmpNaN:
-facmpNaN:-inf:
-&fdec
-fdecNaN:NaN
++inf:bacmpNaN:
+bacmpNaN:inf:
+-inf:bacmpNaN:
+bacmpNaN:-inf:
+&bdec
+bdecNaN:NaN
+inf:inf
-inf:-inf
+0:-1
-99:-100
-98:-99
99:98
-&finc
-fincNaN:NaN
+&binc
+bincNaN:NaN
+inf:inf
-inf:-inf
+0:1
-99:-98
-101:-100
99:100
-&fadd
+&badd
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
-123456789:-987654321:-1111111110
+123456789:-987654321:-864197532
0.001234:0.0001234:0.0013574
-&fsub
+&bsub
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
3:4:7:4
77777:777:123456789:99995084
3.2:6.2:5.2:2.970579856718063040273642739529400818
-&fmul
+&bmul
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
+99999999999:+9:899999999991
6:120:720
10:10000:100000
-&fdiv-list
+&bdiv-list
0:0:NaN,0
0:1:0,0
9:4:2,1
2.1:1:2.1,0
-2.1:-1:2.1,0
-2.1:1:-2.1,0
-&fdiv
+&bdiv
$div_scale = 40; $round_mode = 'even'
abc:abc:NaN
abc:+1:abc:NaN
123456789.1234:1:100000000
# reset scale for further tests
$div_scale = 40
-&fmod
+&bmod
+9:4:1
+9:5:4
+9000:56:40
3:1:0
-3:-1:0
3:-1:0
-&ffac
+&bfac
Nanfac:NaN
-1:NaN
+inf:inf
10:3628800
11:39916800
12:479001600
-&froot
+&broot
# sqrt()
+0:2:0
+1:2:1
-1:2:NaN
-# -$x ** (1/2) => -$y, but not in froot()
+# -$x ** (1/2) => -$y, but not in broot()
-123.456:2:NaN
+inf:2:inf
-inf:2:NaN
16:4:2
81:4:3
# see t/bigroot() for more tests
-&fsqrt
+&bsqrt
+0:0
-1:NaN
-2:NaN
-16:NaN
-123.45:NaN
-nanfsqrt:NaN
+nanbsqrt:NaN
+inf:inf
-inf:NaN
1:1
-inf:1
+inf:0
&parts
-0:0 1
+0:0 0
1:1 0
123:123 0
-123:-123 0
+inf:inf inf
-inf:-inf inf
&exponent
-0:1
+0:0
1:0
123:0
-123:0
1:1
-1:0
-2:0
-&ffloor
+&bfloor
0:0
abc:NaN
+inf:inf
0.1234567:0
0.12345678:0
0.123456789:0
-&fceil
+&bceil
0:0
abc:NaN
+inf:inf
-51.2:-51
12.2:13
-0.4:0
-&fint
+&bint
0:0
NaN:NaN
+inf:inf
# bug #17447: Can't call method Math::BigFloat->bsub, not a valid method
my $c = Math::BigFloat->new( '123.3' );
-is ($c->fsub(123), '0.3'); # calling fsub on a BigFloat works
+is ($c->bsub(123), '0.3'); # calling bsub on a BigFloat works
# Bug until BigInt v1.86, the scale wasn't treated as a scalar:
$c = Math::BigFloat->new('0.008'); my $d = Math::BigFloat->new(3);
# Check numify on non-finite objects.
{
- my $inf = 1e99 ** 1e99;
+ require Math::Complex;
+ my $inf = Math::Complex::Inf();
my $nan = $inf - $inf;
is($class -> binf("+") -> numify(), $inf, "numify of +Inf");
is($class -> binf("-") -> numify(), -$inf, "numify of -Inf");
123:0
-1:0
-2:0
-0:1
+0:0
+inf:inf
-inf:inf
&parts
123:123,0
-1:-1,0
-2:-2,0
-0:0,1
+0:0,0
+inf:inf,inf
-inf:-inf,inf
&bfac
+0:2:0
+1:2:1
-1:2:NaN
-# -$x ** (1/2) => -$y, but not in froot()
+# -$x ** (1/2) => -$y, but not in broot()
-123:2:NaN
+inf:2:inf
-inf:2:NaN
'-inf':-inf
&bsstr
1:1e+0
-0:0e+1
+0:0e+0
2:2e+0
200:2e+2
-5:-5e+0
--- /dev/null
+#!perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 27;
+
+my $class;
+
+BEGIN { $class = 'Math::BigFloat'; }
+BEGIN { use_ok($class, '1.999710'); }
+
+while (<DATA>) {
+ s/\s+\z//;
+ next if /^#/ || ! /\S/;
+
+ my ($in0, $out0) = split /:/;
+ my $x;
+
+ my $test = qq|\$x = $class -> from_hex("$in0");|;
+
+ my $desc = $test;
+
+ print("#\n",
+ "# Now about to execute the following test.\n",
+ "#\n",
+ "# $test\n",
+ "#\n");
+
+ eval $test;
+ die $@ if $@; # this should never happen
+
+ subtest $desc, sub {
+ plan tests => 2,
+
+ # Check output.
+
+ is(ref($x), $class, "output arg is a $class");
+ is($x, $out0, 'output arg has the right value');
+ };
+
+}
+
+__END__
+0x1p+0:1
+0x.8p+1:1
+0x.4p+2:1
+0x.2p+3:1
+0x.1p+4:1
+0x2p-1:1
+0x4p-2:1
+0x8p-3:1
+
+-0x1p+0:-1
+
+0x0p+0:0
+0x0p+7:0
+0x0p-7:0
+0x0.p+0:0
+0x.0p+0:0
+0x0.0p+0:0
+
+0xcafe:51966
+xcafe:51966
+cafe:51966
+
+0x1.9p+3:12.5
+0x12.34p-1:9.1015625
+-0x.789abcdefp+32:-2023406814.9375
+0x12.3456789ap+31:39093746765
+
+NaN:NaN
++inf:NaN
+-inf:NaN
+0x.p+0:NaN
$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->babs(),123401);
$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->bneg(),123401);
-# test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions)
+# test bdiv rounding to A and R (bug in v1.48 and maybe earlier versions)
$mbf->round_mode('even');
-$x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); is ($x,'123.4');
+$x = $mbf->new('740.7')->bdiv('6',4,undef,'zero'); is ($x,'123.4');
$x = $mbi->new('123456'); $y = $mbi->new('123456'); $y->{_a} = 6;
is ($x->bdiv($y),1); is ($x->{_a},6); # carried over
$class->accuracy(undef); # reset for further tests
$class->precision(undef);
}
-# bug with flog(Math::BigFloat,Math::BigInt)
+# bug with blog(Math::BigFloat,Math::BigInt)
$x = Math::BigFloat->new(100);
$x = $x->blog(Math::BigInt->new(10));
123:0
-1:0
-2:0
-0:1
+0:0
+inf:inf
-inf:inf
&parts
123:123,0
-1:-1,0
-2:-2,0
-0:0,1
+0:0,0
+inf:inf,inf
-inf:-inf,inf
&bfac