CryptX

 view release on metacpan or  search on metacpan

t/mbi_ltm_biglog.t  view on Meta::CPAN

use strict;
use Test::More;

BEGIN {
  plan skip_all => "requires Math::BigInt 1.9997+" unless eval { require Math::BigInt && eval($Math::BigInt::VERSION) >= 1.9997 };
  plan tests => 70;
}

use Math::BigFloat only => 'LTM';
use Math::BigInt only => 'LTM';

my $cl = "Math::BigInt";

is (Math::BigInt->config()->{lib}, 'Math::BigInt::LTM', 'LTM loaded');

#############################################################################
# test log($n) in BigInt (broken until 1.80)

is ($cl->new(2)->blog(), '0', "blog(2)");
is ($cl->new(288)->blog(), '5',"blog(288)");
is ($cl->new(2000)->blog(), '7', "blog(2000)");

#############################################################################
# test exp($n) in BigInt

is ($cl->new(1)->bexp(), '2', "bexp(1)");
is ($cl->new(2)->bexp(), '7',"bexp(2)");
is ($cl->new(3)->bexp(), '20', "bexp(3)");

#############################################################################
#############################################################################
# BigFloat tests

#############################################################################
# test log(2, N) where N > 67 (broken until 1.82)

$cl = "Math::BigFloat";

# These tests can take quite a while, but are nec. Maybe protect them with
# some alarm()?

# this triggers the calculation and caching of ln(2):
is ($cl->new(5)->blog(undef,71),
'1.6094379124341003746007593332261876395256013542685177219126478914741790');

# if the cache was correct, we should get this result, fast:
is ($cl->new(2)->blog(undef,71),
'0.69314718055994530941723212145817656807550013436025525412068000949339362');

is ($cl->new(11)->blog(undef,71),
'2.3978952727983705440619435779651292998217068539374171752185677091305736');

is ($cl->new(21)->blog(undef,71),
'3.0445224377234229965005979803657054342845752874046106401940844835750742');

#############################################################################

# These tests are now really fast, since they collapse to blog(10), basically
# Don't attempt to run them with older versions. You are warned.

# $x < 0 => NaN
is ($cl->new(-2)->blog(), 'NaN');
is ($cl->new(-1)->blog(), 'NaN');
is ($cl->new(-10)->blog(), 'NaN');
is ($cl->new(-2,2)->blog(), 'NaN');

my $ten = $cl->new(10)->blog();

# 10 is cached (up to 75 digits)
is ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601');

# 0.1 is using the cached value for log(10), too

is ($cl->new(0.1)->blog(), -$ten);
is ($cl->new(0.01)->blog(), -$ten * 2);
is ($cl->new(0.001)->blog(), -$ten * 3);
is ($cl->new(0.0001)->blog(), -$ten * 4);

# also cached
is ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755');
is ($cl->new(4)->blog(), $cl->new(2)->blog * 2);

# These are still slow, so do them only to 10 digits

is ($cl->new('0.2')->blog(undef,10), '-1.609437912');
is ($cl->new('0.3')->blog(undef,10), '-1.203972804');
is ($cl->new('0.4')->blog(undef,10), '-0.9162907319');
is ($cl->new('0.5')->blog(undef,10), '-0.6931471806');
is ($cl->new('0.6')->blog(undef,10), '-0.5108256238');
is ($cl->new('0.7')->blog(undef,10), '-0.3566749439');
is ($cl->new('0.8')->blog(undef,10), '-0.2231435513');
is ($cl->new('0.9')->blog(undef,10), '-0.1053605157');

is ($cl->new('9')->blog(undef,10), '2.197224577');

is ($cl->new('10')->blog(10,10),   '1.000000000');
is ($cl->new('20')->blog(20,10),   '1.000000000');
is ($cl->new('100')->blog(100,10), '1.000000000');

is ($cl->new('100')->blog(10,10),  '2.000000000');      # 10 ** 2 == 100
is ($cl->new('400')->blog(20,10),  '2.000000000');      # 20 ** 2 == 400

is ($cl->new('4')->blog(2,10),  '2.000000000');         # 2 ** 2 == 4
is ($cl->new('16')->blog(2,10), '4.000000000');         # 2 ** 4 == 16

is ($cl->new('1.2')->bpow('0.3',10),  '1.056219968');
is ($cl->new('10')->bpow('0.6',10),   '3.981071706');

#############################################################################
# some integer results
is ($cl->new(2)->bpow(32)->blog(2),  '32', "2 ** 32");
is ($cl->new(3)->bpow(32)->blog(3),  '32', "3 ** 32");
is ($cl->new(2)->bpow(65)->blog(2),  '65', "2 ** 65");

my $x = Math::BigInt->new( '777' ) ** 256;
my $base = Math::BigInt->new( '12345678901234' );
is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)');

$x = Math::BigInt->new( '777' ) ** 777;
$base = Math::BigInt->new( '777' );
is ($x->copy()->blog($base), 777, 'blog(777**777, 777)');

#############################################################################
# test for bug in bsqrt() not taking negative _e into account
test_bpow ('200','0.5',10,      '14.14213562');



( run in 0.634 second using v1.01-cache-2.11-cpan-e1769b4cff6 )