File Coverage

blib/lib/Math/PlanePath/Base/Generic.pm
Criterion Covered Total %
statement 37 45 82.2
branch 10 10 100.0
condition 2 6 33.3
subroutine 9 13 69.2
pod 1 6 16.6
total 59 80 73.7


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 .
17              
18              
19             package Math::PlanePath::Base::Generic;
20 101     101   18745 use 5.004;
  101         380  
21 101     101   557 use strict;
  101         209  
  101         3233  
22              
23 101     101   544 use vars '$VERSION','@ISA','@EXPORT_OK';
  101         254  
  101         7016  
24             $VERSION = 127;
25              
26 101     101   691 use Exporter;
  101         219  
  101         16940  
27             @ISA = ('Exporter');
28             @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 1267587     1267587 1 1806742 my ($x) = @_;
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 1267587         1707997 my $int = int($x) + 0;
47 1267587 100       2780789 if ($x == $int) {
48             ### is an integer: "$int", $int
49 1267461         2385815 return $x;
50             }
51 126         811 $x -= $int;
52             ### int: "$int"
53             ### frac: "$x"
54 126 100       1410 if ($x >= .5) {
55             ### round up ...
56 28         824 return $int + 1;
57             }
58 98 100       1825 if ($x < -.5) {
59             ### round down ...
60 4         419 return $int - 1;
61             }
62             ### within +/- .5 ...
63 94         1476 return $int;
64             }
65              
66 101         8777 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 101     101   734 };
  101         242  
74 101         32671 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 101     101   728 };
  101         406  
82              
83             #------------------------------------------------------------------------------
84             # these not documented ...
85              
86             sub is_infinite {
87 596241     596241 0 906494 my ($x) = @_;
88 596241   66     2488959 return ($x != $x # nan
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     119810 0 187037 my ($x) = @_;
102             ### floor(): "$x", $x
103 119810         179572 my $int = int($x);
104 119810 100       210793 if ($x == $int) {
105             ### is an integer ...
106 10286         18172 return $x;
107             }
108 109524         146714 $x -= $int;
109             ### frac: "$x"
110 109524 100       175708 if ($x >= 0) {
111             ### frac is non-negative ...
112 60574         119441 return $int;
113             } else {
114             ### frac is negative ...
115 48950         91326 return $int-1;
116             }
117             }
118              
119             # not documented yet
120             sub xy_is_visited_quad1 {
121 0     0 0   my ($self, $x, $y) = @_;
122 0   0       return ! (2*$x < -1 || 2*$y < -1);
123             }
124             # not documented yet
125             sub xy_is_visited_quad12 {
126 0     0 0   my ($self, $x, $y) = @_;
127 0           return (2*$y >= -1);
128             }
129             # not documented yet
130             sub _xy_is_visited_x_positive {
131 0     0     my ($self, $x, $y) = @_;
132 0           return (2*$x >= -1);
133             }
134             # not documented yet
135             sub xy_is_even {
136 0     0 0   my ($self, $x, $y) = @_;
137 0           return (round_nearest($x)%2 == round_nearest($y)%2);
138             }
139              
140             1;
141             __END__