lib/Math/SigFigs.pm

filter: Statementstmt 100% 160 / 160 Branchbran 100% 88 / 88 Conditioncond 100% 29 / 29 MC/DCmcdc 100% 17 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 100% 315 / 315 CC 55 SCAR 24.6
CC
55
Cov
100%
_Simplify
20.0
FormatSigFigs
13.0
_add
11.0
CRAP 11.7
↑ Summary
1 package Math::SigFigs;
2  
3 # Copyright (c) 1995-2019 Sullivan Beck. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 ########################################################################
8  
9 8 require 5.004;
10 8 require Exporter;
11 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 our (@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
16 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 @EXPORT = qw(FormatSigFigs
18 CountSigFigs
19 );
20 8 @EXPORT_OK = qw(FormatSigFigs
21 CountSigFigs
22 addSF subSF multSF divSF
23 VERSION);
24  
25 8 %EXPORT_TAGS = ('all' => \@EXPORT_OK);
26  
27 8 our($VERSION);
28 8 $VERSION='1.22';
29  
30 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 52 ▶sub addSF {
Subroutine addSF: called CC=1 SCAR=0.0
Pod documented
33 52 my($n1,$n2)=@_;
34 52 _add($n1,$n2,0);
35 }
36  
37 26 ▶sub subSF {
Subroutine subSF: called CC=1 SCAR=0.0
Pod documented
38 26 my($n1,$n2)=@_;
39 26 _add($n1,$n2,1);
40 }
41  
42 78 ▶sub _add {
Subroutine _add: called CC=11 SCAR=24.0
43 78 my($n1in,$n2in,$sub) = @_;
44  
45 78 my($n1,$sig1,$lsp1,$s1,$int1,$dec1,$n2,$sig2,$lsp2,$s2,$int2,$dec2);
46  
47 78 ▶ if (defined($n1in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n1in) 74 4 78
48 74 ($n1,$sig1,$lsp1,$s1,$int1,$dec1) = _Simplify($n1in);
49 }
50 78 ▶ return if (! defined($n1));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n1 6 72 78
51  
52 72 ▶ if (defined($n2in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n2in) 70 2 72
53 70 ($n2,$sig2,$lsp2,$s2,$int2,$dec2) = _Simplify($n2in);
54 }
55 72 ▶ return if (! defined($n2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n2 4 68 72
56  
57 68 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 21 47 68
58 21 ▶ if ($n2<0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n2 < 0) { } 8 13 21
elsif ($n2 > 0) { } 12 1 13
59 8 $n2 =~ s/\-//;
60 8 $s2 = '';
61 } elsif ($n2 > 0) {
62 12 $n2 =~ s/^\+?/-/;
63 12 $s2 = '-';
64 }
65 }
66  
67 68 ▶ return $n2 if ($n1in eq '0');
Branch: 1 branch
BranchTrueFalseTotal
if $n1in eq 0 1 67 68
68 67 ▶ return $n1 if ($n2in eq '0');
Branch: 1 branch
BranchTrueFalseTotal
if $n2in eq 0 1 66 67
69  
70 66 ▶ my $lsp = ($lsp1 > $lsp2 ? $lsp1 : $lsp2);
Branch: 1 branch
BranchTrueFalseTotal
$lsp1 > $lsp2 ? : 8 58 66
71  
72 66 ($n1) = _ToExp($s1,$int1,$dec1,$lsp);
73 66 ($n2) = _ToExp($s2,$int2,$dec2,$lsp);
74  
75 66 my($n,$sig,$tmp,$s,$int,$dec) = _Simplify($n1+$n2);
76 66 $n = sprintf("%.0f",$n) . ".e$lsp";
77 66 ($n,$sig,$lsp,$tmp,$int,$dec) = _Simplify("${n}");
78 66 return $n;
79 }
80  
81 12 ▶sub multSF {
Subroutine multSF: called CC=1 SCAR=0.0
Pod documented
82 12 my($n1,$n2)=@_;
83 12 _mult($n1,$n2,0);
84 }
85  
86 10 ▶sub divSF {
Subroutine divSF: called CC=1 SCAR=0.0
Pod documented
87 10 my($n1,$n2)=@_;
88 10 _mult($n1,$n2,1);
89 }
90  
91 22 ▶sub _mult {
Subroutine _mult: called CC=9 SCAR=22.0
92 22 my($n1,$n2,$div)=@_;
93 22 my($sig1,$sig2);
94  
95 22 ▶ if (defined($n1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n1) 18 4 22
96 18 ($n1,$sig1) = _Simplify($n1);
97 }
98 22 ▶ return if (! defined($n1));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n1 4 18 22
99  
100 18 ▶ if (defined($n2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n2) 16 2 18
101 16 ($n2,$sig2) = _Simplify($n2);
102 }
103 18 ▶ return if (! defined($n2) ||
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n2 or $div and $n2 == 0 3 15 18
Condition100%6/6
not defined $n2 or $div and $n2 == 0
l!l&&r!l&&!r
2115
$div and $n2 == 0
!ll&&!rl&&r
961
MC/DC100%3/3
not defined $n2 or $div and $n2 == 0
not defined $n2 $div $n2 == 0
Truth table100%4/4
not(A) or B and C
A= not defined $n2B= $divC= $n2 == 0
ABCresult
1XX1
0111
00X0
0100
104 ($div && $n2 == 0));
105  
106 15 ▶ my $sig = ($sig1 < $sig2 ? $sig1 : $sig2);
Branch: 1 branch
BranchTrueFalseTotal
$sig1 < $sig2 ? : 4 11 15
107 15 ▶ my($n) = ($div ? $n1/$n2 : $n1*$n2);
Branch: 1 branch
BranchTrueFalseTotal
$div ? : 6 9 15
108 15 return FormatSigFigs($n,$sig);
109 }
110  
111 174 ▶sub FormatSigFigs {
Subroutine FormatSigFigs: called CC=13 SCAR=25.6
Pod documented
112 174 my($N,$n) = @_;
113 174 ▶ return '' if ($n !~ /^\d+$/ || $n == 0);
Branch: 1 branch
BranchTrueFalseTotal
if not $n =~ /^\d+$/ or $n == 0 3 171 174
Condition100%3/3
not $n =~ /^\d+$/ or $n == 0
l!l&&r!l&&!r
21171
MC/DC100%2/2
not $n =~ /^\d+$/ or $n == 0
not $n =~ /^\d+$/ $n == 0
Truth table100%3/3
not(A) or B
A= not $n =~ /^\d+$/B= $n == 0
ABresult
1X1
011
000
114  
115 171 my($ret,$sig,$lsp,$s,$int,$dec);
116 171 ($N,$sig,$lsp,$s,$int,$dec) = _Simplify($N);
117 171 ▶ return "" if (! defined($N));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $N 1 170 171
118 170 ▶ return '0.0' if ($N==0 && $n==1);
Branch: 1 branch
BranchTrueFalseTotal
if $N == 0 and $n == 1 2 168 170
Condition100%3/3
$N == 0 and $n == 1
!ll&&!rl&&r
16172
MC/DC100%2/2
$N == 0 and $n == 1
$N == 0 $n == 1
Truth table100%3/3
A and B
A= $N == 0B= $n == 1
ABresult
0X0
100
111
119  
120 168 ▶ return $N if ($sig eq $n);
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq $n 25 143 168
121  
122 # Convert $N to an exponential where the numeric part with the exponent
123 # ignored is 0.1 <= $num < 1.0. i.e. 0.#####e## where the first '#' is
124 # non-zero. Then we can format it using a simple sprintf command.
125  
126 143 my($num,$e);
127 143 ▶ if ($int > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($int > 0) { } 98 45 143
elsif ($dec ne '' and $dec > 0) { } 38 7 45
128 98 $num = "0.$int$dec";
129 98 $e = length($int);
130 ▶ } elsif ($dec ne '' && $dec > 0) {
Condition100%3/3
$dec ne "" and $dec > 0
!ll&&!rl&&r
4338
MC/DC100%2/2
$dec ne "" and $dec > 0
$dec ne "" $dec > 0
Truth table100%3/3
A and B
A= $dec ne ""B= $dec > 0
ABresult
0X0
100
111
131 38 $dec =~ s/^(0*)//;
132 38 $num = "0.$dec";
133 38 $e = -length($1);
134 } else {
135 7 $e = 0;
136 7 $num = "$int.$dec";
137 }
138  
139 # sprintf doesn't round 5 up, so convert a 5 to 6 in the n+1'th position
140  
141 143 ▶ if ($n < $sig && substr($num,$n+2,1) eq '5') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $sig and substr($num, $n + 2, 1) eq 5) 16 127 143
Condition100%3/3
$n < $sig and substr($num, $n + 2, 1) eq 5
!ll&&!rl&&r
507716
MC/DC100%2/2
$n < $sig and substr($num, $n + 2, 1) eq 5
$n < $sig substr($num, $n + 2, 1) eq 5
Truth table100%3/3
A and B
A= $n < $sigB= substr($num, $n + 2, 1) eq 5
ABresult
0X0
100
111
142 16 substr($num,$n+2,1) = '6';
143 }
144  
145 # We have to handle the one special case:
146 # 0.99 (1) => 1.0
147 # If sprintf rounds a number to 1.0 or higher, then we reduce the
148 # number of decimal points by 1.
149  
150 143 my $tmp = sprintf("%.${n}f",$num);
151 143 ▶ if ($tmp >= 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp >= 1) 38 105 143
152 38 $n--;
153 38 $tmp = sprintf("%.${n}f",$num);
154 }
155 143 ($N,$sig,$lsp,$s,$int,$dec) = _Simplify("$s${tmp}e$e");
156 143 return $N;
157 }
158  
159 14 ▶sub CountSigFigs {
Subroutine CountSigFigs: called CC=2 SCAR=6.9
Pod documented
160 14 my($N) = @_;
161 14 my($sig);
162 14 ($N,$sig) = _Simplify($N);
163 14 ▶ return () if (! defined($N));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $N 1 13 14
164 13 return $sig;
165 }
166  
167 ########################################################################
168 # NOT FOR EXPORT
169 #
170 # These are for internal use only. They are not guaranteed to remain
171 # backward compatible (or even to exist at all) in future versions.
172 ########################################################################
173  
174 # This takes the parts of a number ($int and $dec) and turns it into
175 # an exponential with the LSP in the 1's place. The exponent is
176 # returned (rather than appended to the number).
177 #
178 139 ▶sub _ToExp {
Subroutine _ToExp: called CC=5 SCAR=16.1
179 139 my($s,$int,$dec,$lsp) = @_;
180  
181 139 ▶ if ($lsp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lsp == 0) 15 124 139
182 15 return ("$s$int.${dec}",0);
183 }
184  
185 124 ▶ if ($lsp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lsp > 0) 33 91 124
186 33 ▶ my $z = ($lsp > length($int) ?
Branch: 1 branch
BranchTrueFalseTotal
$lsp > length $int ? : 1 32 33
187 "0"x($lsp-length($int)) : "");
188 33 $int = "$z$int";
189 33 $dec = substr($int,-$lsp) . $dec;
190 33 $int = substr($int,0,length($int)-$lsp);
191 33 return ("$s$int.${dec}",-$lsp);
192 }
193  
194 91 ▶ $dec .= "0"x(-$lsp-length($dec)) if (-$lsp > length($dec));
Branch: 1 branch
BranchTrueFalseTotal
if -$lsp > length $dec 1 90 91
195 91 $int .= substr($dec,0,-$lsp);
196 91 $dec = substr($dec,-$lsp);
197 91 return ("$s$int.${dec}",-$lsp);
198 }
199  
200 # This prepares a number by converting it to it's simplest correct
201 # form. All space is ignored. It handles numbers of the form:
202 # signed (+, -, or no sign)
203 # integers
204 # reals (###.###)
205 # exponential (###.###e###)
206 #
207 # It returns:
208 # the number in the simplest form
209 # the number of significant figures
210 # the power of the least significant digit
211 #
212 724 ▶sub _Simplify {
Subroutine _Simplify: called CC=20 SCAR=30.0
213 724 my($n) = @_;
214 724 ▶ return if (! defined($n));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 723 724
215 723 $n =~ s/\s+//g;
216 723 $n =~ s/^([+-])//;
217 723 ▶ my $s = $1 || '';
Condition100%2/2
$1 || ''
l!l
82641
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
218 723 ▶ return if ($n eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 2 721 723
219 721 my $exp;
220 721 ▶ if ($n =~ s/[eE]([+-]*\d+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/[eE]([+-]*\d+)$//) { } 244 477 721
221 244 $exp = $1;
222 } else {
223 477 $exp = 0;
224 }
225  
226 721 my($int,$dec,$sig,$lsp);
227  
228 721 ▶ if ($n =~ /^\d+$/) { # 00 0123 012300
Branch: 3 branches
BranchTrueFalseTotal
if ($n =~ /^\d+$/) { } 132 589 721
elsif ($n =~ /^0*\.(\d+)$/) { } 233 356 589
elsif ($n =~ /^0*(\d+)\.(\d*)$/) { } 348 8 356
229 132 $int = $n+0; # 0 123 12300
230 132 $int =~ /^(\d+?)(0*)$/;
231 132 my($i,$z) = ($1,$2); # 0,'' 123,'' 123,00
232 132 $lsp = length($z); # 0 0 2
233 132 $sig = length($int) - $lsp; # 1 3 3
234 132 $dec = '';
235  
236 } elsif ($n =~ /^0*\.(\d+)$/) { # .000 .00123 .0012300
237 233 $dec = $1; # 000 00123 0012300
238 233 $int = '';
239 233 $dec =~ /^(0*?)([1-9]\d*?)?(0*+)$/;
240 233 my($z0,$d,$z1) = ($1,$2,$3); # '','',000 00,123,'' 00,123,00
241 233 $lsp = -length($dec); # -3 -5 -7
242 233 $sig = length($dec)-length($z0); # 3 3 5
243  
244 } elsif ($n =~ /^0*(\d+)\.(\d*)$/) { # 12. 12.3
245 348 ($int,$dec) = ($1,$2); # 12,'' 12,3
246 348 $lsp = -length($dec); # 0 -1
247 348 $sig = length($int) + length($dec);# 2 3
248  
249 } else {
250 8 return;
251 }
252  
253 # Handle the exponent, if any
254  
255 713 ▶ if ($exp > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp > 0) { } 139 574 713
elsif ($exp < 0) { } 66 508 574
256 139 ▶ if ($exp >= length($dec)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp >= length $dec) { } 95 44 139
257 95 $int = "$int$dec" . "0"x($exp-length($dec));
258 95 $dec = '';
259 } else {
260 44 $int .= substr($dec,0,$exp);
261 44 $dec = substr($dec,$exp);
262 }
263 139 $lsp += $exp;
264 139 $int =~ s/^0*//;
265 139 ▶ $int = '0' if (! $int);
Branch: 1 branch
BranchTrueFalseTotal
unless $int 9 130 139
266  
267 } elsif ($exp < 0) {
268 66 ▶ if (-$exp < length($int)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-$exp < length $int) { } 27 39 66
269 27 $dec = substr($int,$exp) . $dec;
270 27 $int = substr($int,0,length($int)+$exp);
271 } else {
272 39 $dec = "0"x(-$exp-length($int)) . "$int$dec";
273 39 $int = "0";
274 }
275 66 $lsp += $exp;
276 }
277  
278 # We have a decimal point if:
279 # There is a decimal section
280 # An integer ends with a significant 0 but is not exactly 0
281 # We prepend a sign to anything except for 0
282  
283 713 my $num;
284 713 ▶ if ($dec eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dec eq '') { } 248 465 713
285 248 $num = $int;
286 248 ▶ $num .= "." if ($lsp == 0 && $int =~ /0$/ && $int ne '0');
Branch: 1 branch
BranchTrueFalseTotal
if $lsp == 0 and $int =~ /0$/ and $int ne 0 34 214 248
Condition100%6/6
$lsp == 0 and $int =~ /0$/ and $int ne 0
!ll&&!rl&&r
2001434
$lsp == 0 and $int =~ /0$/
!ll&&!rl&&r
1336748
MC/DC100%3/3
$lsp == 0 and $int =~ /0$/ and $int ne 0
$lsp == 0 $int =~ /0$/ $int ne 0
Truth table100%4/4
A and B and C
A= $lsp == 0B= $int =~ /0$/C= $int ne 0
ABCresult
0XX0
10X0
1100
1111
287 } else {
288 465 ▶ $int = "0" if ($int eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $int eq "" 145 320 465
289 465 $num = "$int.$dec";
290 }
291 713 ▶ $s = '' if ($num == 0 || $s eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 or $s eq "+" 73 640 713
Condition100%3/3
$num == 0 or $s eq "+"
l!l&&r!l&&!r
5023640
MC/DC100%2/2
$num == 0 or $s eq "+"
$num == 0 $s eq "+"
Truth table100%3/3
A or B
A= $num == 0B= $s eq "+"
ABresult
1X1
011
000
292 713 $num = "$s$num";
293  
294 713 return ($num,$sig,$lsp,$s,$int,$dec);
295 }
296  
297 8 1;
298 # Local Variables:
299 # mode: cperl
300 # indent-tabs-mode: nil
301 # cperl-indent-level: 3
302 # cperl-continued-statement-offset: 2
303 # cperl-continued-brace-offset: 0
304 # cperl-brace-offset: 0
305 # cperl-brace-imaginary-offset: 0
306 # cperl-label-offset: 0
307 # End:
↑ Summary