File: author-batan2-mbf.t

package info (click to toggle)
libmath-bigint-perl 1.999818-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,628 kB
  • sloc: perl: 47,633; pascal: 5,299; makefile: 2
file content (133 lines) | stat: -rwxr-xr-x 3,445 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!perl

BEGIN {
    unless ($ENV{AUTHOR_TESTING}) {
        print "1..0 # SKIP these tests are for testing by the author";
        exit;
    }
}

use strict;
use warnings;

use Test::More tests => 3528;

use Math::Complex ();

use Math::BigFloat;

my $inf = Math::Complex::Inf();
my $nan = $inf - $inf;

my $class = 'Math::BigFloat';

# isnan X

#sub isnan { !($_[0] <= 0 || $_[0] > 0) }
sub isnan { !defined($_[0] <=> 0) }

# linspace MIN, MAX, N
#
# Returns N linearly spaced elements from MIN to MAX.

sub linspace {
    my ($xmin, $xmax, $n) = @_;

    if ($n == 0) {
        return ();
    } elsif ($n == 1) {
        return ($xmin);
    } else {
        my $c = ($xmax - $xmin) / ($n - 1);
        return map { $xmin + $c * $_  } 0 .. ($n - 1);
    }
}

# logspace MIN, MAX, N
#
# Returns N logarithmically spaced elements from MIN to MAX.

sub logspace {
    my ($xmin, $xmax, $n) = @_;
    if ($n == 0) {
        return ();
    } elsif ($n == 1) {
        return ($xmin);
    } else {
        my @lin = linspace(log($xmin), log($xmax), $n);
        my @log = map { exp } @lin;
        $log[   0   ] = $xmin;
        $log[ $#log ] = $xmax;
        return @log;
    }
}

my @x;
@x = logspace(0.01, 12, 20);
@x = map { sprintf "%.3g", $_ } @x;
@x = (reverse(map( { -$_ } @x)), 0, @x, $nan);

my $accu       = 16;
my $tol        = 1e-14;
my $max_relerr = 0;

for my $ply (@x) {
    for my $plx (@x) {
        my $plz = CORE::atan2($ply, $plx);

        # $y -> batan2($x) where $x is a scalar

        {
            my $y = $class -> new($ply);
            $y -> batan2($plx, $accu);

            my $desc = qq|\$y = $class->new("$ply");|
                     . qq| \$y->batan2("$plx", $accu)|
                     . qq| vs. CORE::atan2($ply, $plx)|;

            if (isnan($plz)) {
                is($y, "NaN", $desc);
            } elsif ($plz == 0) {
                cmp_ok($y, '==', $plz, $desc);
            } else {
                my $relerr = abs(($y - $plz) / $plz);
                if (!cmp_ok($relerr, '<', $tol, "relative error of $desc")) {
                    diag(sprintf("             CORE::atan2(...): %.15g\n" .
                                 "  Math::BigFloat->batan2(...): %.15g\n",
                                 $plz, $y));
                }
                $max_relerr = $relerr if $relerr > $max_relerr;
            }
        }

        # $y -> batan2($x) where $x is an object

        {
            my $x = $class -> new($plx);
            my $y = $class -> new($ply);
            $y -> batan2($plx, $accu);

            my $desc = qq|\$y = $class->new("$ply");|
                     . qq| \$x = $class->new("$plx");|
                     . qq| \$y->batan2(\$x, $accu)|
                     . qq| vs. CORE::atan2($ply, $plx)|;

            if (isnan($plz)) {
                is($y, "NaN", $desc);
            } elsif ($plz == 0) {
                cmp_ok($y, '==', $plz, $desc);
            } else {
                my $relerr = abs(($y - $plz) / $plz);
                if (!cmp_ok($relerr, '<', $tol, "relative error of $desc")) {
                    diag(sprintf("             CORE::atan2(...): %.15g\n" .
                                 "  Math::BigFloat->batan2(...): %.15g\n",
                                  $plz, $y));
                }
                $max_relerr = $relerr if $relerr > $max_relerr;
            }
        }

    }
}

diag("Maximum relative error = ", $max_relerr -> numify(), "\n");