summaryrefslogtreecommitdiff
path: root/cpan/Math-BigRat/t/biglog.t
blob: 44f5962cad211254eba5f5eda2458942d3f8c9e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!perl

# Test blog function (and bpow, since it uses blog), as well as bexp().

use strict;
use warnings;

use Test::More tests => 17;

use Math::BigRat;

my $cl = "Math::BigRat";

#############################################################################
# test log($n)

# does not work yet
#is ($cl->new(2)->blog(), '0', "blog(2)");
#is ($cl->new(288)->blog(), '5',"blog(288)");
#is ($cl->new(2000)->blog(), '7', "blog(2000)");

#############################################################################
# test exp($n)

is($cl->new(1)->bexp()->as_int(), '2',  qq|$cl->new(1)->bexp()->as_int()|);
is($cl->new(2)->bexp()->as_int(), '7',  qq|$cl->new(1)->bexp()->as_int()|);
is($cl->new(3)->bexp()->as_int(), '20', qq|$cl->new(1)->bexp()->as_int()|);

# rounding not implemented yet
#is ($cl->new(3)->bexp(10), '20', "bexp(3,10)");

# $x < 0 => NaN
is($cl->new(-2)->blog(),   'NaN', qq|$cl->new(-2)->blog()|);
is($cl->new(-1)->blog(),   'NaN', qq|$cl->new(-1)->blog()|);
is($cl->new(-10)->blog(),  'NaN', qq|$cl->new(-10)->blog()|);
is($cl->new(-2,2)->blog(), 'NaN', qq|$cl->new(-2,2)->blog()|);

#############################################################################
# test bexp() with cached results

is($cl->new(1)->bexp(),
  '90933395208605785401971970164779391644753259799242' . '/' .
  '33452526613163807108170062053440751665152000000000',
  'bexp(1)');
is($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)');

is($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)');

#############################################################################
# test bexp() with big values (non-cached)

is($cl->new(1)->bexp(1,100)->as_float(100),
  '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427',
 'bexp(100)');

is($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91),
  'bexp(12.5) to 91 digits');

#############################################################################
# some integer results

is($cl->new(2)->bpow(32)->blog(2),  '32', "2 ** 32");
is($cl->new(3)->bpow(32)->blog(3),  '32', "3 ** 32");
is($cl->new(2)->bpow(65)->blog(2),  '65', "2 ** 65");

my $x    = Math::BigInt->new( '777' ) ** 256;
my $base = Math::BigInt->new( '12345678901234' );
is($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)');

$x = Math::BigInt->new( '777' ) ** 777;
$base = Math::BigInt->new( '777' );
is($x->copy()->blog($base), 777, 'blog(777**777, 777)');