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, 2019, 2020, 2021 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 104     104   21485 use 5.004;
  104         397  
21 104     104   652 use strict;
  104         200  
  104         3332  
22              
23 104     104   611 use vars '$VERSION','@ISA','@EXPORT_OK';
  104         255  
  104         7664  
24             $VERSION = 129;
25              
26 104     104   733 use Exporter;
  104         225  
  104         19755  
27             @ISA = ('Exporter');
28             @EXPORT_OK = ('round_nearest',
29              
30             # not documented yet
31             'is_infinite',
32             'floor',
33             'xy_is_even',
34             'xy_is_visited_quad1',
35             );
36              
37             # uncomment this to run the ### lines
38             # use Smart::Comments;
39              
40              
41             # with a view to being friendly to BigRat/BigFloat
42             sub round_nearest {
43 1269633     1269633 1 1858088 my ($x) = @_;
44             ### round_nearest(): "$x", $x
45              
46             # BigRat through to perl 5.12.4 has some dodginess giving a bigint -0
47             # which is considered !=0. Adding +0 to numify seems to avoid the problem.
48 1269633         1736805 my $int = int($x) + 0;
49 1269633 100       2862215 if ($x == $int) {
50             ### is an integer: "$int", $int
51 1269491         2414392 return $x;
52             }
53 142         1054 $x -= $int;
54             ### int: "$int"
55             ### frac: "$x"
56 142 100       1798 if ($x >= .5) {
57             ### round up ...
58 33         1021 return $int + 1;
59             }
60 109 100       2251 if ($x < -.5) {
61             ### round down ...
62 4         513 return $int - 1;
63             }
64             ### within +/- .5 ...
65 105         1776 return $int;
66             }
67              
68 104         9338 use constant parameter_info_nstart0 => { name => 'n_start',
69             share_key => 'n_start_0',
70             display => 'N Start',
71             type => 'integer',
72             default => 0,
73             width => 3,
74             description => 'Starting N.',
75 104     104   886 };
  104         413  
76 104         39395 use constant parameter_info_nstart1 => { name => 'n_start',
77             share_key => 'n_start_1',
78             display => 'N Start',
79             type => 'integer',
80             default => 1,
81             width => 3,
82             description => 'Starting N.',
83 104     104   768 };
  104         726  
84              
85             #------------------------------------------------------------------------------
86             # these not documented ...
87              
88             sub is_infinite {
89 688051     688051 0 1082236 my ($x) = @_;
90 688051   66     2916239 return ($x != $x # nan
91             || ($x != 0 && $x == 2*$x)); # inf
92             }
93              
94             # With a view to being friendly to BigRat/BigFloat.
95             #
96             # For reference, POSIX::floor() in perl 5.12.4 is a bit bizarre on UV=64bit
97             # and NV=53bit double. UV=2^64-1 rounds up to NV=2^64 which floor() then
98             # returns, so floor() in fact increases the value of what was an integer
99             # already.
100             #
101             # not documented yet
102             sub floor {
103 119810     119810 0 193542 my ($x) = @_;
104             ### floor(): "$x", $x
105 119810         171388 my $int = int($x);
106 119810 100       211642 if ($x == $int) {
107             ### is an integer ...
108 10286         19691 return $x;
109             }
110 109524         149893 $x -= $int;
111             ### frac: "$x"
112 109524 100       178351 if ($x >= 0) {
113             ### frac is non-negative ...
114 60567         117292 return $int;
115             } else {
116             ### frac is negative ...
117 48957         92221 return $int-1;
118             }
119             }
120              
121             # not documented yet
122             sub xy_is_visited_quad1 { # or maybe base class with class_x_negative too
123 0     0 0   my ($self, $x, $y) = @_;
124 0   0       return ! (2*$x < -1 || 2*$y < -1);
125             }
126             # not documented yet
127             sub xy_is_visited_quad12 {
128 0     0 0   my ($self, $x, $y) = @_;
129 0           return (2*$y >= -1);
130             }
131             # not documented yet
132             sub _xy_is_visited_x_positive {
133 0     0     my ($self, $x, $y) = @_;
134 0           return (2*$x >= -1);
135             }
136             # not documented yet
137             sub xy_is_even {
138 0     0 0   my ($self, $x, $y) = @_;
139 0           return (round_nearest($x)%2 == round_nearest($y)%2);
140             }
141              
142             1;
143             __END__