PlanePath/Base/Generic.pm

filter: Statementstmt 83.6% 41 / 49 Branchbran 100% 10 / 10 Conditioncond 66.6% 6 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 69.2% 9 / 13 Podpod 16.6% 1 / 6 totaltotal 76.0% 70 / 92 CC 9 SCAR 10.2
CC
9
Cov
84%
xy_is_visited_quad1
6.0
round_nearest
4.0
is_infinite
3.0
CRAP 2.8
« PlanePath/HexArms.pm ↑ Summary NumSeq/PlanePathN.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Math::PlanePath::Base::Generic;
20 103 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 103 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 103 ▶use vars '$VERSION','@ISA','@EXPORT_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 103 $VERSION = 128;
25  
26 103 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 103 @ISA = ('Exporter');
28 103 @EXPORT_OK = ('round_nearest',
29  
30 # not documented yet
31 'is_infinite',
32 'floor',
33 'xy_is_even');
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38  
39 # with a view to being friendly to BigRat/BigFloat
40 sub round_nearest {
41 1269161 ▶ my ($x) = @_;
Subroutine round_nearest: called CC=4 SCAR=13.9
Pod documented
42 ### round_nearest(): "$x", $x
43  
44 # BigRat through to perl 5.12.4 has some dodginess giving a bigint -0
45 # which is considered !=0. Adding +0 to numify seems to avoid the problem.
46 1269161 my $int = int($x) + 0;
47 1269161 ▶ if ($x == $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $int) 1269019 142 1269161
48 ### is an integer: "$int", $int
49 1269019 return $x;
50 }
51 142 $x -= $int;
52 ### int: "$int"
53 ### frac: "$x"
54 142 ▶ if ($x >= .5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= "0.5") 33 109 142
55 ### round up ...
56 33 return $int + 1;
57 }
58 109 ▶ if ($x < -.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < "-0.5") 4 105 109
59 ### round down ...
60 4 return $int - 1;
61 }
62 ### within +/- .5 ...
63 105 return $int;
64 }
65  
66 103 use constant parameter_info_nstart0 => { name => 'n_start',
67 share_key => 'n_start_0',
68 display => 'N Start',
69 type => 'integer',
70 default => 0,
71 width => 3,
72 description => 'Starting N.',
73 103 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
74 103 use constant parameter_info_nstart1 => { name => 'n_start',
75 share_key => 'n_start_1',
76 display => 'N Start',
77 type => 'integer',
78 default => 1,
79 width => 3,
80 description => 'Starting N.',
81 103 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 #------------------------------------------------------------------------------
84 # these not documented ...
85  
86 sub is_infinite {
87 685952 ▶ my ($x) = @_;
Subroutine is_infinite: called CC=3 SCAR=11.0
Pod undocumented
88 685952 ▶ return ($x != $x # nan
Condition100%6/6
$x != $x || $x != 0 && $x == 2 * $x
l!l&&r!l&&!r
16685945
$x != 0 and $x == 2 * $x
!ll&&!rl&&r
450456409006
MC/DC100%3/3
$x != $x || $x != 0 && $x == 2 * $x
$x != $x $x != 0 $x == 2 * $x
Truth table100%4/4
A || B and C
A= $x != $xB= $x != 0C= $x == 2 * $x
ABCresult
1XX1
0111
00X0
0100
89 || ($x != 0 && $x == 2*$x)); # inf
90 }
91  
92 # With a view to being friendly to BigRat/BigFloat.
93 #
94 # For reference, POSIX::floor() in perl 5.12.4 is a bit bizarre on UV=64bit
95 # and NV=53bit double. UV=2^64-1 rounds up to NV=2^64 which floor() then
96 # returns, so floor() in fact increases the value of what was an integer
97 # already.
98 #
99 # not documented yet
100 sub floor {
101 119810 ▶ my ($x) = @_;
Subroutine floor: called CC=3 SCAR=11.0
Pod undocumented
102 ### floor(): "$x", $x
103 119810 my $int = int($x);
104 119810 ▶ if ($x == $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $int) 10286 109524 119810
105 ### is an integer ...
106 10286 return $x;
107 }
108 109524 $x -= $int;
109 ### frac: "$x"
110 109524 ▶ if ($x >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0) { } 60566 48958 109524
111 ### frac is non-negative ...
112 60566 return $int;
113 } else {
114 ### frac is negative ...
115 48958 return $int-1;
116 }
117 }
118  
119 # not documented yet
120 sub xy_is_visited_quad1 {
121 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited_quad1: not called CC=2 SCAR=17.9
Pod undocumented
122 0 ▶ return ! (2*$x < -1 || 2*$y < -1);
Condition0%0/3
2 * $x < -1 || 2 * $y < -1
l!l&&r!l&&!r
000
MC/DC0%0/2
2 * $x < -1 || 2 * $y < -1
2 * $x < -1 2 * $y < -1
Truth table0%0/3
A || B
A= 2 * $x < -1B= 2 * $y < -1
ABresult
1X1
011
000
123 }
124 # not documented yet
125 sub xy_is_visited_quad12 {
126 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited_quad12: not called CC=1 SCAR=6.9
Pod undocumented
127 0 return (2*$y >= -1);
128 }
129 # not documented yet
130 sub _xy_is_visited_x_positive {
131 0 ▶ my ($self, $x, $y) = @_;
Subroutine _xy_is_visited_x_positive: not called CC=1 SCAR=6.9
132 0 return (2*$x >= -1);
133 }
134 # not documented yet
135 sub xy_is_even {
136 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_even: not called CC=1 SCAR=6.9
Pod undocumented
137 0 return (round_nearest($x)%2 == round_nearest($y)%2);
138 }
139  
140 103 1;
141 __END__
« PlanePath/HexArms.pm ↑ Summary NumSeq/PlanePathN.pm »