1#!/usr/bin/perl -w 2 3# Test blog function (and bpow, since it uses blog), as well as bexp(). 4 5# It is too slow to be simple included in bigfltpm.inc, where it would get 6# executed 3 times. One time would be under BareCalc, which shouldn't make any 7# difference since there is no CALC->_log() function, and one time under a 8# subclass, which *should* work. 9 10# But it is better to test the numerical functionality, instead of not testing 11# it at all (which did lead to wrong answers for 0 < $x < 1 in blog() in 12# versions up to v1.63, and for bsqrt($x) when $x << 1 for instance). 13 14use Test::More; 15use strict; 16 17BEGIN 18 { 19 $| = 1; 20 # to locate the testing files 21 my $location = $0; $location =~ s/biglog.t//i; 22 if ($ENV{PERL_CORE}) 23 { 24 # testing with the core distribution 25 @INC = qw(../lib); 26 } 27 unshift @INC, '../lib'; 28 if (-d 't') 29 { 30 chdir 't'; 31 require File::Spec; 32 unshift @INC, File::Spec->catdir(File::Spec->updir, $location); 33 } 34 else 35 { 36 unshift @INC, $location; 37 } 38 print "# INC = @INC\n"; 39 40 plan tests => 70; 41 } 42 43use Math::BigFloat; 44use Math::BigInt; 45 46my $cl = "Math::BigInt"; 47 48############################################################################# 49# test log($n) in BigInt (broken until 1.80) 50 51is ($cl->new(2)->blog(), '0', "blog(2)"); 52is ($cl->new(288)->blog(), '5',"blog(288)"); 53is ($cl->new(2000)->blog(), '7', "blog(2000)"); 54 55############################################################################# 56# test exp($n) in BigInt 57 58is ($cl->new(1)->bexp(), '2', "bexp(1)"); 59is ($cl->new(2)->bexp(), '7',"bexp(2)"); 60is ($cl->new(3)->bexp(), '20', "bexp(3)"); 61 62############################################################################# 63############################################################################# 64# BigFloat tests 65 66############################################################################# 67# test log(2, N) where N > 67 (broken until 1.82) 68 69$cl = "Math::BigFloat"; 70 71# These tests can take quite a while, but are nec. Maybe protect them with 72# some alarm()? 73 74# this triggers the calculation and caching of ln(2): 75ok ($cl->new(5)->blog(undef,71), 76'1.6094379124341003746007593332261876395256013542685177219126478914741790'); 77 78# if the cache was correct, we should get this result, fast: 79ok ($cl->new(2)->blog(undef,71), 80'0.69314718055994530941723212145817656807550013436025525412068000949339362'); 81 82ok ($cl->new(10)->blog(undef,71), 83'2.3025850929940456840179914546843642076011014886287729760333279009675726'); 84 85ok ($cl->new(21)->blog(undef,71), 86'3.0445224377234229965005979803657054342845752874046106401940844835750742'); 87 88############################################################################# 89 90# These tests are now really fast, since they collapse to blog(10), basically 91# Don't attempt to run them with older versions. You are warned. 92 93# $x < 0 => NaN 94ok ($cl->new(-2)->blog(), 'NaN'); 95ok ($cl->new(-1)->blog(), 'NaN'); 96ok ($cl->new(-10)->blog(), 'NaN'); 97ok ($cl->new(-2,2)->blog(), 'NaN'); 98 99my $ten = $cl->new(10)->blog(); 100 101# 10 is cached (up to 75 digits) 102ok ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601'); 103 104# 0.1 is using the cached value for log(10), too 105 106ok ($cl->new(0.1)->blog(), -$ten); 107ok ($cl->new(0.01)->blog(), -$ten * 2); 108ok ($cl->new(0.001)->blog(), -$ten * 3); 109ok ($cl->new(0.0001)->blog(), -$ten * 4); 110 111# also cached 112ok ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755'); 113ok ($cl->new(4)->blog(), $cl->new(2)->blog * 2); 114 115# These are still slow, so do them only to 10 digits 116 117ok ($cl->new('0.2')->blog(undef,10), '-1.609437912'); 118ok ($cl->new('0.3')->blog(undef,10), '-1.203972804'); 119ok ($cl->new('0.4')->blog(undef,10), '-0.9162907319'); 120ok ($cl->new('0.5')->blog(undef,10), '-0.6931471806'); 121ok ($cl->new('0.6')->blog(undef,10), '-0.5108256238'); 122ok ($cl->new('0.7')->blog(undef,10), '-0.3566749439'); 123ok ($cl->new('0.8')->blog(undef,10), '-0.2231435513'); 124ok ($cl->new('0.9')->blog(undef,10), '-0.1053605157'); 125 126ok ($cl->new('9')->blog(undef,10), '2.197224577'); 127 128ok ($cl->new('10')->blog(10,10), '1.000000000'); 129ok ($cl->new('20')->blog(20,10), '1.000000000'); 130ok ($cl->new('100')->blog(100,10), '1.000000000'); 131 132ok ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100 133ok ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400 134 135ok ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4 136ok ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16 137 138ok ($cl->new('1.2')->bpow('0.3',10), '1.056219968'); 139ok ($cl->new('10')->bpow('0.6',10), '3.981071706'); 140 141# blog should handle bigint input 142is (Math::BigFloat::blog(Math::BigInt->new(100),10), 2, "blog(100)"); 143 144############################################################################# 145# some integer results 146is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); 147is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); 148is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); 149 150my $x = Math::BigInt->new( '777' ) ** 256; 151my $base = Math::BigInt->new( '12345678901234' ); 152is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); 153 154$x = Math::BigInt->new( '777' ) ** 777; 155$base = Math::BigInt->new( '777' ); 156is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); 157 158############################################################################# 159# test for bug in bsqrt() not taking negative _e into account 160test_bpow ('200','0.5',10, '14.14213562'); 161test_bpow ('20','0.5',10, '4.472135955'); 162test_bpow ('2','0.5',10, '1.414213562'); 163test_bpow ('0.2','0.5',10, '0.4472135955'); 164test_bpow ('0.02','0.5',10, '0.1414213562'); 165test_bpow ('0.49','0.5',undef , '0.7'); 166test_bpow ('0.49','0.5',10 , '0.7000000000'); 167test_bpow ('0.002','0.5',10, '0.04472135955'); 168test_bpow ('0.0002','0.5',10, '0.01414213562'); 169test_bpow ('0.0049','0.5',undef,'0.07'); 170test_bpow ('0.0049','0.5',10 , '0.07000000000'); 171test_bpow ('0.000002','0.5',10, '0.001414213562'); 172test_bpow ('0.021','0.5',10, '0.1449137675'); 173test_bpow ('1.2','0.5',10, '1.095445115'); 174test_bpow ('1.23','0.5',10, '1.109053651'); 175test_bpow ('12.3','0.5',10, '3.507135583'); 176 177test_bpow ('9.9','0.5',10, '3.146426545'); 178test_bpow ('9.86902225','0.5',10, '3.141500000'); 179test_bpow ('9.86902225','0.5',undef, '3.1415'); 180 181test_bpow ('0.2','0.41',10, '0.5169187652'); 182 183############################################################################# 184# test bexp() with cached results 185 186is ($cl->new(1)->bexp(), '2.718281828459045235360287471352662497757', 'bexp(1)'); 187is ($cl->new(2)->bexp(40), $cl->new(1)->bexp(45)->bpow(2,40), 'bexp(2)'); 188 189is ($cl->new("12.5")->bexp(61), $cl->new(1)->bexp(65)->bpow(12.5,61), 'bexp(12.5)'); 190 191############################################################################# 192# test bexp() with big values (non-cached) 193 194is ($cl->new(1)->bexp(100), 195 '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', 196 'bexp(100)'); 197 198is ($cl->new("12.5")->bexp(91), $cl->new(1)->bexp(95)->bpow(12.5,91), 199 'bexp(12.5) to 91 digits'); 200 201# all done 2021; 203 204############################################################################# 205sub test_bpow 206 { 207 my ($x,$y,$scale,$result) = @_; 208 209 print "# Tried: $x->bpow($y,$scale);\n" 210 unless ok ($cl->new($x)->bpow($y,$scale),$result); 211 } 212 213 214