blib/lib/Scalar/Number.pm

filter: Statementstmt 98.1% 103 / 105 Branchbran 91.0% 51 / 56 Conditioncond 91.6% 33 / 36 MC/DCmcdc 85.7% 18 / 21 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 94.6% 229 / 242 CC 41 SCAR 18.3
CC
41
Cov
95%
sclnum_is_float
12.0
scalar_num_part
10.0
sclnum_val_cmp
8.0
CRAP 6.2
↑ Summary lib/Scalar/Number.xs »
1 =head1 NAME
2  
3 Scalar::Number - numeric aspects of scalars
4  
5 =head1 SYNOPSIS
6  
7 use Scalar::Number qw(scalar_num_part);
8  
9 $num = scalar_num_part($scalar);
10  
11 use Scalar::Number qw(sclnum_is_natint sclnum_is_float);
12  
13 if(sclnum_is_natint($value)) { ...
14 if(sclnum_is_float($value)) { ...
15  
16 use Scalar::Number qw(sclnum_val_cmp sclnum_id_cmp);
17  
18 @sorted_nums = sort { sclnum_val_cmp($a, $b) } @floats;
19 @sorted_nums = sort { sclnum_id_cmp($a, $b) } @floats;
20  
21 =head1 DESCRIPTION
22  
23 This module is about the numeric part of plain (string) Perl scalars.
24 A scalar has a numeric value, which may be expressed in either the
25 native integer type or the native floating point type. Many values
26 are expressible both ways, in which case the exact representation is
27 insignificant. To fully understand Perl arithmetic it is necessary to
28 know about both of these representations, and the differing behaviours
29 of numbers according to which way they are expressible.
30  
31 This module provides functions to extract the numeric part of a scalar,
32 classify a number by expressibility, and compare numbers across
33 representations.
34  
35 This module is implemented in XS, with a pure Perl backup version for
36 systems that can't handle XS.
37  
38 =cut
39  
40 package Scalar::Number;
41  
42 8 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 our $VERSION = "0.007";
47  
48 8 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 our @EXPORT_OK = qw(
50 scalar_num_part
51 sclnum_is_natint sclnum_is_float
52 sclnum_val_cmp sclnum_id_cmp
53 );
54  
55 4 eval { local $SIG{__DIE__};
56 69 require XSLoader;
57 17 XSLoader::load(__PACKAGE__, $VERSION);
58 };
59  
60 17 ▶if($@ eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq '') { } 4 0 4
61 16 close(DATA);
62 65 ▶ *scalar_num_part = sub($) {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 8 ▶ no warnings qw(numeric uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 65 return _warnable_scalar_num_part($_[0]);
65 16 };
66 } else {
67 12 local $/ = undef;
68 12 my $pp_code = <DATA>;
69 2 close(DATA);
70 {
71 10 local $SIG{__DIE__};
72 0 ▶ eval $pp_code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=2 SCAR=6.9
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=2 SCAR=6.9
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 26 branches
BranchTrueFalseTotal
if ((&max_natint() - 2) % 2 != 1) 1 16 17
if ($@ eq '') { } 6 10 16
overload::StrVal($_[0]) =~ /0x([0-9a-f]+)\)\z/ or die "don't understand StrVal output" 49 13 62
if (ref $val ne "") 2 47 49
unless defined $meth 0 47 47
if ($@ ne "" or ref $newval ne "" and builtin::refaddr($newval) == builtin::refaddr($val)) 160 5705 5865
if ((my $tval = $val) == 0) { } 156 5549 5705
if (not defined $val or ref \$val eq 'GLOB') { } 1213 4336 5549
elsif (do { my $warned; local $SIG{'__WARN__'} = sub { $warned = 1; } ; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} 0 + (my $tval = $val); $warned }) { } 4 156 160
if ($a != $a) { } 42 706 748
elsif ($b != $b) { } 166 540 706
elsif ($a == 0 and $b == 0) { } 290 250 540
$b != $b ? : 38 212 250
if ($val == 0) { } 11 201 212
elsif (int $val != $val or &float_is_infinite($val)) { } 3 8 11
if $val >= '-9007199254740992' and $val <= '9007199254740992' 985 122 1107
if $val < &min_natint() or $val > "1.84467440737096e+19" 87 898 985
if ($val > 18446744073709549568) { } 42 706 748
if $val & 1 162 544 706
if ($val >= '9007199254740992') 4188 148 4336
if $val & 1 69 79 148
if ($val == 0) { } 1628 148 1776
elsif (int $val != $val) { } 1420 208 1628
unless defined $cmp and $cmp == 0 0 208 208
if $a < 0 0 4 4
if ($t != '4503599627370496') 0 4 4
Condition91%33/36
$@ ne "" or ref $newval ne "" and builtin::refaddr($newval) == builtin::refaddr($val)
l!l&&r!l&&!r
0210
ref $newval ne "" and builtin::refaddr($newval) == builtin::refaddr($val)
!ll&&!rl&&r
822
not defined $val or ref \$val eq "GLOB"
l!l&&r!l&&!r
1147
$a == 0 and $b == 0
!ll&&!rl&&r
282415121213
int $val != $val or &float_is_infinite($val)
l!l&&r!l&&!r
1624540
$val >= '-9007199254740992' and $val <= '9007199254740992'
!ll&&!rl&&r
107143290
$val < &min_natint() or $val > "1.84467440737096e+19"
l!l&&r!l&&!r
2414212
$val >= &min_natint() && ($val < 18446744073709549568 || $val <= 18446744073709551616.0 && $val - 18446744073709549568 != 2048)
!ll&&!rl&&r
2618500
$val < '18446744073709549568' or $val <= "1.84467440737096e+19" and $val - '18446744073709549568' != 2048
l!l&&r!l&&!r
4861418
$val <= "1.84467440737096e+19" and $val - '18446744073709549568' != 2048
!ll&&!rl&&r
16214
defined $cmp and $cmp == 0
!ll&&!rl&&r
04188148
$a >= $t and $b >= $t
!ll&&!rl&&r
14200208
MC/DC66%2/3
$@ ne "" or ref $newval ne "" and builtin::refaddr($newval) == builtin::refaddr($val)
$@ ne "" ref $newval ne "" builtin::refaddr($newval) == builtin::refaddr($val)
MC/DC100%2/2
not defined $val or ref \$val eq "GLOB"
not defined $val ref \$val eq "GLOB"
MC/DC100%2/2
$a == 0 and $b == 0
$a == 0 $b == 0
MC/DC100%2/2
int $val != $val or &float_is_infinite($val)
int $val != $val &float_is_infinite($val)
MC/DC100%2/2
$val >= '-9007199254740992' and $val <= '9007199254740992'
$val >= '-9007199254740992' $val <= '9007199254740992'
MC/DC100%2/2
$val < &min_natint() or $val > "1.84467440737096e+19"
$val < &min_natint() $val > "1.84467440737096e+19"
MC/DC100%4/4
$val >= &min_natint() && ($val < 18446744073709549568 || $val <= 18446744073709551616.0 && $val - 18446744073709549568 != 2048)
$val >= &min_natint() $val < '18446744073709549568' $val <= "1.84467440737096e+19" $val - '18446744073709549568' != 2048
MC/DC50%1/2
defined $cmp and $cmp == 0
defined $cmp $cmp == 0
MC/DC50%1/2
$a >= $t and $b >= $t
$a >= $t $b >= $t
Truth table75%3/4
A or B and C
A= $@ ne ""B= ref $newval ne ""C= builtin::refaddr($newval) == builtin::refaddr($val)
ABCresult
1XX1
0111
00X0
0100
Truth table100%3/3
not(A) or B
A= not defined $valB= ref \$val eq "GLOB"
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $a == 0B= $b == 0
ABresult
0X0
100
111
Truth table100%3/3
A or B
A= int $val != $valB= &float_is_infinite($val)
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $val >= '-9007199254740992'B= $val <= '9007199254740992'
ABresult
0X0
100
111
Truth table100%3/3
A or B
A= $val < &min_natint()B= $val > "1.84467440737096e+19"
ABresult
1X1
011
000
Truth table100%5/5
A && B or C and D
A= $val >= &min_natint()B= $val < '18446744073709549568'C= $val <= "1.84467440737096e+19"D= $val - '18446744073709549568' != 2048
ABCDresult
0XXX0
100X0
10100
11XX1
10111
Truth table66%2/3
A and B
A= defined $cmpB= $cmp == 0
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $a >= $tB= $b >= $t
ABresult
0X0
100
111
73 }
74 2 ▶ die $@ if $@ ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 13 62 75
75 }
76  
77 51 1;
78  
79 __DATA__
↑ Summary lib/Scalar/Number.xs »