| 1 | package Math::Erf::Approx; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use Sub::Exporter -setup => { exports => [qw< erf erfc >] }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use Test::More; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 3 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | no warnings 'once'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 3 | $Math::Erf::Approx::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||
| 13 | 3 | $Math::Erf::Approx::VERSION = '0.002'; | |||||||||||||||||||
| 14 | }; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub erf | ||||||||||||||||||||
| 17 | 92 | ▶ | { | ||||||||||||||||||
|
Subroutine
erf: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||
| 18 | 92 | my ($x) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 92 | ▶ | if ($x < 0) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | { | ||||||||||||||||||||
| 22 | 0 | return -erf(-$x); | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 92 | my @a = qw< 1.0 0.278393 0.230389 0.000972 0.078108>; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 92 | my $sum; | |||||||||||||||||||
| 28 | 92 | for my $i (0 .. 4) | |||||||||||||||||||
| 29 | { | ||||||||||||||||||||
| 30 | 460 | $sum += $a[$i] * ($x ** $i); | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 92 | 1.0 - ($sum ** -4); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub erfc | ||||||||||||||||||||
| 37 | 46 | ▶ | { | ||||||||||||||||||
|
Subroutine
erfc: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 38 | 46 | my ($x, $erf) = @_; | |||||||||||||||||||
| 39 | 46 | ▶ | $erf //= \&erf; | ||||||||||||||||||
|
Condition50%1/2
$erf //= \&Math::Erf::Approx::erf
MC/DC0%0/1
$erf //= \&Math::Erf::Approx::erf
$erf
Truth table50%1/2
A
A= $erf
| |||||||||||||||||||||
| 40 | 46 | 1.0 - $erf->($x); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub _test_cases | ||||||||||||||||||||
| 45 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
_test_cases: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 46 | 1 | my @numbers = grep { /\d/ } split /\s+/, q{ | |||||||||||||||||||
| 47 | 0.00 0.0000000 1.0000000 1.30 0.9340079 0.0659921 | ||||||||||||||||||||
| 48 | 0.05 0.0563720 0.9436280 1.40 0.9522851 0.0477149 | ||||||||||||||||||||
| 49 | 0.10 0.1124629 0.8875371 1.50 0.9661051 0.0338949 | ||||||||||||||||||||
| 50 | 0.15 0.1679960 0.8320040 1.60 0.9763484 0.0236516 | ||||||||||||||||||||
| 51 | 0.20 0.2227026 0.7772974 1.70 0.9837905 0.0162095 | ||||||||||||||||||||
| 52 | 0.25 0.2763264 0.7236736 1.80 0.9890905 0.0109095 | ||||||||||||||||||||
| 53 | 0.30 0.3286268 0.6713732 1.90 0.9927904 0.0072096 | ||||||||||||||||||||
| 54 | 0.35 0.3793821 0.6206179 2.00 0.9953223 0.0046777 | ||||||||||||||||||||
| 55 | 0.40 0.4283924 0.5716076 2.10 0.9970205 0.0029795 | ||||||||||||||||||||
| 56 | 0.45 0.4754817 0.5245183 2.20 0.9981372 0.0018628 | ||||||||||||||||||||
| 57 | 0.50 0.5204999 0.4795001 2.30 0.9988568 0.0011432 | ||||||||||||||||||||
| 58 | 0.55 0.5633234 0.4366766 2.40 0.9993115 0.0006885 | ||||||||||||||||||||
| 59 | 0.60 0.6038561 0.3961439 2.50 0.9995930 0.0004070 | ||||||||||||||||||||
| 60 | 0.65 0.6420293 0.3579707 2.60 0.9997640 0.0002360 | ||||||||||||||||||||
| 61 | 0.70 0.6778012 0.3221988 2.70 0.9998657 0.0001343 | ||||||||||||||||||||
| 62 | 0.75 0.7111556 0.2888444 2.80 0.9999250 0.0000750 | ||||||||||||||||||||
| 63 | 0.80 0.7421010 0.2578990 2.90 0.9999589 0.0000411 | ||||||||||||||||||||
| 64 | 0.85 0.7706681 0.2293319 3.00 0.9999779 0.0000221 | ||||||||||||||||||||
| 65 | 0.90 0.7969082 0.2030918 3.10 0.9999884 0.0000116 | ||||||||||||||||||||
| 66 | 0.95 0.8208908 0.1791092 3.20 0.9999940 0.0000060 | ||||||||||||||||||||
| 67 | 1.00 0.8427008 0.1572992 3.30 0.9999969 0.0000031 | ||||||||||||||||||||
| 68 | 1.10 0.8802051 0.1197949 3.40 0.9999985 0.0000015 | ||||||||||||||||||||
| 69 | 1.20 0.9103140 0.0896860 3.50 0.9999993 0.0000007 | ||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | my @test_cases; | |||||||||||||||||||
| 73 | 1 | ▶ | while (@numbers) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | { | ||||||||||||||||||||
| 75 | 46 | push @test_cases, [shift @numbers, shift @numbers, shift @numbers]; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | return @test_cases; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | sub _close_enough | ||||||||||||||||||||
| 82 | 92 | ▶ | { | ||||||||||||||||||
|
Subroutine
_close_enough: called CC=2 SCAR=9.2
| |||||||||||||||||||||
| 83 | 92 | my ($x, $y) = @_; | |||||||||||||||||||
| 84 | 92 | my $err = abs($x - $y); | |||||||||||||||||||
| 85 | 92 | ▶ | return 1 if $err < 0.0005; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | diag "Got: $x"; | |||||||||||||||||||
| 87 | 0 | diag "Expected $y"; | |||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | sub run_tests | ||||||||||||||||||||
| 92 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
run_tests: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 93 | 1 | my ($class) = @_; | |||||||||||||||||||
| 94 | 1 | my @cases = $class->_test_cases; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | plan tests => (2 * @cases); | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | foreach my $tc (@cases) | |||||||||||||||||||
| 99 | { | ||||||||||||||||||||
| 100 | 46 | my ($x, $erf, $erfc) = @$tc; | |||||||||||||||||||
| 101 | 46 | ok _close_enough(erf($x), $erf), "erf($x)"; | |||||||||||||||||||
| 102 | 46 | ok _close_enough(erfc($x), $erfc), "erfc($x)"; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 3 | ▶ | caller(0) or __PACKAGE__->run_tests; | ||||||||||||||||||
|
Condition33%1/3
caller 0 || __PACKAGE__->run_tests
MC/DC0%0/2
caller 0 || __PACKAGE__->run_tests
caller 0
__PACKAGE__->run_tests
Truth table33%1/3
A || B
A= caller 0B= __PACKAGE__->run_tests
| |||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||