xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt-FastCalc/t/bigroot.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1*256a93a4Safresh1# -*- mode: perl; -*-
25759b3d2Safresh1
35759b3d2Safresh1# Test broot function (and bsqrt() function, since it is used by broot()).
45759b3d2Safresh1
55759b3d2Safresh1# It is too slow to be simple included in bigfltpm.inc, where it would get
65759b3d2Safresh1# executed 3 times.
75759b3d2Safresh1
85759b3d2Safresh1# But it is better to test the numerical functionality, instead of not testing
95759b3d2Safresh1# it at all.
105759b3d2Safresh1
115759b3d2Safresh1use strict;                     # restrict unsafe constructs
125759b3d2Safresh1use warnings;                   # enable optional warnings
135759b3d2Safresh1
14f3efcd01Safresh1use Test::More tests => 16;
155759b3d2Safresh1
165759b3d2Safresh1use Math::BigFloat only => 'FastCalc';
175759b3d2Safresh1use Math::BigInt;
185759b3d2Safresh1
195759b3d2Safresh1my $mbf = "Math::BigFloat";
205759b3d2Safresh1my $mbi = "Math::BigInt";
215759b3d2Safresh1
225759b3d2Safresh1# 2 ** 240 =
235759b3d2Safresh1# 1766847064778384329583297500742918515827483896875618958121606201292619776
245759b3d2Safresh1
25f3efcd01Safresh1test_broot('2', '240', 8, undef,
26f3efcd01Safresh1           '1073741824');
27f3efcd01Safresh1test_broot('2', '240', 9, undef,
28f3efcd01Safresh1           '106528681.3099908308759836475139583940127');
29f3efcd01Safresh1test_broot('2', '120', 9, undef,
30f3efcd01Safresh1           '10321.27324073880096577298929482324664787');
31f3efcd01Safresh1test_broot('2', '120', 17, undef,
32f3efcd01Safresh1           '133.3268493632747279600707813049418888729');
335759b3d2Safresh1
345759b3d2Safresh1test_broot('2', '120', 8, undef,
355759b3d2Safresh1           '32768');
365759b3d2Safresh1test_broot('2', '60', 8, undef,
375759b3d2Safresh1           '181.0193359837561662466161566988413540569');
385759b3d2Safresh1test_broot('2', '60', 9, undef,
395759b3d2Safresh1           '101.5936673259647663841091609134277286651');
405759b3d2Safresh1test_broot('2', '60', 17, undef,
415759b3d2Safresh1           '11.54672461623965153271017217302844672562');
425759b3d2Safresh1
435759b3d2Safresh1sub test_broot {
445759b3d2Safresh1    my ($x, $n, $y, $scale, $expected) = @_;
455759b3d2Safresh1
465759b3d2Safresh1    my $s = $scale || 'undef';
475759b3d2Safresh1    is($mbf->new($x)->bpow($n)->broot($y, $scale), $expected,
485759b3d2Safresh1       "Try: $mbf->new($x)->bpow($n)->broot($y, $s) == $expected");
49f3efcd01Safresh1
50f3efcd01Safresh1    # Math::BigInt returns the truncated integer part of the output, so remove
51f3efcd01Safresh1    # the dot an anything after it before comparing.
52f3efcd01Safresh1
535759b3d2Safresh1    $expected =~ s/\..*//;
545759b3d2Safresh1    is($mbi->new($x)->bpow($n)->broot($y, $scale), $expected,
555759b3d2Safresh1       "Try: $mbi->new($x)->bpow($n)->broot($y, $s) == $expected");
565759b3d2Safresh1}
57