Commit | Line | Data |
---|---|---|
116a1b2f SP |
1 | #!/usr/bin/perl -w |
2 | ||
3 | # Test blog function (and bpow, since it uses blog), as well as bexp(). | |
4 | ||
116a1b2f | 5 | use strict; |
c32198f6 | 6 | use Test::More tests => 17; |
116a1b2f SP |
7 | |
8 | use Math::BigRat; | |
9 | ||
10 | my $cl = "Math::BigRat"; | |
11 | ||
12 | ############################################################################# | |
13 | # test log($n) | |
14 | ||
15 | # does not work yet | |
16 | #is ($cl->new(2)->blog(), '0', "blog(2)"); | |
17 | #is ($cl->new(288)->blog(), '5',"blog(288)"); | |
18 | #is ($cl->new(2000)->blog(), '7', "blog(2000)"); | |
19 | ||
20 | ############################################################################# | |
21 | # test exp($n) | |
22 | ||
23 | is ($cl->new(1)->bexp()->as_int(), '2', "bexp(1)"); | |
24 | is ($cl->new(2)->bexp()->as_int(), '7',"bexp(2)"); | |
25 | is ($cl->new(3)->bexp()->as_int(), '20', "bexp(3)"); | |
26 | ||
27 | # rounding not implemented yet | |
28 | #is ($cl->new(3)->bexp(10), '20', "bexp(3,10)"); | |
29 | ||
30 | # $x < 0 => NaN | |
31 | ok ($cl->new(-2)->blog(), 'NaN'); | |
32 | ok ($cl->new(-1)->blog(), 'NaN'); | |
33 | ok ($cl->new(-10)->blog(), 'NaN'); | |
34 | ok ($cl->new(-2,2)->blog(), 'NaN'); | |
35 | ||
36 | ############################################################################# | |
37 | # test bexp() with cached results | |
38 | ||
39 | is ($cl->new(1)->bexp(), | |
40 | '90933395208605785401971970164779391644753259799242' . '/' . | |
41 | '33452526613163807108170062053440751665152000000000', | |
42 | 'bexp(1)'); | |
4de3d162 | 43 | is ($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)'); |
116a1b2f | 44 | |
4de3d162 | 45 | is ($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)'); |
116a1b2f SP |
46 | |
47 | ############################################################################# | |
48 | # test bexp() with big values (non-cached) | |
49 | ||
4de3d162 T |
50 | is ($cl->new(1)->bexp(1,100)->as_float(100), |
51 | '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', | |
52 | 'bexp(100)'); | |
116a1b2f | 53 | |
4de3d162 | 54 | is ($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91), |
116a1b2f SP |
55 | 'bexp(12.5) to 91 digits'); |
56 | ||
57 | ############################################################################# | |
58 | # some integer results | |
59 | is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); | |
60 | is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); | |
61 | is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); | |
62 | ||
63 | my $x = Math::BigInt->new( '777' ) ** 256; | |
64 | my $base = Math::BigInt->new( '12345678901234' ); | |
65 | is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); | |
66 | ||
67 | $x = Math::BigInt->new( '777' ) ** 777; | |
68 | $base = Math::BigInt->new( '777' ); | |
69 | is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); | |
70 | ||
71 | # all done | |
72 | 1; | |
73 |