Commit | Line | Data |
116a1b2f |
1 | #!/usr/bin/perl -w |
2 | |
3 | # Test blog function (and bpow, since it uses blog), as well as bexp(). |
4 | |
5 | use Test::More; |
6 | use strict; |
7 | |
8 | BEGIN |
9 | { |
10 | $| = 1; |
11 | # to locate the testing files |
12 | my $location = $0; $location =~ s/biglog.t//i; |
13 | if ($ENV{PERL_CORE}) |
14 | { |
15 | # testing with the core distribution |
16 | @INC = qw(../lib); |
17 | } |
18 | unshift @INC, '../lib'; |
19 | if (-d 't') |
20 | { |
21 | chdir 't'; |
22 | require File::Spec; |
23 | unshift @INC, File::Spec->catdir(File::Spec->updir, $location); |
24 | } |
25 | else |
26 | { |
27 | unshift @INC, $location; |
28 | } |
29 | print "# INC = @INC\n"; |
30 | |
4de3d162 |
31 | plan tests => 17; |
116a1b2f |
32 | } |
33 | |
34 | use Math::BigRat; |
35 | |
36 | my $cl = "Math::BigRat"; |
37 | |
38 | ############################################################################# |
39 | # test log($n) |
40 | |
41 | # does not work yet |
42 | #is ($cl->new(2)->blog(), '0', "blog(2)"); |
43 | #is ($cl->new(288)->blog(), '5',"blog(288)"); |
44 | #is ($cl->new(2000)->blog(), '7', "blog(2000)"); |
45 | |
46 | ############################################################################# |
47 | # test exp($n) |
48 | |
49 | is ($cl->new(1)->bexp()->as_int(), '2', "bexp(1)"); |
50 | is ($cl->new(2)->bexp()->as_int(), '7',"bexp(2)"); |
51 | is ($cl->new(3)->bexp()->as_int(), '20', "bexp(3)"); |
52 | |
53 | # rounding not implemented yet |
54 | #is ($cl->new(3)->bexp(10), '20', "bexp(3,10)"); |
55 | |
56 | # $x < 0 => NaN |
57 | ok ($cl->new(-2)->blog(), 'NaN'); |
58 | ok ($cl->new(-1)->blog(), 'NaN'); |
59 | ok ($cl->new(-10)->blog(), 'NaN'); |
60 | ok ($cl->new(-2,2)->blog(), 'NaN'); |
61 | |
62 | ############################################################################# |
63 | # test bexp() with cached results |
64 | |
65 | is ($cl->new(1)->bexp(), |
66 | '90933395208605785401971970164779391644753259799242' . '/' . |
67 | '33452526613163807108170062053440751665152000000000', |
68 | 'bexp(1)'); |
4de3d162 |
69 | is ($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)'); |
116a1b2f |
70 | |
4de3d162 |
71 | is ($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)'); |
116a1b2f |
72 | |
73 | ############################################################################# |
74 | # test bexp() with big values (non-cached) |
75 | |
4de3d162 |
76 | is ($cl->new(1)->bexp(1,100)->as_float(100), |
77 | '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', |
78 | 'bexp(100)'); |
116a1b2f |
79 | |
4de3d162 |
80 | is ($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91), |
116a1b2f |
81 | 'bexp(12.5) to 91 digits'); |
82 | |
83 | ############################################################################# |
84 | # some integer results |
85 | is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); |
86 | is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); |
87 | is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); |
88 | |
89 | my $x = Math::BigInt->new( '777' ) ** 256; |
90 | my $base = Math::BigInt->new( '12345678901234' ); |
91 | is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); |
92 | |
93 | $x = Math::BigInt->new( '777' ) ** 777; |
94 | $base = Math::BigInt->new( '777' ); |
95 | is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); |
96 | |
97 | # all done |
98 | 1; |
99 | |