Upgrade to CPAN-1.90.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / biglog.t
CommitLineData
990fb837 1#!/usr/bin/perl -w
2
3# Test blog function (and bpow, since it uses blog).
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;
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
a0ac753d 40 plan tests => 56;
990fb837 41 }
42
43use Math::BigFloat;
44use Math::BigInt;
45
a0ac753d 46my $cl = "Math::BigInt";
47
48# test log($n) in BigInt (broken until 1.80)
49
50ok ($cl->new(2)->blog(), '0');
51ok ($cl->new(288)->blog(), '5');
52ok ($cl->new(2000)->blog(), '7');
53
54#############################################################################
55
56$cl = "Math::BigFloat";
990fb837 57
9b924220 58# These tests are now really fast, since they collapse to blog(10), basically
990fb837 59# Don't attempt to run them with older versions. You are warned.
60
61# $x < 0 => NaN
62ok ($cl->new(-2)->blog(), 'NaN');
63ok ($cl->new(-1)->blog(), 'NaN');
64ok ($cl->new(-10)->blog(), 'NaN');
65ok ($cl->new(-2,2)->blog(), 'NaN');
66
67my $ten = $cl->new(10)->blog();
68
69# 10 is cached (up to 75 digits)
70ok ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601');
71
72# 0.1 is using the cached value for log(10), too
73
74ok ($cl->new(0.1)->blog(), -$ten);
75ok ($cl->new(0.01)->blog(), -$ten * 2);
76ok ($cl->new(0.001)->blog(), -$ten * 3);
77ok ($cl->new(0.0001)->blog(), -$ten * 4);
78
79# also cached
80ok ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755');
091c87b1 81ok ($cl->new(4)->blog(), $cl->new(2)->blog * 2);
990fb837 82
83# These are still slow, so do them only to 10 digits
84
85ok ($cl->new('0.2')->blog(undef,10), '-1.609437912');
86ok ($cl->new('0.3')->blog(undef,10), '-1.203972804');
87ok ($cl->new('0.4')->blog(undef,10), '-0.9162907319');
88ok ($cl->new('0.5')->blog(undef,10), '-0.6931471806');
89ok ($cl->new('0.6')->blog(undef,10), '-0.5108256238');
90ok ($cl->new('0.7')->blog(undef,10), '-0.3566749439');
91ok ($cl->new('0.8')->blog(undef,10), '-0.2231435513');
92ok ($cl->new('0.9')->blog(undef,10), '-0.1053605157');
93
94ok ($cl->new('9')->blog(undef,10), '2.197224577');
95
96ok ($cl->new('10')->blog(10,10), '1.000000000');
97ok ($cl->new('20')->blog(20,10), '1.000000000');
98ok ($cl->new('100')->blog(100,10), '1.000000000');
99
100ok ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100
101ok ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400
102
103ok ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4
104ok ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16
105
106ok ($cl->new('1.2')->bpow('0.3',10), '1.056219968');
107ok ($cl->new('10')->bpow('0.6',10), '3.981071706');
108
109# blog should handle bigint input
93c87d9d 110ok (Math::BigFloat::blog(Math::BigInt->new(100),10), 2);
990fb837 111
9b924220 112# some integer results
113ok ($cl->new(2)->bpow(32)->blog(2), '32'); # 2 ** 32
114ok ($cl->new(3)->bpow(32)->blog(3), '32'); # 3 ** 32
115ok ($cl->new(2)->bpow(65)->blog(2), '65'); # 2 ** 65
116
990fb837 117# test for bug in bsqrt() not taking negative _e into account
118test_bpow ('200','0.5',10, '14.14213562');
119test_bpow ('20','0.5',10, '4.472135955');
120test_bpow ('2','0.5',10, '1.414213562');
121test_bpow ('0.2','0.5',10, '0.4472135955');
122test_bpow ('0.02','0.5',10, '0.1414213562');
123test_bpow ('0.49','0.5',undef , '0.7');
124test_bpow ('0.49','0.5',10 , '0.7000000000');
125test_bpow ('0.002','0.5',10, '0.04472135955');
126test_bpow ('0.0002','0.5',10, '0.01414213562');
127test_bpow ('0.0049','0.5',undef,'0.07');
128test_bpow ('0.0049','0.5',10 , '0.07000000000');
129test_bpow ('0.000002','0.5',10, '0.001414213562');
130test_bpow ('0.021','0.5',10, '0.1449137675');
131test_bpow ('1.2','0.5',10, '1.095445115');
132test_bpow ('1.23','0.5',10, '1.109053651');
133test_bpow ('12.3','0.5',10, '3.507135583');
134
135test_bpow ('9.9','0.5',10, '3.146426545');
136test_bpow ('9.86902225','0.5',10, '3.141500000');
137test_bpow ('9.86902225','0.5',undef, '3.1415');
138
139test_bpow ('0.2','0.41',10, '0.5169187652');
140
141sub test_bpow
142 {
143 my ($x,$y,$scale,$result) = @_;
144
9b924220 145 print "# Tried: $x->bpow($y,$scale);\n"
990fb837 146 unless ok ($cl->new($x)->bpow($y,$scale),$result);
147 }
148