blib/lib/Math/Ryu.pm

filter: Statementstmt 77.5% 121 / 156 Branchbran 71.4% 60 / 84 Conditioncond 50.0% 12 / 24 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 76.9% 20 / 26 Podpod 0.0% 0 / 13 totaltotal 68.3% 218 / 319 CC 53 SCAR 26.8
CC
53
Cov
73%
fmtpy_pp
34.6
BEGIN
17.1
BEGIN
12.8
CRAP 14.5
↑ Summary Ryu.xs »
1 ## This file generated by InlineX::C2XS (version 0.26) using Inline::C (version 0.79_001)
2 package Math::Ryu;
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=25.5
8 # In this BEGIN{} block we check for the presence of a
9 # perl bug that can set the POK flag when it should not.
10 8 ▶ use B qw(svref_2object);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶ if($] < 5.035010) { # TODO - Check if this value can be reduced (to around 5.02 ?)
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.03501) { } 0 8 8
13 0 my %flags;
14 {
15 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
16 0 for my $flag (qw(
17 SVf_IOK
18 SVf_NOK
19 SVf_POK
20 SVp_IOK
21 SVp_NOK
22 SVp_POK
23 )) {
24 0 ▶ if (defined &{'B::'.$flag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{'B::' . $flag;}) 0 0 0
25 0 $flags{$flag} = &{'B::'.$flag};
26 }
27 }
28 }
29  
30 0 my $test_nv = 1.3;
31 0 my $buggery = "$test_nv";
32 0 my $flags = B::svref_2object(\$test_nv)->FLAGS;
33 0 my $fstr = join ' ', sort grep $flags & $flags{$_}, keys %flags;
34  
35 0 ▶ if($fstr =~ /SVf_POK/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fstr =~ /SVf_POK/) { } 0 0 0
36 0 $Math::Ryu::PV_NV_BUG = 1;
37 }
38 else {
39 0 $Math::Ryu::PV_NV_BUG = 0;
40 }
41 } # close if{} block
42 else {
43 8 $Math::Ryu::PV_NV_BUG = 0;
44 }
45 }; # close BEGIN{} block
46  
47 8 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=28.4
48 8 ▶ if($Config{nvsize} == 8) { $::max_dig = 17 }
Branch: 3 branches
BranchTrueFalseTotal
if ($Math::Ryu::Config{'nvsize'} == 8) { } 8 0 8
elsif ($Math::Ryu::Config{'nvtype'} eq '__float128') { } 0 0 0
elsif (defined $Math::Ryu::Config{'longdblkind'} and $Math::Ryu::Config{'longdblkind'} < 3) { } 0 0 0
49 0 elsif($Config{nvtype} eq '__float128') { $::max_dig = 36 }
50 ▶ elsif(defined($Config{longdblkind})
Condition0%0/3
defined $Math::Ryu::Config{"longdblkind"} and $Math::Ryu::Config{"longdblkind"} < 3
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::Ryu::Config{"longdblkind"} and $Math::Ryu::Config{"longdblkind"} < 3
defined $Math::Ryu::Config{"longdblkind"} $Math::Ryu::Config{"longdblkind"} < 3
Truth table0%0/3
A and B
A= defined $Math::Ryu::Config{"longdblkind"}B= $Math::Ryu::Config{"longdblkind"} < 3
ABresult
0X0
100
111
51 0 && $Config{longdblkind} < 3) { $::max_dig = 36 }
52 0 else { $::max_dig = 21 }
53  
54 }; # close BEGIN{} block
55  
56 8 ▶use constant PV_NV_BUG => $Math::Ryu::PV_NV_BUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 8 ▶use constant IVSIZE => $Config{ivsize};
Subroutine BEGIN: called CC=1 SCAR=0.0
58 8 ▶use constant MAX_DEC_DIG => $::max_dig; # set in second BEGIN{} block
Subroutine BEGIN: called CC=1 SCAR=0.0
59 8 ▶use constant RYU_MAX_INT => $Config{ivsize} == 4 ? 4294967295
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$Math::Ryu::Config{'ivsize'} == 4 ? : 0 8 8
60 8 : 18446744073709551615;
61 8 ▶use constant RYU_MIN_INT => $Config{ivsize} == 4 ? -2147483648
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$Math::Ryu::Config{'ivsize'} == 4 ? : 0 8 8
62 8 : -9223372036854775808;
63  
64 8 require Exporter;
65 8 *import = \&Exporter::import;
66 8 require DynaLoader;
67  
68 8 our $VERSION = '1.09';
69  
70 8 DynaLoader::bootstrap Math::Ryu $VERSION;
71  
72 8 my @tagged = qw(
73 d2s ld2s q2s nv2s fx2s
74 is_NV
75 pn pnv pany sn snv sany
76 spanyf
77 n2s
78 s2d
79 fmtpy fmtpy_pp
80 ryu_lln ryu_refcnt ryu_SvIOK ryu_SvNOK ryu_SvPOK ryu_SvIOKp
81 );
82  
83 8 @Math::Ryu::EXPORT = ();
84 8 @Math::Ryu::EXPORT_OK = @tagged;
85 8 %Math::Ryu::EXPORT_TAGS = (all => \@tagged);
86  
87 8 my $double_inf = 2 ** 1500;
88 8 my $double_nan = $double_inf / $double_inf;
89  
90 # If $Math::Ryu::PERL_INFNAN is set to 0, then the string "inf" represents
91 # positive infinity, the string "-inf" represents negative infinity, and the
92 # string "nan" represents a NaN (not a number).
93 # If $Math::Ryu::PERL_INFNAN is set to a true value then the strings will be
94 # whatever perl uses to signify positive infinity, negative infinity and nan.
95  
96 8 $Math::Ryu::PERL_INFNAN = 0; # Default setting. You can override this
97 # setting in your code.
98  
99 8 my $pinf = 1e5000;
100 8 my $ninf = -$pinf;
101 8 my $nanv = $pinf / $pinf;
102 8 $Math::Ryu::pinfstr = "$pinf";
103 8 $Math::Ryu::ninfstr = "$ninf";
104 8 $Math::Ryu::nanvstr = "$nanv";
105  
106 8 ▶sub dl_load_flags {0} # Prevent DynaLoader from complaining and croaking
Subroutine dl_load_flags: called CC=1 SCAR=0.0
Pod undocumented
107  
108 2790 ▶sub nv2s {
Subroutine nv2s: called CC=1 SCAR=1.2
Pod undocumented
109 2790 my $nv = shift;
110 2790 return fmtpy( d2s($nv)) if MAX_DEC_DIG == 17;
111 0 return fmtpy(ld2s($nv)) if MAX_DEC_DIG == 21;
112 0 return fmtpy( q2s($nv));
113 }
114  
115 8 ▶sub n2s {
Subroutine n2s: called CC=7 SCAR=19.7
Pod undocumented
116 8 my $arg = shift;
117 8 ▶ die "The n2s() function does not accept ", ref($arg), " references"
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg 1 7 8
118 if ref($arg);
119 7 ▶ if(!ryu_SvPOK($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ryu_SvPOK($arg)) 3 4 7
120 3 ▶ return $arg if ryu_SvIOK($arg);
Branch: 1 branch
BranchTrueFalseTotal
if ryu_SvIOK($arg) 0 3 3
121 3 ▶ return _from_NV($arg) if _NV_fits_IV($arg);
Branch: 1 branch
BranchTrueFalseTotal
if _NV_fits_IV($arg) 0 3 3
122 }
123 7 ▶ return nv2s($arg) if ryu_SvNOK($arg);
Branch: 1 branch
BranchTrueFalseTotal
if ryu_SvNOK($arg) 4 3 7
124 # When this sub is called by pany() or sany(), it
125 # will have returned before reaching here.
126 # $arg is neither integer nor float nor reference.
127 # If the numified $arg fits into an IV, return the
128 # stringification of that value.
129 # Else, return nv2s($arg), which will coerce $arg
130 # to an NV.
131 3 my $ret = $arg + 0;
132 3 ▶ return "$ret" if ryu_SvIOK($ret);
Branch: 1 branch
BranchTrueFalseTotal
if ryu_SvIOK($ret) 0 3 3
133 #return nv2s($ret) if ryu_SvNOK($ret);
134 3 return nv2s($arg);
135 }
136  
137 14 ▶sub fmtpy_pp {
Subroutine fmtpy_pp: called CC=24 SCAR=35.5
Pod undocumented
138 # Pure perl rendition of the fmtpy function. Not exported.
139 # The given argument will be either 'Infinity', '-Infinity', 'NaN'
140 # or a finite value of the form "mantissaEexponent".
141 # The mantissa portion will include a decimal point (with that decimal
142 # point being the second character in the mantissa) unless the
143 # mantissa consists of only one decimal significant decimal digit,
144 # in which case there is no decimal point and the mantissa consists
145 # solely of that digit.
146  
147 14 my $s = shift;
148 14 my $sign = '';
149 14 my $bitpos = 0;
150  
151 14 ▶ $sign = '-' if $s =~ s/^\-//;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ s/^\-// 6 8 14
152  
153 14 ▶ $bitpos = 1 if substr($s, 1, 1) eq '.'; # else there isn't a decimal point.
Branch: 1 branch
BranchTrueFalseTotal
if substr($s, 1, 1) eq "." 4 10 14
154  
155 14 ▶ if($bitpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitpos) { } 4 10 14
156 # Mantissa's second character is the decimal point.
157 # Split into mantissa and exponent
158 4 my @parts = split /E/i, $s;
159 4 ▶ if($parts[1] > 0 && $parts[1] < MAX_DEC_DIG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[1] > 0 and $parts[1] < 17) 4 0 4
Condition33%1/3
$parts[1] > 0 and $parts[1] < 17
!ll&&!rl&&r
004
MC/DC0%0/2
$parts[1] > 0 and $parts[1] < 17
$parts[1] > 0 $parts[1] < 17
Truth table33%1/3
A and B
A= $parts[1] > 0B= $parts[1] < 17
ABresult
0X0
100
111
160 # We want, eg, a value like 1.1E-3 to be returned as "0.0011".
161 4 my $zero_pad = $parts[1] - (length($parts[0]) - 2);
162 4 ▶ if($zero_pad >= 0 && ($zero_pad + length($parts[0])) < MAX_DEC_DIG + 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($zero_pad >= 0 and $zero_pad + length($parts[0]) < 18) { } 2 2 4
elsif ($zero_pad < 0) { } 0 2 2
Condition66%2/3
$zero_pad >= 0 and $zero_pad + length($parts[0]) < 18
!ll&&!rl&&r
022
MC/DC50%1/2
$zero_pad >= 0 and $zero_pad + length($parts[0]) < 18
$zero_pad >= 0 $zero_pad + length($parts[0]) < 18
Truth table66%2/3
A and B
A= $zero_pad >= 0B= $zero_pad + length($parts[0]) < 18
ABresult
0X0
100
111
163 2 substr($parts[0], 1, 1, '');
164 2 return $sign . $parts[0] . ('0' x $zero_pad) . '.0';
165 }
166 elsif($zero_pad < 0) {
167 # We want, eg, a value like 1.23625E2 to be returned as "123.625".
168 # relocate the decimal point
169 0 substr($parts[0], 1, 1, '');
170 0 substr($parts[0], $zero_pad, 0, '.');
171 0 return $sign . $parts[0];
172 }
173 }
174  
175 # Return as is, except that we replace the 'E' with 'e', ensuring also
176 # that the exponent is preceded by a '+' or '-' sign, and that
177 # negative exponents consist of at least 2 digits.
178 2 ▶ $s =~ s/e/e\+/i if $parts[1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[1] > 0 2 0 2
179 2 ▶ if ($parts[1] < -4 || $parts[1] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[1] < -4 or $parts[1] >= 0) 2 0 2
Condition33%1/3
$parts[1] < -4 or $parts[1] >= 0
l!l&&r!l&&!r
020
MC/DC0%0/2
$parts[1] < -4 or $parts[1] >= 0
$parts[1] < -4 $parts[1] >= 0
Truth table33%1/3
A or B
A= $parts[1] < -4B= $parts[1] >= 0
ABresult
1X1
011
000
180 2 $s =~ s/E0$//i;
181 2 ▶ substr($s, -1, 0, '0') if substr($s, -2, 1) eq '-'; # pad exponent with a leading '0'.
Branch: 1 branch
BranchTrueFalseTotal
if substr($s, -2, 1) eq "-" 0 2 2
182 2 return $sign . lc($s);
183 }
184 # Return, eg 6.25E1 as "0.625"
185 0 substr($parts[0], 1, 1, ''); # remove decimal point.
186 0 return $sign . '0.' . ('0' x (abs($parts[1]) - 1)) . $parts[0] ;
187 }
188 else {
189 # Return '-inf', 'inf', or 'nan' if (and as) appropriate.
190 10 ▶ if($s =~ /n/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /n/i) 6 4 10
191 6 ▶ if($Math::Ryu::PERL_INFNAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::Ryu::PERL_INFNAN) 3 3 6
192 3 ▶ return $Math::Ryu::nanvstr if $s =~ /a/i;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /a/i 1 2 3
193 2 ▶ return $Math::Ryu::ninfstr if $sign;
Branch: 1 branch
BranchTrueFalseTotal
if $sign 1 1 2
194 1 return $Math::Ryu::pinfstr;
195 }
196 3 return $sign . lc(substr($s, 0, 3));
197 }
198  
199 # Append '.0' to the mantissa and return it if the exponent is 0.
200 4 ▶ return $sign . $s . '.0' if $s =~ s/E0$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ s/E0$//i 0 4 4
201 4 my @parts = split /E/i, $s;
202  
203 # Return as is, except that we replace the 'E' with 'e', ensuring also
204 # that the exponent is preceded by a '+' or '-' sign, and that
205 # negative exponents consist of at least 2 digits.
206 4 ▶ $s =~ s/e/e\+/i if $parts[1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[1] > 0 4 0 4
207 4 ▶ $s =~ s/e\-/e\-0/i if ($parts[1] < -4 && $parts[1] > -10);
Branch: 1 branch
BranchTrueFalseTotal
if $parts[1] < -4 and $parts[1] > -10 0 4 4
Condition33%1/3
$parts[1] < -4 and $parts[1] > -10
!ll&&!rl&&r
400
MC/DC0%0/2
$parts[1] < -4 and $parts[1] > -10
$parts[1] < -4 $parts[1] > -10
Truth table33%1/3
A and B
A= $parts[1] < -4B= $parts[1] > -10
ABresult
0X0
100
111
208 4 ▶ return $sign . lc($s) if ($parts[1] < -4 || $parts[1] > MAX_DEC_DIG - 2);
Branch: 1 branch
BranchTrueFalseTotal
if $parts[1] < -4 or $parts[1] > 15 2 2 4
Condition66%2/3
$parts[1] < -4 or $parts[1] > 15
l!l&&r!l&&!r
022
MC/DC50%1/2
$parts[1] < -4 or $parts[1] > 15
$parts[1] < -4 $parts[1] > 15
Truth table66%2/3
A or B
A= $parts[1] < -4B= $parts[1] > 15
ABresult
1X1
011
000
209  
210 2 ▶ if($parts[1] >= 0 ) { # $parts[1] is in the range 1..(MAX_DEC_DIG - 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[1] >= 0) 2 0 2
211 2 return $sign . $parts[0] . (0 x $parts[1]) . '.0';
212 }
213  
214 # Return, eg, 6E-3 as "0.006".
215 0 return $sign . '0.' . ('0' x (abs($parts[1]) - 1)) . $parts[0] ;
216 }
217 }
218  
219 9647 ▶sub s2d {
Subroutine s2d: called CC=5 SCAR=16.1
Pod undocumented
220 9647 die "s2d() is available only to perls whose NV is of type 'double'"
221 unless MAX_DEC_DIG == 17;
222 9647 my $str = shift;
223  
224 9647 ▶ die("Strings passed to s2d() must \"look like a number\"")
Branch: 1 branch
BranchTrueFalseTotal
unless ryu_lln($str) 2 9645 9647
225 unless ryu_lln($str);
226 # For _s2d, we need to remove all leading and trailing whitespace.
227 # If $str contained internal whitespace, then s2d has already died.
228 9645 $str =~ s/\s//g;
229  
230 # _s2d doesn't handle inf/nan, so we attend to that first.
231 # However, if perl recognizes these strings as numeric, then it's
232 # not really necessary to assign them using s2d.
233 # And if perl does NOT recognize these strings as numeric, then s2d
234 # is simply going to croak.
235 # Anyway, here they are:
236 9645 ▶ return $double_inf if $str =~ /^\+?inf/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\+?inf/i 146 9499 9645
237 9499 ▶ return -$double_inf if $str =~ /^\-inf/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\-inf/i 37 9462 9499
238 9462 ▶ return $double_nan if $str =~ /^(\-|\+)?nan/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^(\-|\+)?nan/i 2 9460 9462
239  
240 9460 return _s2d($str);
241 }
242  
243 0 ▶sub pn {
Subroutine pn: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my $arg = shift;
245 0 print n2s($arg);
246 }
247  
248 0 ▶sub sn {
Subroutine sn: not called CC=1 SCAR=6.9
Pod undocumented
249 0 my $arg = shift;
250 0 print n2s($arg), "\n";
251 }
252  
253 0 ▶sub pnv {
Subroutine pnv: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my $nv = shift;
255 0 print nv2s($nv);
256 }
257  
258 0 ▶sub snv {
Subroutine snv: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my $nv = shift;
260 0 print nv2s($nv), "\n";
261 }
262  
263 0 ▶sub pany { # "p"rint "any"
Subroutine pany: not called CC=1 SCAR=6.9
Pod undocumented
264 0 print spanyf(@_)
265 }
266  
267 0 ▶sub sany {
Subroutine sany: not called CC=1 SCAR=6.9
Pod undocumented
268 0 print spanyf(@_) . "\n";
269 }
270  
271 13 ▶sub spanyf {
Subroutine spanyf: called CC=4 SCAR=13.9
Pod undocumented
272 # Prepares the string that pany/sany will print.
273 13 my $ret = '';
274  
275 13 for my $arg (@_) {
276 39 ▶ if(is_NV($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_NV($arg)) { } 8 31 39
277 8 ▶ if(_NV_fits_IV($arg)) { $ret .= _from_NV($arg) } # nv2s($arg) would append a ".0"
Branch: 1 branch
BranchTrueFalseTotal
if (_NV_fits_IV($arg)) { } 3 5 8
278 5 else { $ret .= nv2s($arg) }
279 }
280 else {
281 31 $ret .= "$arg";
282 }
283 }
284 13 return $ret;
285 }
286  
287 11 ▶sub _NV_fits_IV {
Subroutine _NV_fits_IV: called CC=4 SCAR=13.9
288 # Called only when the argument is an NV
289 11 my $nv = shift;
290 11 ▶ return 0 if $nv != int($nv);
Branch: 1 branch
BranchTrueFalseTotal
if $nv != int $nv 3 8 11
291 8 ▶ return 1 if ( $nv <= RYU_MAX_INT && $nv >= RYU_MIN_INT );
Branch: 1 branch
BranchTrueFalseTotal
if $nv <= '18446744073709551615' and $nv >= '-9223372036854775808' 3 5 8
Condition66%2/3
$nv <= '18446744073709551615' and $nv >= '-9223372036854775808'
!ll&&!rl&&r
503
MC/DC50%1/2
$nv <= '18446744073709551615' and $nv >= '-9223372036854775808'
$nv <= '18446744073709551615' $nv >= '-9223372036854775808'
Truth table66%2/3
A and B
A= $nv <= '18446744073709551615'B= $nv >= '-9223372036854775808'
ABresult
0X0
100
111
292 5 return 0;
293 }
294  
295 55 ▶sub is_NV {
Subroutine is_NV: called CC=4 SCAR=13.9
Pod undocumented
296 55 my $arg = shift;
297  
298 55 return 1
299 if(PV_NV_BUG && ryu_SvPOK($arg) && ryu_SvNOK($arg) && !ryu_SvIOKp($arg));
300  
301 55 ▶ if(ryu_lln($arg)) { # Wraps the perl API function looks_like_number(),
Branch: 1 branch
BranchTrueFalseTotal
if (ryu_lln($arg)) 26 29 55
302 # which differs from Scalar::Util::looks_like_number().
303 26 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if ryu_SvPOK($arg) or ryu_SvIOK($arg) 15 11 26
Condition100%3/3
ryu_SvPOK($arg) or ryu_SvIOK($arg)
l!l&&r!l&&!r
13211
MC/DC100%2/2
ryu_SvPOK($arg) or ryu_SvIOK($arg)
ryu_SvPOK($arg) ryu_SvIOK($arg)
Truth table100%3/3
A or B
A= ryu_SvPOK($arg)B= ryu_SvIOK($arg)
ABresult
1X1
011
000
304 if(ryu_SvPOK($arg) || ryu_SvIOK($arg));
305  
306 # At this point we know that $arg looks like a number
307 # and neither its POK flag nor its IOK flag is set.
308 # It must be an NV.
309 11 return 1;
310 }
311 29 return 0;
312 }
313  
314 8 1;
↑ Summary Ryu.xs »