1#!perl
2
3# Test broot function (and bsqrt() function, since it is used by broot()).
4
5# It is too slow to be simple included in bigfltpm.inc, where it would get
6# executed 3 times.
7
8# But it is better to test the numerical functionality, instead of not testing
9# it at all.
10
11use strict;                     # restrict unsafe constructs
12use warnings;                   # enable optional warnings
13
14use Test::More tests => 16;
15
16use Math::BigFloat only => 'Calc';
17use Math::BigInt;
18
19my $mbf = "Math::BigFloat";
20my $mbi = "Math::BigInt";
21
22# 2 ** 240 =
23# 1766847064778384329583297500742918515827483896875618958121606201292619776
24
25test_broot('2', '240', 8, undef,
26           '1073741824');
27test_broot('2', '240', 9, undef,
28           '106528681.3099908308759836475139583940127');
29test_broot('2', '120', 9, undef,
30           '10321.27324073880096577298929482324664787');
31test_broot('2', '120', 17, undef,
32           '133.3268493632747279600707813049418888729');
33
34test_broot('2', '120', 8, undef,
35           '32768');
36test_broot('2', '60', 8, undef,
37           '181.0193359837561662466161566988413540569');
38test_broot('2', '60', 9, undef,
39           '101.5936673259647663841091609134277286651');
40test_broot('2', '60', 17, undef,
41           '11.54672461623965153271017217302844672562');
42
43sub test_broot {
44    my ($x, $n, $y, $scale, $expected) = @_;
45
46    my $s = $scale || 'undef';
47    is($mbf->new($x)->bpow($n)->broot($y, $scale), $expected,
48       "Try: $mbf->new($x)->bpow($n)->broot($y, $s) == $expected");
49
50    # Math::BigInt returns the truncated integer part of the output, so remove
51    # the dot an anything after it before comparing.
52
53    $expected =~ s/\..*//;
54    is($mbi->new($x)->bpow($n)->broot($y, $scale), $expected,
55       "Try: $mbi->new($x)->bpow($n)->broot($y, $s) == $expected");
56}
57