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