| 1 | ############################################################################# | ||||||||||||||||||||||||||||
| 2 | # Math/Big.pm -- useful routines with big numbers (Math::BigInt/Math::BigFloat) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Math::Big; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | require 5.006002; # anything lower is simple untested | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Math::BigInt '1.97'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Math::BigFloat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | our $VERSION = '1.16'; | |||||||||||||||||||||||||||
| 16 | 3 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 17 | 3 | our @EXPORT_OK = qw( primes fibonacci base to_base hailstone factorial | |||||||||||||||||||||||||||
| 18 | euler bernoulli pi log | ||||||||||||||||||||||||||||
| 19 | tan cos sin cosh sinh arctan arctanh arcsin arcsinh | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # some often used constants: | ||||||||||||||||||||||||||||
| 23 | 3 | my $four = Math::BigFloat->new(4); | |||||||||||||||||||||||||||
| 24 | 3 | my $sixteen = Math::BigFloat->new(16); | |||||||||||||||||||||||||||
| 25 | 3 | my $fone = Math::BigFloat->bone(); # pi | |||||||||||||||||||||||||||
| 26 | 3 | my $one = Math::BigInt->bone(); # hailstone, sin, cos etc | |||||||||||||||||||||||||||
| 27 | 3 | my $two = Math::BigInt->new(2); # hailstone, sin, cos etc | |||||||||||||||||||||||||||
| 28 | 3 | my $three = Math::BigInt->new(3); # hailstone | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | my $five = Math::BigFloat->new(5); # for pi | |||||||||||||||||||||||||||
| 31 | 3 | my $twothreenine = Math::BigFloat->new(239); # for pi | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # In scalar context this returns the prime count (# of primes <= N). | ||||||||||||||||||||||||||||
| 34 | # In array context it returns a list of primes from 2 to N. | ||||||||||||||||||||||||||||
| 35 | 16 | ▶ | sub primes { | ||||||||||||||||||||||||||
|
Subroutine
primes: called CC=17 SCAR=33.4
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 16 | my $end = shift; | |||||||||||||||||||||||||||
| 37 | 16 | ▶ | return unless defined $end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 16 | ▶ | $end = $end->numify() if ref($end) =~ /^Math::Big/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($end < 2) { return !wantarray ? Math::BigInt->bzero() : (); } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($end < 3) { return !wantarray ? $one->copy : ($two->copy); } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 16 | ▶ | if ($end < 5) { return !wantarray ? $two->copy : ($two->copy, $three->copy); } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 13 | ▶ | $end-- unless ($end & 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 13 | my $s_end = $end >> 1; | |||||||||||||||||||||||||||
| 45 | 13 | my $whole = int( ($end>>1) / 15); | |||||||||||||||||||||||||||
| 46 | # Be conservative. This would result in terabytes of array output. | ||||||||||||||||||||||||||||
| 47 | 13 | ▶ | die "Cannot return $end primes!" if $whole > 1_145_324_612; # ~32 GB string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 13 | my $sieve = "100010010010110" . "011010010010110" x $whole; | |||||||||||||||||||||||||||
| 49 | 13 | substr($sieve, $s_end+1) = ''; # Clip to the right number of entries | |||||||||||||||||||||||||||
| 50 | 13 | my ($n, $limit) = ( 7, int(sqrt($end)) ); | |||||||||||||||||||||||||||
| 51 | 13 | ▶ | while ( $n <= $limit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | for (my $s = ($n*$n) >> 1; $s <= $s_end; $s += $n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | substr($sieve, $s, 1) = '1'; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | do { $n += 2 } while substr($sieve, $n>>1, 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | ▶ | return Math::BigInt->new(1 + $sieve =~ tr/0//) if !wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 12 | my @primes = (2, 3, 5); | |||||||||||||||||||||||||||
| 61 | 12 | $n = 7-2; | |||||||||||||||||||||||||||
| 62 | 12 | foreach my $s (split("0", substr($sieve, 3), -1)) { | |||||||||||||||||||||||||||
| 63 | 44 | $n += 2 + 2 * length($s); | |||||||||||||||||||||||||||
| 64 | 44 | ▶ | push @primes, $n if $n <= $end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 12 | return map { Math::BigInt->new($_) } @primes; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub fibonacci | ||||||||||||||||||||||||||||
| 70 | 22 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fibonacci: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 22 | my $x = shift; | |||||||||||||||||||||||||||
| 72 | 22 | ▶ | $x = Math::BigInt -> new($x) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x and $x->isa("Math::BigInt")
MC/DC0%0/2
ref $x and $x->isa("Math::BigInt")
ref $x
$x->isa("Math::BigInt")
Truth table33%1/3
A and B
A= ref $xB= $x->isa("Math::BigInt")
| |||||||||||||||||||||||||||||
| 73 | unless ref($x) && $x -> isa("Math::BigInt"); | ||||||||||||||||||||||||||||
| 74 | 22 | $x -> bfib(); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub base | ||||||||||||||||||||||||||||
| 78 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
base: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 7 | my ($number,$base) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | ▶ | $number = Math::BigInt->new($number) unless ref $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 7 | ▶ | $base = Math::BigInt->new($base) unless ref $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 7 | ▶ | return if $number < $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 7 | my $n = Math::BigInt->new(0); | |||||||||||||||||||||||||||
| 86 | 7 | my $trial = $base; | |||||||||||||||||||||||||||
| 87 | # 9 = 2**3 + 1 | ||||||||||||||||||||||||||||
| 88 | 7 | ▶ | while ($trial < $number) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 20 | $trial *= $base; $n++; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 7 | $trial /= $base; $a = $number - $trial; | |||||||||||||||||||||||||||
| 93 | 7 | ($n,$a); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub to_base | ||||||||||||||||||||||||||||
| 97 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_base: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 10 | my $x = shift; | |||||||||||||||||||||||||||
| 99 | 10 | ▶ | $x = Math::BigInt->new($x) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x and $x->isa("Math::BigInt")
MC/DC0%0/2
ref $x and $x->isa("Math::BigInt")
ref $x
$x->isa("Math::BigInt")
Truth table33%1/3
A and B
A= ref $xB= $x->isa("Math::BigInt")
| |||||||||||||||||||||||||||||
| 100 | unless ref($x) && $x -> isa("Math::BigInt"); | ||||||||||||||||||||||||||||
| 101 | 10 | $x -> to_base(@_); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub hailstone | ||||||||||||||||||||||||||||
| 105 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
hailstone: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | # return in list context the hailstone sequence, in scalar context the | ||||||||||||||||||||||||||||
| 107 | # number of steps to reach 1 | ||||||||||||||||||||||||||||
| 108 | 11 | my ($n) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 11 | ▶ | $n = Math::BigInt->new($n) unless ref $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 11 | ▶ | return if $n->is_nan() || $n->is_negative(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n->is_nan or $n->is_negative
MC/DC0%0/2
$n->is_nan or $n->is_negative
$n->is_nan
$n->is_negative
Truth table33%1/3
A or B
A= $n->is_nanB= $n->is_negative
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Use the Math::BigInt lib directly for more speed, since all numbers | ||||||||||||||||||||||||||||
| 115 | # involved are positive integers. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 11 | my $lib = Math::BigInt->config()->{lib}; | |||||||||||||||||||||||||||
| 118 | 11 | $n = $n->{value}; | |||||||||||||||||||||||||||
| 119 | 11 | my $three_ = $three->{value}; | |||||||||||||||||||||||||||
| 120 | 11 | my $two_ = $two->{value}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 11 | ▶ | if (wantarray) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 4 | my @seq; | |||||||||||||||||||||||||||
| 125 | 4 | ▶ | while (! $lib->_is_one($n)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | # push @seq, Math::BigInt->new( $lib->_str($n) ); | ||||||||||||||||||||||||||||
| 128 | 47 | push @seq, bless { value => $lib->_copy($n), sign => '+' }, "Math::BigInt"; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # was: ($n->is_odd()) ? ($n = $n * 3 + 1) : ($n = $n / 2); | ||||||||||||||||||||||||||||
| 131 | 47 | ▶ | if ($lib->_is_odd($n)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 19 | $n = $lib->_mul ($n, $three_); $n = $lib->_inc ($n); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # We now know that $n is at least 10 ( (3 * 3) + 1 ) because $n > 1 | ||||||||||||||||||||||||||||
| 136 | # before we entered, and since $n was odd, it must have been at least | ||||||||||||||||||||||||||||
| 137 | # 3. So the next step is $n /= 2: | ||||||||||||||||||||||||||||
| 138 | 19 | push @seq, bless { value => $lib->_copy($n), sign => '+' }, "Math::BigInt"; | |||||||||||||||||||||||||||
| 139 | # this is better, but slower: | ||||||||||||||||||||||||||||
| 140 | #push @seq, Math::BigInt->new( $lib->_str($n) ); | ||||||||||||||||||||||||||||
| 141 | # next step is $n /= 2 as usual (we save the else {} block, too) | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 47 | $n = $lib->_div($n, $two_); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 4 | push @seq, Math::BigInt->bone(); | |||||||||||||||||||||||||||
| 146 | 4 | return @seq; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 7 | my $i = 1; | |||||||||||||||||||||||||||
| 150 | 7 | ▶ | while (! $lib->_is_one($n)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||
| 152 | 47 | $i++; | |||||||||||||||||||||||||||
| 153 | # was: ($n->is_odd()) ? ($n = $n * 3 + 1) : ($n = $n / 2); | ||||||||||||||||||||||||||||
| 154 | 47 | ▶ | if ($lib->_is_odd($n)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 18 | $n = $lib->_mul ($n, $three_); $n = $lib->_inc ($n); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # We now know that $n is at least 10 ( (3 * 3) + 1 ) because $n > 1 | ||||||||||||||||||||||||||||
| 159 | # before we entered, and since $n was odd, it must have been at least 3. | ||||||||||||||||||||||||||||
| 160 | # So the next step is $n /= 2 as usual (we save the else {} block, too). | ||||||||||||||||||||||||||||
| 161 | 18 | $i++; # one more (we know that $n cannot be 1) | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 47 | $n = $lib->_div($n, $two_); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 7 | Math::BigInt->new($i); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub factorial | ||||||||||||||||||||||||||||
| 169 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
factorial: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | # calculate n! - use Math::BigInt bfac() for speed | ||||||||||||||||||||||||||||
| 171 | 7 | my ($n) = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 7 | ▶ | if (ref($n)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 0 | $n->copy()->bfac(); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else | ||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 7 | Math::BigInt->new($n)->bfac(); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub bernoulli | ||||||||||||||||||||||||||||
| 184 | 61 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
bernoulli: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | # returns the nth Bernoulli number. In scalar context as Math::BigFloat | ||||||||||||||||||||||||||||
| 186 | # fraction, in list context as two Math:BigFloat objects, which, if divided, | ||||||||||||||||||||||||||||
| 187 | # give the same result. The series runs this: | ||||||||||||||||||||||||||||
| 188 | # 1/6, 1/30, 1/42, 1/30, 5/66, 691/2730, etc | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Since I do not have yet a way to compute this, I have a table of the | ||||||||||||||||||||||||||||
| 191 | # first 40. So bernoulli(41) will fail for now. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 61 | my $n = shift; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 61 | ▶ | return if $n < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 61 | my @table_1 = ( 1,1, -1,2 ); # 0, 1 | |||||||||||||||||||||||||||
| 197 | 61 | my @table = ( | |||||||||||||||||||||||||||
| 198 | 1,6, -1,30, 1,42, -1,30, 5,66, -691,2730, # 2, 4, | ||||||||||||||||||||||||||||
| 199 | 7,6, -3617,510, 43867,798, | ||||||||||||||||||||||||||||
| 200 | -174611,330, | ||||||||||||||||||||||||||||
| 201 | 854513,138, | ||||||||||||||||||||||||||||
| 202 | '-236364091',2730, | ||||||||||||||||||||||||||||
| 203 | '8553103',6, | ||||||||||||||||||||||||||||
| 204 | '-23749461029',870, | ||||||||||||||||||||||||||||
| 205 | '8615841276005',14322, | ||||||||||||||||||||||||||||
| 206 | '-7709321041217',510, | ||||||||||||||||||||||||||||
| 207 | '2577687858367',6, | ||||||||||||||||||||||||||||
| 208 | '-26315271553053477373',1919190, | ||||||||||||||||||||||||||||
| 209 | '2929993913841559',6, | ||||||||||||||||||||||||||||
| 210 | '-261082718496449122051',13530, # 40 | ||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | 61 | my ($a,$b); | |||||||||||||||||||||||||||
| 213 | 61 | ▶ | if ($n < 2) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | 2 | $a = Math::BigFloat->new($table_1[$n*2]); | |||||||||||||||||||||||||||
| 216 | 2 | $b = Math::BigFloat->new($table_1[$n*2+1]); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | # n is odd: | ||||||||||||||||||||||||||||
| 219 | elsif (($n & 1) == 1) | ||||||||||||||||||||||||||||
| 220 | { | ||||||||||||||||||||||||||||
| 221 | 20 | $a = Math::BigFloat->bzero(); | |||||||||||||||||||||||||||
| 222 | 20 | $b = Math::BigFloat->bone(); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | elsif ($n <= 40) | ||||||||||||||||||||||||||||
| 225 | { | ||||||||||||||||||||||||||||
| 226 | 39 | $n -= 2; | |||||||||||||||||||||||||||
| 227 | 39 | $a = Math::BigFloat->new($table[$n]); | |||||||||||||||||||||||||||
| 228 | 39 | $b = Math::BigFloat->new($table[$n+1]); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | else | ||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||
| 232 | 0 | ▶ | die 'Bernoulli numbers over 40 not yet implemented.' if $n > 40; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 61 | ▶ | wantarray ? ($a,$b): $a/$b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | sub euler | ||||||||||||||||||||||||||||
| 238 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
euler: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | # Calculate Euler's number. | ||||||||||||||||||||||||||||
| 240 | # first argument is x, so that result is e ** x | ||||||||||||||||||||||||||||
| 241 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 242 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 243 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 244 | 4 | my $x = $_[0]; | |||||||||||||||||||||||||||
| 245 | 4 | ▶ | $x = Math::BigFloat->new($x) if !ref($x) || (!$x->isa('Math::BigFloat')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x and $x->isa("Math::BigFloat")
MC/DC0%0/2
ref $x and $x->isa("Math::BigFloat")
ref $x
$x->isa("Math::BigFloat")
Truth table33%1/3
A and B
A= ref $xB= $x->isa("Math::BigFloat")
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 4 | $x->bexp($_[1]); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | sub sin | ||||||||||||||||||||||||||||
| 251 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
sin: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | # calculate sinus | ||||||||||||||||||||||||||||
| 253 | # first argument is x, so that result is sin(x) | ||||||||||||||||||||||||||||
| 254 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 255 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 256 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 257 | 8 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 8 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 8 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # taylor: x^3 x^5 x^7 x^9 | ||||||||||||||||||||||||||||
| 263 | # sin = x - --- + --- - --- + --- ... | ||||||||||||||||||||||||||||
| 264 | # 3! 5! 7! 9! | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # difference for each term is thus x^2 and 1,2 | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 8 | my $sin = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 269 | 8 | my $sign = 1; # start with -= | |||||||||||||||||||||||||||
| 270 | 8 | my $x2 = $x * $x; # X ^ 2, difference between terms | |||||||||||||||||||||||||||
| 271 | 8 | my $over = $x2 * $x; # X ^ 3 | |||||||||||||||||||||||||||
| 272 | 8 | my $below = Math::BigFloat->new(6); my $factorial = Math::BigFloat->new(4); | |||||||||||||||||||||||||||
| 273 | 8 | ▶ | while ($sin->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | { | ||||||||||||||||||||||||||||
| 275 | 46 | $last = $sin->copy(); | |||||||||||||||||||||||||||
| 276 | 46 | ▶ | if ($sign == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||
| 278 | 22 | $sin += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | else | ||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||
| 282 | 24 | $sin -= $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 46 | $sign = 1-$sign; # alternate | |||||||||||||||||||||||||||
| 285 | 46 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 286 | 46 | $below *= $factorial; $factorial++; # n*(n+1) | |||||||||||||||||||||||||||
| 287 | 46 | $below *= $factorial; $factorial++; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 8 | $sin->bround($d-1); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | sub cos | ||||||||||||||||||||||||||||
| 293 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cos: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 294 | # calculate cosinus | ||||||||||||||||||||||||||||
| 295 | # first argument is x, so that result is cos(x) | ||||||||||||||||||||||||||||
| 296 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 297 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 298 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 299 | 8 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 8 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 8 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # taylor: x^2 x^4 x^6 x^8 | ||||||||||||||||||||||||||||
| 305 | # cos = 1 - --- + --- - --- + --- ... | ||||||||||||||||||||||||||||
| 306 | # 2! 4! 6! 8! | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # difference for each term is thus x^2 and 1,2 | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 8 | my $cos = Math::BigFloat->bone(); my $last = 0; | |||||||||||||||||||||||||||
| 311 | 8 | my $over = $x * $x; # X ^ 2 | |||||||||||||||||||||||||||
| 312 | 8 | my $x2 = $over->copy(); # X ^ 2; difference between terms | |||||||||||||||||||||||||||
| 313 | 8 | my $sign = 1; # start with -= | |||||||||||||||||||||||||||
| 314 | 8 | my $below = Math::BigFloat->new(2); my $factorial = Math::BigFloat->new(3); | |||||||||||||||||||||||||||
| 315 | 8 | ▶ | while ($cos->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||
| 317 | 52 | $last = $cos->copy(); | |||||||||||||||||||||||||||
| 318 | 52 | ▶ | if ($sign == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | { | ||||||||||||||||||||||||||||
| 320 | 24 | $cos += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | else | ||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||
| 324 | 28 | $cos -= $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | 52 | $sign = 1-$sign; # alternate | |||||||||||||||||||||||||||
| 327 | 52 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 328 | 52 | $below *= $factorial; $factorial++; # n*(n+1) | |||||||||||||||||||||||||||
| 329 | 52 | $below *= $factorial; $factorial++; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | 8 | $cos->round($d-1); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub tan | ||||||||||||||||||||||||||||
| 335 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
tan: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 336 | # calculate tangens | ||||||||||||||||||||||||||||
| 337 | # first argument is x, so that result is tan(x) | ||||||||||||||||||||||||||||
| 338 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 339 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 340 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 341 | 3 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 3 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 3 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # taylor: 1 2 3 4 5 | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # x^3 x^5 x^7 x^9 | ||||||||||||||||||||||||||||
| 349 | # tan = x + 1 * ----- + 2 * ----- + 17 * ----- + 62 * ----- ... | ||||||||||||||||||||||||||||
| 350 | # 3 15 315 2835 | ||||||||||||||||||||||||||||
| 351 | # | ||||||||||||||||||||||||||||
| 352 | # 2^2n * ( 2^2n - 1) * Bn * x^(2n-1) 256*255 * 1 * x^7 17 | ||||||||||||||||||||||||||||
| 353 | # ---------------------------------- : n=4: ----------------- = --- * x^7 | ||||||||||||||||||||||||||||
| 354 | # (2n)! 40320 * 30 315 | ||||||||||||||||||||||||||||
| 355 | # | ||||||||||||||||||||||||||||
| 356 | # 8! = 40320, B4 (Bernoully number 4) = 1/30 | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # for each term we need: 2^2n, but if we have 2^2(n-1) we use n = (n-1)*2 | ||||||||||||||||||||||||||||
| 359 | # 2 copy, 7 bmul, 2 bdiv, 3 badd, 1 bernoulli | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 3 | my $tan = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 362 | 3 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 363 | 3 | my $over = $x2*$x; | |||||||||||||||||||||||||||
| 364 | 3 | my $below = Math::BigFloat->new(24); # (1*2*3*4) (2n)! | |||||||||||||||||||||||||||
| 365 | 3 | my $factorial = Math::BigFloat->new(5); # for next (2n)! | |||||||||||||||||||||||||||
| 366 | 3 | my $two_n = Math::BigFloat->new(16); # 2^2n | |||||||||||||||||||||||||||
| 367 | 3 | my $two_factor = Math::BigFloat->new(4); # 2^2(n+1) = $two_n * $two_factor | |||||||||||||||||||||||||||
| 368 | 3 | my ($b,$b1,$b2); $b = 4; | |||||||||||||||||||||||||||
| 369 | 3 | ▶ | while ($tan->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | { | ||||||||||||||||||||||||||||
| 371 | 19 | $last = $tan->copy(); | |||||||||||||||||||||||||||
| 372 | 19 | ($b1,$b2) = bernoulli($b); | |||||||||||||||||||||||||||
| 373 | 19 | $tan += $over->copy()->bmul($two_n)->bmul($two_n - $fone)->bmul($b1->babs())->bdiv($below,$d)->bdiv($b2,$d); | |||||||||||||||||||||||||||
| 374 | 19 | $over *= $x2; # x^3, x^5 etc | |||||||||||||||||||||||||||
| 375 | 19 | $below *= $factorial; $factorial++; # n*(n+1) | |||||||||||||||||||||||||||
| 376 | 19 | $below *= $factorial; $factorial++; | |||||||||||||||||||||||||||
| 377 | 19 | $two_n *= $two_factor; # 2^2(n+1) = 2^2n * 4 | |||||||||||||||||||||||||||
| 378 | 19 | $b += 2; # next bernoulli index | |||||||||||||||||||||||||||
| 379 | 19 | ▶ | last if $b > 40; # safeguard | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | 3 | $tan->round($d-1); | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | sub sinh | ||||||||||||||||||||||||||||
| 385 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
sinh: called CC=5 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 386 | # calculate sinus hyperbolicus | ||||||||||||||||||||||||||||
| 387 | # first argument is x, so that result is sinh(x) | ||||||||||||||||||||||||||||
| 388 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 389 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 390 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 391 | 2 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 2 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 2 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | # taylor: x^3 x^5 x^7 | ||||||||||||||||||||||||||||
| 397 | # sinh = x + --- + --- + --- ... | ||||||||||||||||||||||||||||
| 398 | # 3! 5! 7! | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # difference for each term is thus x^2 and 1,2 | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 2 | my $sinh = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 403 | 2 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 404 | 2 | my $over = $x2 * $x; my $below = Math::BigFloat->new(6); my $factorial = Math::BigFloat->new(4); | |||||||||||||||||||||||||||
| 405 | 2 | ▶ | while ($sinh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on accuracy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | { | ||||||||||||||||||||||||||||
| 407 | 0 | $last = $sinh->copy(); | |||||||||||||||||||||||||||
| 408 | 0 | $sinh += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 409 | 0 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 410 | 0 | $below *= $factorial; $factorial++; # n*(n+1) | |||||||||||||||||||||||||||
| 411 | 0 | $below *= $factorial; $factorial++; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | 2 | $sinh->bround($d-1); | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | sub cosh | ||||||||||||||||||||||||||||
| 417 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cosh: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 418 | # calculate cosinus hyperbolicus | ||||||||||||||||||||||||||||
| 419 | # first argument is x, so that result is cosh(x) | ||||||||||||||||||||||||||||
| 420 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 421 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 422 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 423 | 0 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 0 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 0 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | # taylor: x^2 x^4 x^6 | ||||||||||||||||||||||||||||
| 429 | # cosh = x + --- + --- + --- ... | ||||||||||||||||||||||||||||
| 430 | # 2! 4! 6! | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # difference for each term is thus x^2 and 1,2 | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 0 | my $cosh = Math::BigFloat->bone(); my $last = 0; | |||||||||||||||||||||||||||
| 435 | 0 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 436 | 0 | my $over = $x2; my $below = Math::BigFloat->new(); my $factorial = Math::BigFloat->new(3); | |||||||||||||||||||||||||||
| 437 | 0 | ▶ | while ($cosh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on accuracy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | { | ||||||||||||||||||||||||||||
| 439 | 0 | $last = $cosh->copy(); | |||||||||||||||||||||||||||
| 440 | 0 | $cosh += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 441 | 0 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 442 | 0 | $below *= $factorial; $factorial++; # n*(n+1) | |||||||||||||||||||||||||||
| 443 | 0 | $below *= $factorial; $factorial++; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | 0 | $cosh->bround($d-1); | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | sub arctan | ||||||||||||||||||||||||||||
| 449 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
arctan: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 450 | # calculate arcus tangens | ||||||||||||||||||||||||||||
| 451 | # first argument is x, so that result is arctan(x) | ||||||||||||||||||||||||||||
| 452 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 453 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 454 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 455 | 8 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | 8 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 8 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | # taylor: x^3 x^5 x^7 x^9 | ||||||||||||||||||||||||||||
| 461 | # arctan = x - --- + --- - --- + --- ... | ||||||||||||||||||||||||||||
| 462 | # 3 5 7 9 | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | # difference for each term is thus x^2 and 2: | ||||||||||||||||||||||||||||
| 465 | # 2 copy, 1 bmul, 1 badd, 1 bdiv | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 8 | my $arctan = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 468 | 8 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 469 | 8 | my $over = $x2*$x; my $below = Math::BigFloat->new(3); my $add = Math::BigFloat->new(2); | |||||||||||||||||||||||||||
| 470 | 8 | my $sign = 1; | |||||||||||||||||||||||||||
| 471 | 8 | ▶ | while ($arctan->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 472 | { | ||||||||||||||||||||||||||||
| 473 | 85 | $last = $arctan->copy(); | |||||||||||||||||||||||||||
| 474 | 85 | ▶ | if ($sign == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | { | ||||||||||||||||||||||||||||
| 476 | 42 | $arctan += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | else | ||||||||||||||||||||||||||||
| 479 | { | ||||||||||||||||||||||||||||
| 480 | 43 | $arctan -= $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | 85 | $sign = 1-$sign; # alternate | |||||||||||||||||||||||||||
| 483 | 85 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 484 | 85 | $below += $add; | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | 8 | $arctan->bround($d-1); | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | sub arctanh | ||||||||||||||||||||||||||||
| 490 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
arctanh: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 491 | # calculate arcus tangens hyperbolicus | ||||||||||||||||||||||||||||
| 492 | # first argument is x, so that result is arctanh(x) | ||||||||||||||||||||||||||||
| 493 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 494 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 495 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 496 | 2 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 2 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 2 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # taylor: x^3 x^5 x^7 x^9 | ||||||||||||||||||||||||||||
| 502 | # arctanh = x + --- + --- + --- + --- + ... | ||||||||||||||||||||||||||||
| 503 | # 3 5 7 9 | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | # difference for each term is thus x^2 and 2: | ||||||||||||||||||||||||||||
| 506 | # 2 copy, 1 bmul, 1 badd, 1 bdiv | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 2 | my $arctanh = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 509 | 2 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 510 | 2 | my $over = $x2*$x; my $below = Math::BigFloat->new(3); my $add = Math::BigFloat->new(2); | |||||||||||||||||||||||||||
| 511 | 2 | ▶ | while ($arctanh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 512 | { | ||||||||||||||||||||||||||||
| 513 | 0 | $last = $arctanh->copy(); | |||||||||||||||||||||||||||
| 514 | 0 | $arctanh += $over->copy()->bdiv($below,$d); | |||||||||||||||||||||||||||
| 515 | 0 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 516 | 0 | $below += $add; | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | 2 | $arctanh->bround($d-1); | |||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | sub arcsin | ||||||||||||||||||||||||||||
| 522 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
arcsin: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 523 | # calculate arcus sinus | ||||||||||||||||||||||||||||
| 524 | # first argument is x, so that result is arcsin(x) | ||||||||||||||||||||||||||||
| 525 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 526 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 527 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 528 | 4 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 529 | 4 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 42
MC/DC100%1/1
shift() || 42
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 4 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | # taylor: 1 * x^3 1 * 3 * x^5 1 * 3 * 5 * x^7 | ||||||||||||||||||||||||||||
| 534 | # arcsin = x + ------- + ----------- + --------------- + ... | ||||||||||||||||||||||||||||
| 535 | # 2 * 3 2 * 4 * 5 2 * 4 * 6 * 7 | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | # difference for each term is thus x^2 and two muls (fac1, fac2): | ||||||||||||||||||||||||||||
| 538 | # 3 copy, 3 bmul, 1 bdiv, 3 badd | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 4 | my $arcsin = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 541 | 4 | my $x2 = $x*$x; | |||||||||||||||||||||||||||
| 542 | 4 | my $over = $x2*$x; my $below = Math::BigFloat->new(6); | |||||||||||||||||||||||||||
| 543 | 4 | my $fac1 = Math::BigFloat->new(1); | |||||||||||||||||||||||||||
| 544 | 4 | my $fac2 = Math::BigFloat->new(2); | |||||||||||||||||||||||||||
| 545 | 4 | my $two = Math::BigFloat->new(2); | |||||||||||||||||||||||||||
| 546 | 4 | ▶ | while ($arcsin->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 547 | { | ||||||||||||||||||||||||||||
| 548 | 30 | $last = $arcsin->copy(); | |||||||||||||||||||||||||||
| 549 | 30 | $arcsin += $over->copy()->bmul($fac1)->bdiv($below->copy->bmul($fac2),$d); | |||||||||||||||||||||||||||
| 550 | 30 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 551 | 30 | $below += $one; | |||||||||||||||||||||||||||
| 552 | 30 | $fac1 += $two; | |||||||||||||||||||||||||||
| 553 | 30 | $fac2 += $two; | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | 4 | $arcsin->bround($d-1); | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | sub arcsinh | ||||||||||||||||||||||||||||
| 559 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
arcsinh: called CC=6 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||
| 560 | # calculate arcus sinus hyperbolicus | ||||||||||||||||||||||||||||
| 561 | # first argument is x, so that result is arcsinh(x) | ||||||||||||||||||||||||||||
| 562 | # Second argument is accuracy (number of significant digits), it | ||||||||||||||||||||||||||||
| 563 | # stops when at least so much plus one digits are 'stable' and then | ||||||||||||||||||||||||||||
| 564 | # rounds it. Default is 42. | ||||||||||||||||||||||||||||
| 565 | 2 | ▶ | my $x = shift; $x = 0 if !defined $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 566 | 2 | ▶ | my $d = abs(shift || 42); $d = abs($d)+1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 42
MC/DC0%0/1
shift() || 42
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 2 | ▶ | $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | # taylor: 1 * x^3 1 * 3 * x^5 1 * 3 * 5 * x^7 | ||||||||||||||||||||||||||||
| 571 | # arcsin = x - ------- + ----------- - --------------- + ... | ||||||||||||||||||||||||||||
| 572 | # 2 * 3 2 * 4 * 5 2 * 4 * 6 * 7 | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | # difference for each term is thus x^2 and two muls (fac1, fac2): | ||||||||||||||||||||||||||||
| 575 | # 3 copy, 3 bmul, 1 bdiv, 3 badd | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | 2 | my $arcsinh = $x->copy(); my $last = 0; | |||||||||||||||||||||||||||
| 578 | 2 | my $x2 = $x*$x; my $sign = 0; | |||||||||||||||||||||||||||
| 579 | 2 | my $over = $x2*$x; my $below = 6; | |||||||||||||||||||||||||||
| 580 | 2 | my $fac1 = Math::BigInt->new(1); | |||||||||||||||||||||||||||
| 581 | 2 | my $fac2 = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 582 | 2 | ▶ | while ($arcsinh ne $last) # no $x-$last > $diff because bdiv() limit on A | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 583 | { | ||||||||||||||||||||||||||||
| 584 | 0 | $last = $arcsinh->copy(); | |||||||||||||||||||||||||||
| 585 | 0 | ▶ | if ($sign == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 586 | { | ||||||||||||||||||||||||||||
| 587 | 0 | $arcsinh += $over->copy()->bmul( | |||||||||||||||||||||||||||
| 588 | $fac1)->bdiv($below->copy->bmul($fac2),$d); | ||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | else | ||||||||||||||||||||||||||||
| 591 | { | ||||||||||||||||||||||||||||
| 592 | 0 | $arcsinh -= $over->copy()->bmul( | |||||||||||||||||||||||||||
| 593 | $fac1)->bdiv($below->copy->bmul($fac2),$d); | ||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | 0 | $over *= $x2; # $x*$x | |||||||||||||||||||||||||||
| 596 | 0 | $below += $one; | |||||||||||||||||||||||||||
| 597 | 0 | $fac1 += $two; | |||||||||||||||||||||||||||
| 598 | 0 | $fac2 += $two; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | 2 | $arcsinh->round($d-1); | |||||||||||||||||||||||||||
| 601 | } | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | sub log | ||||||||||||||||||||||||||||
| 604 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 605 | 5 | my ($x,$base,$d) = @_; | |||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 5 | my $y; | |||||||||||||||||||||||||||
| 608 | 5 | ▶ | if (!ref($x) || !$x->isa('Math::BigFloat')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x && $x->isa('Math::BigFloat')
MC/DC0%0/2
ref $x && $x->isa('Math::BigFloat')
ref $x
$x->isa('Math::BigFloat')
Truth table33%1/3
A && B
A= ref $xB= $x->isa('Math::BigFloat')
| |||||||||||||||||||||||||||||
| 609 | { | ||||||||||||||||||||||||||||
| 610 | 5 | $y = Math::BigFloat->new($x); | |||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||
| 612 | else | ||||||||||||||||||||||||||||
| 613 | { | ||||||||||||||||||||||||||||
| 614 | 0 | $y = $x->copy(); | |||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||
| 616 | 5 | $y->blog($base,$d); | |||||||||||||||||||||||||||
| 617 | 5 | $y; | |||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | sub pi | ||||||||||||||||||||||||||||
| 621 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pi: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 622 | # calculate PI (as suggested by Robert Creager) | ||||||||||||||||||||||||||||
| 623 | 2 | ▶ | my $digits = abs(shift || 1024); | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1024
MC/DC0%0/1
shift() || 1024
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 2 | my $d = $digits+5; | |||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 2 | my $pi = $sixteen * arctan( scalar $fone->copy()->bdiv($five,$d), $d ) | |||||||||||||||||||||||||||
| 628 | - $four * arctan( scalar $fone->copy()->bdiv($twothreenine,$d), $d); | ||||||||||||||||||||||||||||
| 629 | 2 | $pi->bround($digits+1); # +1 for the "3." | |||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 3 | 1; | |||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | __END__ | ||||||||||||||||||||||||||||