Blame t/biglog.t

Packit dcde0c
#!perl
Packit dcde0c
Packit dcde0c
# Test blog function (and bpow, since it uses blog), as well as bexp().
Packit dcde0c
Packit dcde0c
# It is too slow to be simple included in bigfltpm.inc, where it would get
Packit dcde0c
# executed 3 times. One time would be under Math::BigInt::BareCalc, which
Packit dcde0c
# shouldn't make any difference since there is no CALC->_log() function, and
Packit dcde0c
# one time under a subclass, which *should* work.
Packit dcde0c
Packit dcde0c
# But it is better to test the numerical functionality, instead of not testing
Packit dcde0c
# it at all (which did lead to wrong answers for 0 < $x < 1 in blog() in
Packit dcde0c
# versions up to v1.63, and for bsqrt($x) when $x << 1 for instance).
Packit dcde0c
Packit dcde0c
use strict;
Packit dcde0c
use warnings;
Packit dcde0c
Packit dcde0c
use Test::More tests => 73;
Packit dcde0c
Packit dcde0c
use Math::BigFloat only => 'FastCalc';
Packit dcde0c
use Math::BigInt;
Packit dcde0c
Packit dcde0c
my $class = "Math::BigInt";
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test $n->blog() in Math::BigInt (broken until 1.80)
Packit dcde0c
Packit dcde0c
is($class->new(2)->blog(),    '0', "$class->new(2)->blog()");
Packit dcde0c
is($class->new(288)->blog(),  '5', "$class->new(288)->blog()");
Packit dcde0c
is($class->new(2000)->blog(), '7', "$class->new(2000)->blog()");
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test $n->bexp() in Math::BigInt
Packit dcde0c
Packit dcde0c
is($class->new(1)->bexp(), '2',  "$class->new(1)->bexp()");
Packit dcde0c
is($class->new(2)->bexp(), '7',  "$class->new(2)->bexp()");
Packit dcde0c
is($class->new(3)->bexp(), '20', "$class->new(3)->bexp()");
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
###############################################################################
Packit dcde0c
# Math::BigFloat tests
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test $n->blog(undef, N) where N > 67 (broken until 1.82)
Packit dcde0c
Packit dcde0c
$class = "Math::BigFloat";
Packit dcde0c
Packit dcde0c
# These tests can take quite a while, but are necessary. Maybe protect them
Packit dcde0c
# with some alarm()?
Packit dcde0c
Packit dcde0c
# this triggers the calculation and caching of ln(2):
Packit dcde0c
is($class->new(5)->blog(undef, 71),
Packit dcde0c
   '1.6094379124341003746007593332261876395256013542685177219126478914741790',
Packit dcde0c
   "$class->new(5)->blog(undef, 71)");
Packit dcde0c
Packit dcde0c
# if the cache was correct, we should get this result, fast:
Packit dcde0c
is($class->new(2)->blog(undef, 71),
Packit dcde0c
   '0.69314718055994530941723212145817656807550013436025525412068000949339362',
Packit dcde0c
   "$class->new(2)->blog(undef, 71)");
Packit dcde0c
Packit dcde0c
is($class->new(11)->blog(undef, 71),
Packit dcde0c
   '2.3978952727983705440619435779651292998217068539374171752185677091305736',
Packit dcde0c
   "$class->new(11)->blog(undef, 71)");
Packit dcde0c
Packit dcde0c
is($class->new(21)->blog(undef, 71),
Packit dcde0c
   '3.0445224377234229965005979803657054342845752874046106401940844835750742',
Packit dcde0c
   "$class->new(21)->blog(undef, 71)");
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
Packit dcde0c
# These tests are now really fast, since they collapse to blog(10), basically
Packit dcde0c
# Don't attempt to run them with older versions. You are warned.
Packit dcde0c
Packit dcde0c
# $x < 0 => NaN
Packit dcde0c
is($class->new(-2)->blog(),    'NaN', "$class->new(-2)->blog()");
Packit dcde0c
is($class->new(-1)->blog(),    'NaN', "$class->new(-1)->blog()");
Packit dcde0c
is($class->new(-10)->blog(),   'NaN', "$class->new(-10)->blog()");
Packit dcde0c
is($class->new(-2, 2)->blog(), 'NaN', "$class->new(-2, 2)->blog()");
Packit dcde0c
Packit dcde0c
my $ten = $class->new(10)->blog();
Packit dcde0c
Packit dcde0c
# 10 is cached (up to 75 digits)
Packit dcde0c
is($class->new(10)->blog(),
Packit dcde0c
   '2.302585092994045684017991454684364207601',
Packit dcde0c
   qq|$class->new(10)->blog()|);
Packit dcde0c
Packit dcde0c
# 0.1 is using the cached value for log(10), too
Packit dcde0c
Packit dcde0c
is($class->new("0.1")->blog(), -$ten,
Packit dcde0c
   qq|$class->new("0.1")->blog()|);
Packit dcde0c
is($class->new("0.01")->blog(), -$ten * 2,
Packit dcde0c
   qq|$class->new("0.01")->blog()|);
Packit dcde0c
is($class->new("0.001")->blog(), -$ten * 3,
Packit dcde0c
   qq|$class->new("0.001")->blog()|);
Packit dcde0c
is($class->new("0.0001")->blog(), -$ten * 4,
Packit dcde0c
   qq|$class->new("0.0001")->blog()|);
Packit dcde0c
Packit dcde0c
# also cached
Packit dcde0c
is($class->new(2)->blog(),
Packit dcde0c
   '0.6931471805599453094172321214581765680755',
Packit dcde0c
   qq|$class->new(2)->blog()|);
Packit dcde0c
is($class->new(4)->blog(), $class->new(2)->blog * 2,
Packit dcde0c
   qq|$class->new(4)->blog()|);
Packit dcde0c
Packit dcde0c
# These are still slow, so do them only to 10 digits
Packit dcde0c
Packit dcde0c
is($class->new("0.2")->blog(undef, 10), "-1.609437912",
Packit dcde0c
   qq|$class->new("0.2")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.3")->blog(undef, 10), "-1.203972804",
Packit dcde0c
   qq|$class->new("0.3")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.4")->blog(undef, 10), "-0.9162907319",
Packit dcde0c
   qq|$class->new("0.4")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.5")->blog(undef, 10), "-0.6931471806",
Packit dcde0c
   qq|$class->new("0.5")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.6")->blog(undef, 10), "-0.5108256238",
Packit dcde0c
   qq|$class->new("0.6")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.7")->blog(undef, 10), "-0.3566749439",
Packit dcde0c
   qq|$class->new("0.7")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.8")->blog(undef, 10), "-0.2231435513",
Packit dcde0c
   qq|$class->new("0.8")->blog(undef, 10)|);
Packit dcde0c
is($class->new("0.9")->blog(undef, 10), "-0.1053605157",
Packit dcde0c
   qq|$class->new("0.9")->blog(undef, 10)|);
Packit dcde0c
Packit dcde0c
is($class->new("9")->blog(undef, 10), "2.197224577",
Packit dcde0c
   qq|$class->new("9")->blog(undef, 10)|);
Packit dcde0c
Packit dcde0c
is($class->new("10")->blog(10, 10), "1.000000000",
Packit dcde0c
   qq|$class->new("10")->blog(10, 10)|);
Packit dcde0c
is($class->new("20")->blog(20, 10), "1.000000000",
Packit dcde0c
   qq|$class->new("20")->blog(20, 10)|);
Packit dcde0c
is($class->new("100")->blog(100, 10), "1.000000000",
Packit dcde0c
   qq|$class->new("100")->blog(100, 10)|);
Packit dcde0c
Packit dcde0c
is($class->new("100")->blog(10, 10), "2.000000000",     # 10 ** 2 == 100
Packit dcde0c
   qq|$class->new("100")->blog(10, 10)|);
Packit dcde0c
is($class->new("400")->blog(20, 10), "2.000000000",     # 20 ** 2 == 400
Packit dcde0c
   qq|$class->new("400")->blog(20, 10)|);
Packit dcde0c
Packit dcde0c
is($class->new("4")->blog(2, 10), "2.000000000",        # 2 ** 2 == 4
Packit dcde0c
   qq|$class->new("4")->blog(2, 10)|);
Packit dcde0c
is($class->new("16")->blog(2, 10), "4.000000000",       # 2 ** 4 == 16
Packit dcde0c
   qq|$class->new("16")->blog(2, 10)|);
Packit dcde0c
Packit dcde0c
is($class->new("1.2")->bpow("0.3", 10), "1.056219968",
Packit dcde0c
   qq|$class->new("1.2")->bpow("0.3", 10)|);
Packit dcde0c
is($class->new("10")->bpow("0.6", 10), "3.981071706",
Packit dcde0c
   qq|$class->new("10")->bpow("0.6", 10)|);
Packit dcde0c
Packit dcde0c
# blog should handle bigint input
Packit dcde0c
is(Math::BigFloat::blog(Math::BigInt->new(100), 10), 2, "blog(100)");
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# some integer results
Packit dcde0c
is($class->new(2)->bpow(32)->blog(2), "32", "2 ** 32");
Packit dcde0c
is($class->new(3)->bpow(32)->blog(3), "32", "3 ** 32");
Packit dcde0c
is($class->new(2)->bpow(65)->blog(2), "65", "2 ** 65");
Packit dcde0c
Packit dcde0c
my $x = Math::BigInt->new('777') ** 256;
Packit dcde0c
my $base = Math::BigInt->new('12345678901234');
Packit dcde0c
is($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)');
Packit dcde0c
Packit dcde0c
$x = Math::BigInt->new('777') ** 777;
Packit dcde0c
$base = Math::BigInt->new('777');
Packit dcde0c
is($x->copy()->blog($base), 777, 'blog(777**777, 777)');
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test for bug in bsqrt() not taking negative _e into account
Packit dcde0c
test_bpow('200', '0.5', 10, '14.14213562');
Packit dcde0c
test_bpow('20', '0.5', 10, '4.472135955');
Packit dcde0c
test_bpow('2', '0.5', 10, '1.414213562');
Packit dcde0c
test_bpow('0.2', '0.5', 10, '0.4472135955');
Packit dcde0c
test_bpow('0.02', '0.5', 10, '0.1414213562');
Packit dcde0c
test_bpow('0.49', '0.5', undef, '0.7');
Packit dcde0c
test_bpow('0.49', '0.5', 10, '0.7000000000');
Packit dcde0c
test_bpow('0.002', '0.5', 10, '0.04472135955');
Packit dcde0c
test_bpow('0.0002', '0.5', 10, '0.01414213562');
Packit dcde0c
test_bpow('0.0049', '0.5', undef, '0.07');
Packit dcde0c
test_bpow('0.0049', '0.5', 10, '0.07000000000');
Packit dcde0c
test_bpow('0.000002', '0.5', 10, '0.001414213562');
Packit dcde0c
test_bpow('0.021', '0.5', 10, '0.1449137675');
Packit dcde0c
test_bpow('1.2', '0.5', 10, '1.095445115');
Packit dcde0c
test_bpow('1.23', '0.5', 10, '1.109053651');
Packit dcde0c
test_bpow('12.3', '0.5', 10, '3.507135583');
Packit dcde0c
Packit dcde0c
test_bpow('9.9', '0.5', 10, '3.146426545');
Packit dcde0c
test_bpow('9.86902225', '0.5', 10, '3.141500000');
Packit dcde0c
test_bpow('9.86902225', '0.5', undef, '3.1415');
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# other tests for bpow()
Packit dcde0c
Packit dcde0c
test_bpow('0.2', '0.41', 10, '0.5169187652');
Packit dcde0c
Packit dcde0c
is($class->new("0.01")->bpow("28.4", 40)->bsstr(),
Packit dcde0c
   '1584893192461113485202101373391507013269e-96',
Packit dcde0c
   qq|$class->new("0.01")->bpow("28.4", 40)->bsstr()|);
Packit dcde0c
Packit dcde0c
# The following test takes too long.
Packit dcde0c
#is($class->new("2")->bpow("-1034.5", 40)->bsstr(),
Packit dcde0c
#   '3841222690408590466868250378242558090957e-351',
Packit dcde0c
#   qq|$class->new("2")->bpow("-1034.5", 40)|);
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test bexp() with cached results
Packit dcde0c
Packit dcde0c
is($class->new(1)->bexp(), '2.718281828459045235360287471352662497757',
Packit dcde0c
    'bexp(1)');
Packit dcde0c
is($class->new(2)->bexp(40), $class->new(1)->bexp(45)->bpow(2, 40),
Packit dcde0c
    'bexp(2)');
Packit dcde0c
Packit dcde0c
is($class->new("12.5")->bexp(61), $class->new(1)->bexp(65)->bpow(12.5, 61),
Packit dcde0c
    'bexp(12.5)');
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# test bexp() with big values (non-cached)
Packit dcde0c
Packit dcde0c
is($class->new(1)->bexp(100),
Packit dcde0c
   '2.7182818284590452353602874713526624977572470936999'
Packit dcde0c
   . '59574966967627724076630353547594571382178525166427',
Packit dcde0c
   qq|$class->new(1)->bexp(100)|);
Packit dcde0c
Packit dcde0c
is($class->new("12.5")->bexp(91), $class->new(1)->bexp(95)->bpow(12.5, 91),
Packit dcde0c
   qq|$class->new("12.5")->bexp(91)|);
Packit dcde0c
Packit dcde0c
is($class->new("-118.5")->bexp(20)->bsstr(),
Packit dcde0c
   '34364014567198602057e-71',
Packit dcde0c
   qq|$class->new("-118.5")->bexp(20)->bsstr()|);
Packit dcde0c
Packit dcde0c
is($class->new("-394.84010945715266885")->bexp(20)->bsstr(),
Packit dcde0c
   '33351796227864913873e-191',
Packit dcde0c
   qq|$class->new("-118.5")->bexp(20)->bsstr()|);
Packit dcde0c
Packit dcde0c
# all done
Packit dcde0c
Packit dcde0c
1;
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
Packit dcde0c
sub test_bpow {
Packit dcde0c
    my ($x, $y, $scale, $result) = @_;
Packit dcde0c
    is($class->new($x)->bpow($y, $scale), $result,
Packit dcde0c
         qq|$class->new($x)->bpow($y, |
Packit dcde0c
       . (defined($scale) ? $scale : 'undef')
Packit dcde0c
       . qq|)|);
Packit dcde0c
}