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 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 103     103   18371 use 5.004;
  103         388  
21 103     103   553 use strict;
  103         201  
  103         3260  
22              
23 103     103   637 use vars '$VERSION','@ISA','@EXPORT_OK';
  103         220  
  103         7200  
24             $VERSION = 128;
25              
26 103     103   716 use Exporter;
  103         216  
  103         17865  
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 1269339     1269339 1 1786556 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 1269339         1665538 my $int = int($x) + 0;
47 1269339 100       2752361 if ($x == $int) {
48             ### is an integer: "$int", $int
49 1269197         2315326 return $x;
50             }
51 142         1005 $x -= $int;
52             ### int: "$int"
53             ### frac: "$x"
54 142 100       1731 if ($x >= .5) {
55             ### round up ...
56 33         1089 return $int + 1;
57             }
58 109 100       2179 if ($x < -.5) {
59             ### round down ...
60 4         504 return $int - 1;
61             }
62             ### within +/- .5 ...
63 105         1750 return $int;
64             }
65              
66 103         8738 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     103   797 };
  103         359  
74 103         34185 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     103   745 };
  103         661  
82              
83             #------------------------------------------------------------------------------
84             # these not documented ...
85              
86             sub is_infinite {
87 687028     687028 0 1027588 my ($x) = @_;
88 687028   66     2861745 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 164843 my ($x) = @_;
102             ### floor(): "$x", $x
103 119810         148022 my $int = int($x);
104 119810 100       185159 if ($x == $int) {
105             ### is an integer ...
106 10286         17264 return $x;
107             }
108 109524         127344 $x -= $int;
109             ### frac: "$x"
110 109524 100       156388 if ($x >= 0) {
111             ### frac is non-negative ...
112 60568         99742 return $int;
113             } else {
114             ### frac is negative ...
115 48956         79812 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__