File Coverage

blib/lib/Math/NumSeq/All.pm
Criterion Covered Total %
statement 67 67 100.0
branch 10 10 100.0
condition n/a
subroutine 22 22 100.0
pod 12 12 100.0
total 111 111 100.0


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
2              
3             # This file is part of Math-NumSeq.
4             #
5             # Math-NumSeq is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Math-NumSeq 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-NumSeq. If not, see .
17              
18             package Math::NumSeq::All;
19 3     3   11070 use 5.004;
  3         10  
  3         137  
20 3     3   27 use strict;
  3         6  
  3         117  
21              
22 3     3   15 use vars '$VERSION', '@ISA';
  3         6  
  3         221  
23             $VERSION = 71;
24 3     3   885 use Math::NumSeq;
  3         6  
  3         240  
25             @ISA = ('Math::NumSeq');
26              
27             # uncomment this to run the ### lines
28             #use Smart::Comments;
29              
30             # use constant name => Math::NumSeq::__('All Integers');
31 3     3   17 use constant description => Math::NumSeq::__('All integers 0,1,2,3,etc.');
  3         6  
  3         13  
32 3     3   15 use constant default_i_start => 0;
  3         4  
  3         148  
33 3     3   22 use constant characteristic_increasing => 1;
  3         6  
  3         162  
34 3     3   15 use constant characteristic_integer => 1;
  3         4  
  3         1854  
35              
36             # experimental i_start to get natural numbers ... probably not very important
37             # OEIS-Catalogue: A000027 i_start=1
38             # OEIS-Catalogue: A001477
39             my %oeis_anum = (0 => 'A001477', # non-negatives, starting 0
40             1 => 'A000027'); # natural numbers starting 1
41             sub oeis_anum {
42 2     2 1 10 my ($self) = @_;
43 2         8 return $oeis_anum{$self->i_start};
44             }
45             sub values_min {
46 2     2 1 14 my ($self) = @_;
47 2         8 return $self->i_start;
48             }
49              
50             sub rewind {
51 10     10 1 1014 my ($self) = @_;
52 10         45 $self->seek_to_i($self->i_start);
53             }
54             sub seek_to_i {
55 46     46 1 320 my ($self, $i) = @_;
56 46         131 $self->{'i'} = $i;
57             }
58             sub seek_to_value {
59 22     22 1 2793 my ($self, $value) = @_;
60 22         46 $self->seek_to_i($self->value_to_i_ceil($value));
61             }
62             sub next {
63 107     107 1 7312 my ($self) = @_;
64 107         181 my $i = $self->{'i'}++;
65 107         251 return ($i, $i);
66             }
67              
68             sub ith {
69 48     48 1 136 my ($self, $i) = @_;
70 48         135 return $i;
71             }
72              
73             sub pred {
74 40     40 1 239 my ($self, $value) = @_;
75 40         93 return ($value == int($value));
76             }
77              
78             sub value_to_i {
79 40     40 1 161 my ($self, $value) = @_;
80 40 100       94 if ($value == int($value)) {
81 32         573 return $value;
82             }
83 8         38 return undef;
84             }
85             sub value_to_i_floor {
86 54     54 1 188 my ($self, $value) = @_;
87 54         96 return _floor($value);
88             }
89             sub value_to_i_ceil {
90 67     67 1 179 my ($self, $value) = @_;
91 67         115 return _ceil($value);
92             }
93             sub value_to_i_estimate {
94 32     32 1 847 my ($self, $value) = @_;
95 32         59 return int($value);
96             }
97              
98             #------------------------------------------------------------------------------
99             # generic
100              
101             # _floor() trying to work with BigRat/BigFloat too.
102             #
103             # For reference, POSIX::floor() in perl 5.12.4 is a bit bizarre on UV=64bit
104             # and NV=53bit double. UV=2^64-1 rounds up to NV=2^64 which floor() then
105             # returns, so floor() in fact increases the value of what was an integer
106             # already.
107             #
108             sub _floor {
109 331     331   5339 my ($x) = @_;
110             ### _floor(): "$x", $x
111 331         493 my $int = int($x);
112 331 100       1375 if ($x == $int) {
113             ### is an integer ...
114 173         1619 return $x;
115             }
116 158         189 $x -= $int;
117             ### frac: "$x"
118 158 100       271 if ($x >= 0) {
119             ### frac is non-negative ...
120 130         334 return $int;
121             } else {
122             ### frac is negative ...
123 28         178 return $int-1;
124             }
125             }
126              
127             # _ceil() trying to work with BigRat/BigFloat too.
128             #
129             sub _ceil {
130 67     67   87 my ($x) = @_;
131             ### _ceil(): "$x", $x
132              
133 67         108 my $int = int($x);
134 67 100       332 if ($x == $int) {
135             ### is an integer ...
136 56         483 return $x;
137             }
138 11         16 $x -= $int;
139             ### frac: "$x"
140 11 100       71 if ($x > 0) {
141             ### frac is positive ...
142 6         24 return $int+1;
143             } else {
144             ### frac is non-negative ...
145 5         23 return $int;
146             }
147             }
148              
149             1;
150             __END__