blib/lib/Math/Big.pm untested

filter: Statementstmt 0.0% 0 / 340 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 35 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 527 CC 55 SCAR 38.5
CC
55
Cov
0%
primes
182.0
bernoulli
56.0
hailstone
42.0
CRAP 46.9
1 #############################################################################
2 # Math/Big.pm -- usefull routines with Big numbers (BigInt/BigFloat)
3  
4 package Math::Big;
5  
6 require 5.006002; # anything lower is simple untested
7  
8 use strict;
9 use warnings;
10  
11 use Math::BigInt '1.97';
12 use Math::BigFloat;
13 use Exporter;
14  
15 our $VERSION = '1.15';
16 our @ISA = qw( Exporter );
17 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 my $four = Math::BigFloat->new(4);
24 my $sixteen = Math::BigFloat->new(16);
25 my $fone = Math::BigFloat->bone(); # pi
26 my $one = Math::BigInt->bone(); # hailstone, sin, cos etc
27 my $two = Math::BigInt->new(2); # hailstone, sin, cos etc
28 my $three = Math::BigInt->new(3); # hailstone
29  
30 my $five = Math::BigFloat->new(5); # for pi
31 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 sub primes {
36 my $end = shift;
37 return unless defined $end;
38 $end = $end->numify() if ref($end) =~ /^Math::Big/;
39 if ($end < 2) { return !wantarray ? Math::BigInt->bzero() : (); }
40 if ($end < 3) { return !wantarray ? $one->copy : ($two->copy); }
41 if ($end < 5) { return !wantarray ? $two->copy : ($two->copy, $three->copy); }
42  
43 $end-- unless ($end & 1);
44 my $s_end = $end >> 1;
45 my $whole = int( ($end>>1) / 15);
46 # Be conservative. This would result in terabytes of array output.
47 die "Cannot return $end primes!" if $whole > 1_145_324_612; # ~32 GB string
48 my $sieve = "100010010010110" . "011010010010110" x $whole;
49 substr($sieve, $s_end+1) = ''; # Clip to the right number of entries
50 my ($n, $limit) = ( 7, int(sqrt($end)) );
51 while ( $n <= $limit ) {
52 for (my $s = ($n*$n) >> 1; $s <= $s_end; $s += $n) {
53 substr($sieve, $s, 1) = '1';
54 }
55 do { $n += 2 } while substr($sieve, $n>>1, 1);
56 }
57  
58 return Math::BigInt->new(1 + $sieve =~ tr/0//) if !wantarray;
59  
60 my @primes = (2, 3, 5);
61 $n = 7-2;
62 foreach my $s (split("0", substr($sieve, 3), -1)) {
63 $n += 2 + 2 * length($s);
64 push @primes, $n if $n <= $end;
65 }
66 return map { Math::BigInt->new($_) } @primes;
67 }
68  
69 sub fibonacci
70 {
71 my $x = shift;
72 $x = Math::BigInt -> new($x)
73 unless ref($x) && $x -> isa("Math::BigInt");
74 $x -> bfib();
75 }
76  
77 sub base
78 {
79 my ($number,$base) = @_;
80  
81 $number = Math::BigInt->new($number) unless ref $number;
82 $base = Math::BigInt->new($base) unless ref $base;
83  
84 return if $number < $base;
85 my $n = Math::BigInt->new(0);
86 my $trial = $base;
87 # 9 = 2**3 + 1
88 while ($trial < $number)
89 {
90 $trial *= $base; $n++;
91 }
92 $trial /= $base; $a = $number - $trial;
93 ($n,$a);
94 }
95  
96 sub to_base
97 {
98 my $x = shift;
99 $x = Math::BigInt->new($x)
100 unless ref($x) && $x -> isa("Math::BigInt");
101 $x -> to_base(@_);
102 }
103  
104 sub hailstone
105 {
106 # return in list context the hailstone sequence, in scalar context the
107 # number of steps to reach 1
108 my ($n) = @_;
109  
110 $n = Math::BigInt->new($n) unless ref $n;
111  
112 return if $n->is_nan() || $n->is_negative();
113  
114 # Use the Math::BigInt lib directly for more speed, since all numbers
115 # involved are positive integers.
116  
117 my $lib = Math::BigInt->config()->{lib};
118 $n = $n->{value};
119 my $three_ = $three->{value};
120 my $two_ = $two->{value};
121  
122 if (wantarray)
123 {
124 my @seq;
125 while (! $lib->_is_one($n))
126 {
127 # push @seq, Math::BigInt->new( $lib->_str($n) );
128 push @seq, bless { value => $lib->_copy($n), sign => '+' }, "Math::BigInt";
129  
130 # was: ($n->is_odd()) ? ($n = $n * 3 + 1) : ($n = $n / 2);
131 if ($lib->_is_odd($n))
132 {
133 $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 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 $n = $lib->_div($n, $two_);
144 }
145 push @seq, Math::BigInt->bone();
146 return @seq;
147 }
148  
149 my $i = 1;
150 while (! $lib->_is_one($n))
151 {
152 $i++;
153 # was: ($n->is_odd()) ? ($n = $n * 3 + 1) : ($n = $n / 2);
154 if ($lib->_is_odd($n))
155 {
156 $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 $i++; # one more (we know that $n cannot be 1)
162 }
163 $n = $lib->_div($n, $two_);
164 }
165 Math::BigInt->new($i);
166 }
167  
168 sub factorial
169 {
170 # calculate n! - use Math::BigInt bfac() for speed
171 my ($n) = shift;
172  
173 if (ref($n))
174 {
175 $n->copy()->bfac();
176 }
177 else
178 {
179 Math::BigInt->new($n)->bfac();
180 }
181 }
182  
183 sub bernoulli
184 {
185 # returns the nth Bernoulli number. In scalar context as Math::BigFloat
186 # fraction, in list context as two Math:BigFloats, which, if divided, give
187 # 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 my $n = shift;
194  
195 return if $n < 0;
196 my @table_1 = ( 1,1, -1,2 ); # 0, 1
197 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 my ($a,$b);
213 if ($n < 2)
214 {
215 $a = Math::BigFloat->new($table_1[$n*2]);
216 $b = Math::BigFloat->new($table_1[$n*2+1]);
217 }
218 # n is odd:
219 elsif (($n & 1) == 1)
220 {
221 $a = Math::BigFloat->bzero();
222 $b = Math::BigFloat->bone();
223 }
224 elsif ($n <= 40)
225 {
226 $n -= 2;
227 $a = Math::BigFloat->new($table[$n]);
228 $b = Math::BigFloat->new($table[$n+1]);
229 }
230 else
231 {
232 die 'Bernoulli numbers over 40 not yet implemented.' if $n > 40;
233 }
234 wantarray ? ($a,$b): $a/$b;
235 }
236  
237 sub euler
238 {
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 my $x = $_[0];
245 $x = Math::BigFloat->new($x) if !ref($x) || (!$x->isa('Math::BigFloat'));
246  
247 $x->bexp($_[1]);
248 }
249  
250 sub sin
251 {
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 my $x = shift; $x = 0 if !defined $x;
258 my $d = abs(shift || 42); $d = abs($d)+1;
259  
260 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $sin = $x->copy(); my $last = 0;
269 my $sign = 1; # start with -=
270 my $x2 = $x * $x; # X ^ 2, difference between terms
271 my $over = $x2 * $x; # X ^ 3
272 my $below = Math::BigFloat->new(6); my $factorial = Math::BigFloat->new(4);
273 while ($sin->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy
274 {
275 $last = $sin->copy();
276 if ($sign == 0)
277 {
278 $sin += $over->copy()->bdiv($below,$d);
279 }
280 else
281 {
282 $sin -= $over->copy()->bdiv($below,$d);
283 }
284 $sign = 1-$sign; # alternate
285 $over *= $x2; # $x*$x
286 $below *= $factorial; $factorial++; # n*(n+1)
287 $below *= $factorial; $factorial++;
288 }
289 $sin->bround($d-1);
290 }
291  
292 sub cos
293 {
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 my $x = shift; $x = 0 if !defined $x;
300 my $d = abs(shift || 42); $d = abs($d)+1;
301  
302 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $cos = Math::BigFloat->bone(); my $last = 0;
311 my $over = $x * $x; # X ^ 2
312 my $x2 = $over->copy(); # X ^ 2; difference between terms
313 my $sign = 1; # start with -=
314 my $below = Math::BigFloat->new(2); my $factorial = Math::BigFloat->new(3);
315 while ($cos->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy
316 {
317 $last = $cos->copy();
318 if ($sign == 0)
319 {
320 $cos += $over->copy()->bdiv($below,$d);
321 }
322 else
323 {
324 $cos -= $over->copy()->bdiv($below,$d);
325 }
326 $sign = 1-$sign; # alternate
327 $over *= $x2; # $x*$x
328 $below *= $factorial; $factorial++; # n*(n+1)
329 $below *= $factorial; $factorial++;
330 }
331 $cos->round($d-1);
332 }
333  
334 sub tan
335 {
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 my $x = shift; $x = 0 if !defined $x;
342 my $d = abs(shift || 42); $d = abs($d)+1;
343  
344 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $tan = $x->copy(); my $last = 0;
362 my $x2 = $x*$x;
363 my $over = $x2*$x;
364 my $below = Math::BigFloat->new(24); # (1*2*3*4) (2n)!
365 my $factorial = Math::BigFloat->new(5); # for next (2n)!
366 my $two_n = Math::BigFloat->new(16); # 2^2n
367 my $two_factor = Math::BigFloat->new(4); # 2^2(n+1) = $two_n * $two_factor
368 my ($b,$b1,$b2); $b = 4;
369 while ($tan->bcmp($last) != 0) # no $x-$last > $diff because bdiv() limit on accuracy
370 {
371 $last = $tan->copy();
372 ($b1,$b2) = bernoulli($b);
373 $tan += $over->copy()->bmul($two_n)->bmul($two_n - $fone)->bmul($b1->babs())->bdiv($below,$d)->bdiv($b2,$d);
374 $over *= $x2; # x^3, x^5 etc
375 $below *= $factorial; $factorial++; # n*(n+1)
376 $below *= $factorial; $factorial++;
377 $two_n *= $two_factor; # 2^2(n+1) = 2^2n * 4
378 $b += 2; # next bernoulli index
379 last if $b > 40; # safeguard
380 }
381 $tan->round($d-1);
382 }
383  
384 sub sinh
385 {
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 my $x = shift; $x = 0 if !defined $x;
392 my $d = abs(shift || 42); $d = abs($d)+1;
393  
394 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $sinh = $x->copy(); my $last = 0;
403 my $x2 = $x*$x;
404 my $over = $x2 * $x; my $below = Math::BigFloat->new(6); my $factorial = Math::BigFloat->new(4);
405 while ($sinh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on accuracy
406 {
407 $last = $sinh->copy();
408 $sinh += $over->copy()->bdiv($below,$d);
409 $over *= $x2; # $x*$x
410 $below *= $factorial; $factorial++; # n*(n+1)
411 $below *= $factorial; $factorial++;
412 }
413 $sinh->bround($d-1);
414 }
415  
416 sub cosh
417 {
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 my $x = shift; $x = 0 if !defined $x;
424 my $d = abs(shift || 42); $d = abs($d)+1;
425  
426 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $cosh = Math::BigFloat->bone(); my $last = 0;
435 my $x2 = $x*$x;
436 my $over = $x2; my $below = Math::BigFloat->new(); my $factorial = Math::BigFloat->new(3);
437 while ($cosh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on accuracy
438 {
439 $last = $cosh->copy();
440 $cosh += $over->copy()->bdiv($below,$d);
441 $over *= $x2; # $x*$x
442 $below *= $factorial; $factorial++; # n*(n+1)
443 $below *= $factorial; $factorial++;
444 }
445 $cosh->bround($d-1);
446 }
447  
448 sub arctan
449 {
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 my $x = shift; $x = 0 if !defined $x;
456 my $d = abs(shift || 42); $d = abs($d)+1;
457  
458 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $arctan = $x->copy(); my $last = 0;
468 my $x2 = $x*$x;
469 my $over = $x2*$x; my $below = Math::BigFloat->new(3); my $add = Math::BigFloat->new(2);
470 my $sign = 1;
471 while ($arctan->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A
472 {
473 $last = $arctan->copy();
474 if ($sign == 0)
475 {
476 $arctan += $over->copy()->bdiv($below,$d);
477 }
478 else
479 {
480 $arctan -= $over->copy()->bdiv($below,$d);
481 }
482 $sign = 1-$sign; # alternate
483 $over *= $x2; # $x*$x
484 $below += $add;
485 }
486 $arctan->bround($d-1);
487 }
488  
489 sub arctanh
490 {
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 my $x = shift; $x = 0 if !defined $x;
497 my $d = abs(shift || 42); $d = abs($d)+1;
498  
499 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $arctanh = $x->copy(); my $last = 0;
509 my $x2 = $x*$x;
510 my $over = $x2*$x; my $below = Math::BigFloat->new(3); my $add = Math::BigFloat->new(2);
511 while ($arctanh->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A
512 {
513 $last = $arctanh->copy();
514 $arctanh += $over->copy()->bdiv($below,$d);
515 $over *= $x2; # $x*$x
516 $below += $add;
517 }
518 $arctanh->bround($d-1);
519 }
520  
521 sub arcsin
522 {
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 my $x = shift; $x = 0 if !defined $x;
529 my $d = abs(shift || 42); $d = abs($d)+1;
530  
531 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $arcsin = $x->copy(); my $last = 0;
541 my $x2 = $x*$x;
542 my $over = $x2*$x; my $below = Math::BigFloat->new(6);
543 my $fac1 = Math::BigFloat->new(1);
544 my $fac2 = Math::BigFloat->new(2);
545 my $two = Math::BigFloat->new(2);
546 while ($arcsin->bcmp($last)) # no $x-$last > $diff because bdiv() limit on A
547 {
548 $last = $arcsin->copy();
549 $arcsin += $over->copy()->bmul($fac1)->bdiv($below->copy->bmul($fac2),$d);
550 $over *= $x2; # $x*$x
551 $below += $one;
552 $fac1 += $two;
553 $fac2 += $two;
554 }
555 $arcsin->bround($d-1);
556 }
557  
558 sub arcsinh
559 {
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 my $x = shift; $x = 0 if !defined $x;
566 my $d = abs(shift || 42); $d = abs($d)+1;
567  
568 $x = Math::BigFloat->new($x) if ref($x) ne 'Math::BigFloat';
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 my $arcsinh = $x->copy(); my $last = 0;
578 my $x2 = $x*$x; my $sign = 0;
579 my $over = $x2*$x; my $below = 6;
580 my $fac1 = Math::BigInt->new(1);
581 my $fac2 = Math::BigInt->new(2);
582 while ($arcsinh ne $last) # no $x-$last > $diff because bdiv() limit on A
583 {
584 $last = $arcsinh->copy();
585 if ($sign == 0)
586 {
587 $arcsinh += $over->copy()->bmul(
588 $fac1)->bdiv($below->copy->bmul($fac2),$d);
589 }
590 else
591 {
592 $arcsinh -= $over->copy()->bmul(
593 $fac1)->bdiv($below->copy->bmul($fac2),$d);
594 }
595 $over *= $x2; # $x*$x
596 $below += $one;
597 $fac1 += $two;
598 $fac2 += $two;
599 }
600 $arcsinh->round($d-1);
601 }
602  
603 sub log
604 {
605 my ($x,$base,$d) = @_;
606  
607 my $y;
608 if (!ref($x) || !$x->isa('Math::BigFloat'))
609 {
610 $y = Math::BigFloat->new($x);
611 }
612 else
613 {
614 $y = $x->copy();
615 }
616 $y->blog($base,$d);
617 $y;
618 }
619  
620 sub pi
621 {
622 # calculate PI (as suggested by Robert Creager)
623 my $digits = abs(shift || 1024);
624  
625 my $d = $digits+5;
626  
627 my $pi = $sixteen * arctan( scalar $fone->copy()->bdiv($five,$d), $d )
628 - $four * arctan( scalar $fone->copy()->bdiv($twothreenine,$d), $d);
629 $pi->bround($digits+1); # +1 for the "3."
630 }
631  
632 1;
633  
634 __END__