This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Math::BigInt from 1.999701 to 1.999704
[perl5.git] / cpan / Math-BigInt / t / biglog.t
CommitLineData
990fb837
RGS
1#!/usr/bin/perl -w
2
7d193e39 3# Test blog function (and bpow, since it uses blog), as well as bexp().
990fb837
RGS
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
990fb837 14use strict;
4aa37faf 15use Test::More tests => 70;
990fb837
RGS
16
17use Math::BigFloat;
18use Math::BigInt;
19
a0ac753d
T
20my $cl = "Math::BigInt";
21
7d193e39 22#############################################################################
06ce15ad 23# test $n->blog() in BigInt (broken until 1.80)
a0ac753d 24
7d193e39
T
25is ($cl->new(2)->blog(), '0', "blog(2)");
26is ($cl->new(288)->blog(), '5',"blog(288)");
27is ($cl->new(2000)->blog(), '7', "blog(2000)");
28
29#############################################################################
06ce15ad 30# test $n->bexp() in BigInt
7d193e39
T
31
32is ($cl->new(1)->bexp(), '2', "bexp(1)");
33is ($cl->new(2)->bexp(), '7',"bexp(2)");
34is ($cl->new(3)->bexp(), '20', "bexp(3)");
a0ac753d
T
35
36#############################################################################
7d193e39
T
37#############################################################################
38# BigFloat tests
39
40#############################################################################
06ce15ad 41# test $n->blog(undef, N) where N > 67 (broken until 1.82)
a0ac753d
T
42
43$cl = "Math::BigFloat";
990fb837 44
7d193e39
T
45# These tests can take quite a while, but are nec. Maybe protect them with
46# some alarm()?
47
48# this triggers the calculation and caching of ln(2):
e16b8d1b 49is ($cl->new(5)->blog(undef,71),
7d193e39
T
50'1.6094379124341003746007593332261876395256013542685177219126478914741790');
51
52# if the cache was correct, we should get this result, fast:
e16b8d1b 53is ($cl->new(2)->blog(undef,71),
7d193e39
T
54'0.69314718055994530941723212145817656807550013436025525412068000949339362');
55
e16b8d1b 56is ($cl->new(11)->blog(undef,71),
57'2.3978952727983705440619435779651292998217068539374171752185677091305736');
7d193e39 58
e16b8d1b 59is ($cl->new(21)->blog(undef,71),
7d193e39
T
60'3.0445224377234229965005979803657054342845752874046106401940844835750742');
61
62#############################################################################
63
9b924220 64# These tests are now really fast, since they collapse to blog(10), basically
990fb837
RGS
65# Don't attempt to run them with older versions. You are warned.
66
67# $x < 0 => NaN
e16b8d1b 68is ($cl->new(-2)->blog(), 'NaN');
69is ($cl->new(-1)->blog(), 'NaN');
70is ($cl->new(-10)->blog(), 'NaN');
71is ($cl->new(-2,2)->blog(), 'NaN');
990fb837
RGS
72
73my $ten = $cl->new(10)->blog();
74
75# 10 is cached (up to 75 digits)
e16b8d1b 76is ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601');
990fb837
RGS
77
78# 0.1 is using the cached value for log(10), too
79
e16b8d1b 80is ($cl->new(0.1)->blog(), -$ten);
81is ($cl->new(0.01)->blog(), -$ten * 2);
82is ($cl->new(0.001)->blog(), -$ten * 3);
83is ($cl->new(0.0001)->blog(), -$ten * 4);
990fb837
RGS
84
85# also cached
e16b8d1b 86is ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755');
87is ($cl->new(4)->blog(), $cl->new(2)->blog * 2);
990fb837
RGS
88
89# These are still slow, so do them only to 10 digits
90
e16b8d1b 91is ($cl->new('0.2')->blog(undef,10), '-1.609437912');
92is ($cl->new('0.3')->blog(undef,10), '-1.203972804');
93is ($cl->new('0.4')->blog(undef,10), '-0.9162907319');
94is ($cl->new('0.5')->blog(undef,10), '-0.6931471806');
95is ($cl->new('0.6')->blog(undef,10), '-0.5108256238');
96is ($cl->new('0.7')->blog(undef,10), '-0.3566749439');
97is ($cl->new('0.8')->blog(undef,10), '-0.2231435513');
98is ($cl->new('0.9')->blog(undef,10), '-0.1053605157');
990fb837 99
e16b8d1b 100is ($cl->new('9')->blog(undef,10), '2.197224577');
990fb837 101
e16b8d1b 102is ($cl->new('10')->blog(10,10), '1.000000000');
103is ($cl->new('20')->blog(20,10), '1.000000000');
104is ($cl->new('100')->blog(100,10), '1.000000000');
990fb837 105
e16b8d1b 106is ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100
107is ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400
990fb837 108
e16b8d1b 109is ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4
110is ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16
990fb837 111
e16b8d1b 112is ($cl->new('1.2')->bpow('0.3',10), '1.056219968');
113is ($cl->new('10')->bpow('0.6',10), '3.981071706');
990fb837
RGS
114
115# blog should handle bigint input
7d193e39 116is (Math::BigFloat::blog(Math::BigInt->new(100),10), 2, "blog(100)");
990fb837 117
2ebb273f 118#############################################################################
9b924220 119# some integer results
7d193e39
T
120is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32");
121is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32");
122is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65");
9b924220 123
2ebb273f
T
124my $x = Math::BigInt->new( '777' ) ** 256;
125my $base = Math::BigInt->new( '12345678901234' );
126is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)');
127
128$x = Math::BigInt->new( '777' ) ** 777;
129$base = Math::BigInt->new( '777' );
130is ($x->copy()->blog($base), 777, 'blog(777**777, 777)');
131
132#############################################################################
990fb837
RGS
133# test for bug in bsqrt() not taking negative _e into account
134test_bpow ('200','0.5',10, '14.14213562');
135test_bpow ('20','0.5',10, '4.472135955');
136test_bpow ('2','0.5',10, '1.414213562');
137test_bpow ('0.2','0.5',10, '0.4472135955');
138test_bpow ('0.02','0.5',10, '0.1414213562');
139test_bpow ('0.49','0.5',undef , '0.7');
140test_bpow ('0.49','0.5',10 , '0.7000000000');
141test_bpow ('0.002','0.5',10, '0.04472135955');
142test_bpow ('0.0002','0.5',10, '0.01414213562');
143test_bpow ('0.0049','0.5',undef,'0.07');
144test_bpow ('0.0049','0.5',10 , '0.07000000000');
145test_bpow ('0.000002','0.5',10, '0.001414213562');
146test_bpow ('0.021','0.5',10, '0.1449137675');
147test_bpow ('1.2','0.5',10, '1.095445115');
148test_bpow ('1.23','0.5',10, '1.109053651');
149test_bpow ('12.3','0.5',10, '3.507135583');
150
151test_bpow ('9.9','0.5',10, '3.146426545');
152test_bpow ('9.86902225','0.5',10, '3.141500000');
153test_bpow ('9.86902225','0.5',undef, '3.1415');
154
155test_bpow ('0.2','0.41',10, '0.5169187652');
156
7d193e39 157#############################################################################
50109ad0 158# test bexp() with cached results
7d193e39
T
159
160is ($cl->new(1)->bexp(), '2.718281828459045235360287471352662497757', 'bexp(1)');
161is ($cl->new(2)->bexp(40), $cl->new(1)->bexp(45)->bpow(2,40), 'bexp(2)');
162
163is ($cl->new("12.5")->bexp(61), $cl->new(1)->bexp(65)->bpow(12.5,61), 'bexp(12.5)');
164
50109ad0
RGS
165#############################################################################
166# test bexp() with big values (non-cached)
167
168is ($cl->new(1)->bexp(100),
169 '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427',
170 'bexp(100)');
171
172is ($cl->new("12.5")->bexp(91), $cl->new(1)->bexp(95)->bpow(12.5,91),
173 'bexp(12.5) to 91 digits');
174
7d193e39
T
175# all done
1761;
177
178#############################################################################
990fb837
RGS
179sub test_bpow
180 {
181 my ($x,$y,$scale,$result) = @_;
182
9b924220 183 print "# Tried: $x->bpow($y,$scale);\n"
990fb837
RGS
184 unless ok ($cl->new($x)->bpow($y,$scale),$result);
185 }
186
7d193e39 187