blib/lib/Math/Simple.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 124 CC 12 SCAR 31.9
CC
12
Cov
0%
gcd
56.0
logb
20.0
max
6.0
CRAP 24.2
1 #!/usr/bin/perl
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Math::Simple - Very simple, commonly used math routines
8  
9 =head1 VERSION
10  
11 Version "2.1.2"
12  
13 =cut
14  
15 ###############################################################
16 # Changes
17 # - 2.1.2 - removed spurious inclusion of uneeded header (Types::Core)
18 # - 2.1.1 - reformat header as it didn't seem to index properly
19 # - 2.1.0 - add gcd function;
20 # - Update docs & clarify logb usage
21 # - added more tests to test suite
22 # - 2.0.3 - change min & max to handle an Array ref as 1st arg
23 # - 2.0.2 - change logb to be a default export as it is basis for
24 # all other log bases (i.e. - log2, log10, log1024...etc)
25 # - 2.0.1 - minor typo's
26 # - 2.0.0 - First public release (no tests yet)
27 #
28 package Math::Simple;
29 our $VERSION='2.1.2';
30  
31 {
32  
33  
34 our @__baselogs__;
35 BEGIN {
36 require $_.".pm" && $_->import for qw(strict warnings);
37 $__baselogs__[10]=log(10);
38 $__baselogs__[2]=log(2);
39 }
40 our @EXPORT = qw(max min logb log10 gcd);
41 our @EXPORT_OK = qw(log2 );
42 use Xporter;
43  
44 {
45 sub logb($;$){
46 die "logb requires 1 or 2 params" unless @_;
47 if (@_==2) {
48 return log($_[1]) / ($__baselogs__[$_[0]] ||= log $_[0]) }
49 elsif ( @_==1) {
50 my $base = $__baselogs__[$_[0]] ||= log $_[0];
51 return sub ($) {
52 log($_[0]) / $base;
53 }
54 }
55 }
56 }
57  
58 BEGIN {
59 sub log2 ($) { logb(2, $_[0]) }
60 sub log10 ($) { logb(10, $_[0]) }
61 }
62 sub max ($;@);
63 sub min ($;@);
64  
65 sub max ($;@) { @_ == 2 and return $_[0] >= $_[1] ? $_[0] : $_[1];
66 @_ == 1 and return $_[0];
67 max( max(pop @_, pop @_), @_);
68 }
69  
70 sub min ($;@) { @_ == 2 and return $_[0] <= $_[1] ? $_[0] : $_[1];
71 @_ == 1 and return $_[0];
72 min( min(pop @_, pop @_), @_);
73 }
74 sub gcd($$) {
75 my ($sign, $num, $den, $gcd) = (1, @_);
76 if ($num == 0) { $den=$gcd=1 }
77 elsif ($den) {
78 if ($den < 0) {$num = -$num,$den = -$den}
79 if ($num < 0) {$sign= - 1, $num = -$num}
80 my ($x, $y) = $num<$den ? ($num,$den) : ($den, $num);
81 my $m; while ($y && ($m = $x % $y)) {$x = $y; $y = $m}
82 $gcd = $y;
83 } else {$gcd=1, $num=0, $den=1}
84 unless ($gcd) {$gcd=1; return 1}
85 $num /= $gcd*$sign; $den /= $gcd;
86 $gcd;
87 }
88 1}
89 ###########################################################################
90 # Pod documentation {{{1
91 # use Math::Simple
92  
93 =head1 SYNOPSIS
94  
95 use Math::Simple qw(log2);
96 my $low = min($a,$b, $c, $d ...); # least positive num
97 my $hi = max($a,$b); # most positive num
98 my $digits_ƒ = sub ($) {int log10($_[0]); # log10 default export
99 my $log_in_base = logb($base,$num); # log arbitrary base
100 my $log16_ƒ = logb(16); # create log16 func
101 my $bits_needed_ƒ = sub ($) {int log2($_[0])};
102 use constant nbits => log2(~1); # compile constant
103 my $gcd = gcd(42,12)
104  
105 =head1 DESCRIPTION
106  
107 Very simple math routines that I end up re-using in many progs and
108 libified for easy access.
109  
110 Most of the functions are exported by default, with easy options
111 to unexport the ones you don't.
112 As of this version, default exports are C<min>, C<max>, C<log10>, C<logb>,
113 and C<gcd>, with C<log2> being an optional export.
114  
115 I<Note> on the C<logb> function. It returns a function or a result
116 based on the number of inputs. If one input is given, it is a
117 logarithm function factory -- producing a specialized, or I<curried>
118 log function for the base given. If the C<logb> function is given
119 two parameters, it will return the logarithm of the second
120 number in the base of the first.
121  
122 C<Math::Simple> uses L<Xporter>, so including C<log2> or
123 any future additions won't break the default C<EXPORT> list see L<Xporter>.
124  
125  
126 =head2 Performance Note
127  
128 In order to calculate the log in another base, one must first take
129 the log of the base in a known base (C<Perl>'s natural logarithm function
130 is used for this). This means that non-natural logarithm bases normally
131 require 2 logarithms/call, however, the bases are cached the first
132 time used so future calls will only need the logarithm function called
133 on the new data.
134  
135 =cut
136  
137