blib/lib/Math/SigDig.pm

filter: Statementstmt 74.7% 86 / 115 Branchbran 66.6% 52 / 78 Conditioncond 50.0% 12 / 24 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 67.3% 161 / 239 CC 49 SCAR 47.2
CC
49
Cov
69%
sigdig
142.8
getsigdig
7.0
BEGIN
1.0
CRAP 112.6
↑ Summary
1 package Math::SigDig;
2  
3 #Robert W. Leach
4 #Princeton University
5 #Carl Icahn Laboratory
6 #Lewis Sigler Institute for Integrative Genomics
7 #Bioinformatics Group
8 #Room 133A
9 #Princeton, NJ 08544
10 #rleach@genomics.princeton.edu
11 #Copyright 2014
12  
13 #NOTICE
14 #
15 #This software (Math::SigDig) and ancillary information (herein
16 #called "SOFTWARE") is free: you can redistribute it and/or modify
17 #it under the terms of the GNU General Public License as published by
18 #the Free Software Foundation, either version 3 of the License, or
19 #(at your option) any later version.
20 #
21 #This software is distributed in the hope that it will be useful,
22 #but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 #GNU General Public License for more details.
25 #
26 #You should have received a copy of the GNU General Public License
27 #along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 #If SOFTWARE is modified to produce derivative works, such modified
30 #SOFTWARE should be clearly marked, so as not to confuse it with this
31 #version.
32  
33  
34  
35 1 ▶use 5.012003;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 $VERSION = 1.00;
43 1 @ISA = qw(Exporter);
44 1 @EXPORT = qw(sigdig getsigdig);
45 1 @EXPORT_OK = ();
46 1 %EXPORT_TAGS = ();
47  
48 sub sigdig
49 9 ▶ {
Subroutine sigdig: called CC=43 SCAR=49.6
Pod undocumented
50 9 my $num = $_[0];
51 9 ▶ my $places = defined($_[1]) ? $_[1] : 3;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 8 1 9
52  
53 #Pad with 0's by adding decimal places up to $places significant digits, or
54 #if $places is 0, up to the pad value (note there can still be numbers in
55 #this case with more significant digits than specified by the pad value)
56 9 ▶ my $pad = defined($_[2]) ? ($places > 0 && $_[2] ? $places : $_[2]) : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $_[2] ? : 4 5 9
$places > 0 && $_[2] ? : 1 3 4
Condition100%3/3
$places > 0 and $_[2]
!ll&&!rl&&r
211
MC/DC100%2/2
$places > 0 and $_[2]
$places > 0 $_[2]
Truth table100%3/3
A and B
A= $places > 0B= $_[2]
ABresult
0X0
100
111
57  
58 9 my $new_num = '';
59  
60 9 ▶ if(!defined($places) || $places !~ /\d/ || $places =~ /\D/ || $places < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $places && $places =~ /\d/u or $places =~ /\D/u or $places < 0) { } 0 9 9
elsif ($places == 0) { } 2 7 9
Condition33%3/9
not defined $places && $places =~ /\d/u or $places =~ /\D/u or $places < 0
l!l&&r!l&&!r
009
not defined $places && $places =~ /\d/u or $places =~ /\D/u
l!l&&r!l&&!r
009
defined $places && $places =~ /\d/u
!ll&&!rl&&r
009
MC/DC0%0/4
not defined $places && $places =~ /\d/u or $places =~ /\D/u or $places < 0
defined $places $places =~ /\d/u $places =~ /\D/u $places < 0
Truth table20%1/5
not(A && B) or C or D
A= defined $placesB= $places =~ /\d/uC= $places =~ /\D/uD= $places < 0
ABCDresult
0XXX1
10XX1
111X1
11011
11000
61 {
62 0 print STDERR ("ERROR:sigdig(): An invalid number of significant ",
63 "digits was specified.");
64 0 return($num);
65 }
66 #0 means all are significant digits
67 elsif($places == 0)
68 {
69 2 $new_num = $num;
70 2 my $cur_sig_dig = getsigdig($new_num);
71 2 ▶ if($pad > $cur_sig_dig)
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $cur_sig_dig) 1 1 2
72 {
73 #If there's an exponent
74 1 ▶ if($new_num =~ /^([+\-]?[0-9\.]+)(e[+\-]?[0-9\.]+)$/i)
Branch: 2 branches
BranchTrueFalseTotal
if ($new_num =~ /^([+\-]?[0-9\.]+)(e[+\-]?[0-9\.]+)$/iu) { } 0 1 1
elsif ($new_num =~ /^([+\-]?)e[+\-]?[0-9\.]+$/iu) { } 0 1 1
75 {
76 0 my $pree = $1;
77 0 my $e = $2;
78 0 return(sigdig($pree,$places,$pad).$e);
79 }
80 #If there's an exponent and no preceding number
81 elsif($new_num =~ /^([+\-]?)e[+\-]?[0-9\.]+$/i)
82 {
83 0 my $sign = $1;
84 0 my $e = $2;
85 0 return(sigdig($sign.'1',$places,$pad).$e);
86 }
87 #Pad the number
88 else
89 {
90 1 ▶ if($new_num !~ /\./)
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_num =~ /\./u) 1 0 1
91 1 {$new_num .= '.'}
92 1 $new_num .= '0' x ($pad - $cur_sig_dig);
93 1 return($new_num);
94 }
95 }
96  
97 1 return($new_num);
98 }
99  
100 #If there's an exponent with a preceding number
101 7 ▶ if($num =~ /^([+\-]?[0-9\.]*)(e[+\-]?[0-9\.]+)$/i)
Branch: 3 branches
BranchTrueFalseTotal
if ($num =~ /^([+\-]?[0-9\.]*)(e[+\-]?[0-9\.]+)$/iu) { } 1 6 7
elsif ($num =~ /[^0-9\.+\-]/u or $num =~ /\..*\./u or $num =~ /.[+\-]/u) { } 0 6 6
elsif ($num == 0) { } 0 6 6
102 {
103 1 my $pree = $1;
104 1 my $e = $2;
105 1 ▶ $pree = $pree . '1' if($pree !~ /\d/);
Branch: 1 branch
BranchTrueFalseTotal
unless $pree =~ /\d/u 0 1 1
106 1 return(sigdig($pree,$places,$pad).$e);
107 }
108 ▶ elsif($num =~ /[^0-9\.+\-]/ || $num =~ /\..*\./ || $num =~ /.[+\-]/)
Condition33%2/6
$num =~ /[^0-9\.+\-]/u or $num =~ /\..*\./u or $num =~ /.[+\-]/u
l!l&&r!l&&!r
006
$num =~ /[^0-9\.+\-]/u or $num =~ /\..*\./u
l!l&&r!l&&!r
006
MC/DC0%0/3
$num =~ /[^0-9\.+\-]/u or $num =~ /\..*\./u or $num =~ /.[+\-]/u
$num =~ /[^0-9\.+\-]/u $num =~ /\..*\./u $num =~ /.[+\-]/u
Truth table25%1/4
A or B or C
A= $num =~ /[^0-9\.+\-]/uB= $num =~ /\..*\./uC= $num =~ /.[+\-]/u
ABCresult
1XX1
01X1
0011
0000
109 {
110 0 print STDERR ("ERROR:sigdig(): Invalid number format: [$num].");
111 0 return($num);
112 }
113 elsif($num == 0)
114 {
115 0 ▶ if($pad)
Branch: 1 branch
BranchTrueFalseTotal
if ($pad) 0 0 0
116 {
117 0 $new_num = '0.';
118 0 ▶ if($new_num !~ /\./)
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_num =~ /\./u) 0 0 0
119 0 {$new_num .= '.'}
120 0 $new_num .= '0' x $pad;
121 0 return($new_num);
122 }
123 0 return(0);
124 }
125  
126 6 my $first_real = 0;
127 6 my $num_added = 0;
128 6 my $decimal_seen = 0;
129 6 my $last_digit = 0;
130 6 my $sign = '';
131 6 foreach my $digit (unpack("(A)*",$num))
132 {
133 34 ▶ if($digit =~ /\+|-/)
Branch: 1 branch
BranchTrueFalseTotal
if ($digit =~ /\+|-/u) 1 33 34
134 {
135 1 $sign = $digit;
136 1 next;
137 }
138 33 ▶ if($digit =~ /[1-9]/)
Branch: 2 branches
BranchTrueFalseTotal
if ($digit =~ /[1-9]/u) { } 23 10 33
elsif ($digit eq '.') { } 6 4 10
139 23 {$first_real = 1}
140 elsif($digit eq '.')
141 {
142 6 $decimal_seen = 1;
143 6 ▶ if($new_num eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($new_num eq "") 0 6 6
144 0 {$new_num = '0'}
145 6 ▶ if($num_added < $places)
Branch: 2 branches
BranchTrueFalseTotal
if ($num_added < $places) { } 5 1 6
elsif ($num_added > $places) { } 0 1 1
146 5 {$new_num .= '.'}
147 elsif($num_added > $places)
148 0 {last}
149 6 next;
150 }
151  
152 27 ▶ if($first_real)
Branch: 2 branches
BranchTrueFalseTotal
if ($first_real) { } 27 0 27
elsif ($decimal_seen) { } 0 0 0
153 {
154 27 ▶ if($num_added < $places)
Branch: 3 branches
BranchTrueFalseTotal
if ($num_added < $places) { } 21 6 27
elsif ($num_added == $places) { } 6 0 6
elsif (not $decimal_seen) { } 0 0 0
155 {
156 21 $new_num .= $digit;
157 21 $num_added++;
158 }
159 elsif($num_added == $places)
160 {
161 6 ▶ if($digit >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if ($digit >= 5) 1 5 6
162 {
163 #This gets rid of the decimal
164 1 my $tmp_num = join("",split(/\D*/,$new_num)) + 1;
165 1 ▶ if($new_num =~ /\.(\d*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($new_num =~ /\.(\d*)$/u) 1 0 1
166 {
167 1 my $len = length($1);
168 1 ▶ unless($tmp_num =~ s/(?=\d{$len}\Z)/./)
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp_num =~ s/(?=\d{$len}\Z)/./u) 0 1 1
169 {
170 0 ▶ if($new_num =~ /^(0\.0+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($new_num =~ /^(0\.0+)/u) 0 0 0
171 0 {$tmp_num = "$1$tmp_num"}
172 }
173 }
174 1 $new_num = $tmp_num;
175 }
176  
177 #If we haven't gotten to the end of the whole number yet
178 6 ▶ if(!$decimal_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (not $decimal_seen) { } 0 6 6
179 {
180 0 $new_num .= '0';
181 0 $num_added++;
182 }
183 else
184 6 {last}
185 }
186 elsif(!$decimal_seen)
187 {
188 0 $new_num .= '0';
189 0 $num_added++;
190 }
191 else
192 0 {last}
193 21 $last_digit = $digit;
194 }
195 elsif($decimal_seen)
196 0 {$new_num .= '0'}
197 }
198  
199 6 ▶ if($pad)
Branch: 2 branches
BranchTrueFalseTotal
if ($pad) { } 1 5 6
elsif ($new_num =~ /\./iu) { } 4 1 5
200 {
201 1 my $cur_sig_dig = getsigdig($new_num);
202 1 ▶ if($pad > $cur_sig_dig && $new_num !~ /\./)
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $cur_sig_dig and not $new_num =~ /\./u) 0 1 1
Condition33%1/3
$pad > $cur_sig_dig and not $new_num =~ /\./u
!ll&&!rl&&r
100
MC/DC0%0/2
$pad > $cur_sig_dig and not $new_num =~ /\./u
$pad > $cur_sig_dig not $new_num =~ /\./u
Truth table33%1/3
A and not(B)
A= $pad > $cur_sig_digB= not $new_num =~ /\./u
ABresult
0X0
100
111
203 0 {$new_num .= '.'}
204 1 $new_num .= '0' x ($pad - $cur_sig_dig);
205 }
206 #Trim the trailing zeros
207 elsif($new_num =~ /\./i)
208 {
209 4 $new_num =~ s/0+$//;
210 4 $new_num =~ s/\.$//;
211 }
212  
213 6 return("$sign$new_num");
214 }
215  
216 sub getsigdig
217 11 ▶ {
Subroutine getsigdig: called CC=7 SCAR=19.5
Pod undocumented
218 11 my $num = $_[0];
219 11 ▶ my $nowholetail = defined($_[1]) ? $_[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 3 8 11
220 11 ▶ my $nodecimaltail = defined($_[2]) ? $_[2] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 1 10 11
221  
222 #Remove sign
223 11 $num =~ s/[+\-]+//g;
224  
225 #Repair exponents with assumed 1s
226 11 ▶ if($num =~ /^e/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /^e/iu) 0 11 11
227 0 {$num = "1$num"}
228 #Remove exponent
229 11 $num =~ s/e.*//i;
230  
231 #Remove leading zeros
232 11 $num =~ s/^0+//;
233  
234 #If $nowholetail is true and there's no decimal point, remove trailing
235 #whole-number zeros
236 11 ▶ $num =~ s/0+$// if($nowholetail && $num !~ /\./);
Branch: 1 branch
BranchTrueFalseTotal
if $nowholetail and not $num =~ /\./u 1 10 11
Condition100%3/3
$nowholetail and not $num =~ /\./u
!ll&&!rl&&r
911
MC/DC100%2/2
$nowholetail and not $num =~ /\./u
$nowholetail not $num =~ /\./u
Truth table100%3/3
A and not(B)
A= $nowholetailB= not $num =~ /\./u
ABresult
0X0
100
111
237  
238 #Remove decimal point
239 11 $num =~ s/\.//;
240  
241 #If $nodecimaltail is true, remove all trailing zeros
242 11 ▶ $num =~ s/0+$// if($nodecimaltail);
Branch: 1 branch
BranchTrueFalseTotal
if $nodecimaltail 1 10 11
243  
244 11 return(length($num));
245 }
246  
247  
248  
249  
250  
251  
252 1 1;
253 __END__
↑ Summary