File Coverage

blib/lib/Math/NumSeq/All.pm
Criterion Covered Total %
statement 66 66 100.0
branch 10 10 100.0
condition n/a
subroutine 22 22 100.0
pod 12 12 100.0
total 110 110 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   5867 use 5.004;
  3         8  
20 3     3   13 use strict;
  3         3  
  3         93  
21              
22 3     3   10 use vars '$VERSION', '@ISA';
  3         4  
  3         150  
23             $VERSION = 72;
24 3     3   359 use Math::NumSeq;
  3         2  
  3         102  
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   12 use constant description => Math::NumSeq::__('All integers 0,1,2,3,etc.');
  3         7  
  3         13  
32 3     3   13 use constant default_i_start => 0;
  3         7  
  3         134  
33 3     3   12 use constant characteristic_increasing => 1;
  3         3  
  3         122  
34 3     3   11 use constant characteristic_integer => 1;
  3         4  
  3         1357  
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 7 my ($self) = @_;
43 2         4 return $oeis_anum{$self->i_start};
44             }
45             sub values_min {
46 2     2 1 7 my ($self) = @_;
47 2         5 return $self->i_start;
48             }
49              
50             sub rewind {
51 10     10 1 660 my ($self) = @_;
52 10         25 $self->seek_to_i($self->i_start);
53             }
54             sub seek_to_i {
55 46     46 1 150 my ($self, $i) = @_;
56 46         62 $self->{'i'} = $i;
57             }
58             sub seek_to_value {
59 22     22 1 1728 my ($self, $value) = @_;
60 22         28 $self->seek_to_i($self->value_to_i_ceil($value));
61             }
62             sub next {
63 107     107 1 976 my ($self) = @_;
64 107         87 my $i = $self->{'i'}++;
65 107         110 return ($i, $i);
66             }
67              
68             sub ith {
69 48     48 1 69 my ($self, $i) = @_;
70 48         49 return $i;
71             }
72              
73             sub pred {
74 40     40 1 122 my ($self, $value) = @_;
75 40         37 return ($value == int($value));
76             }
77              
78             sub value_to_i {
79 40     40 1 93 my ($self, $value) = @_;
80 40 100       64 if ($value == int($value)) {
81 32         360 return $value;
82             }
83 8         8 return undef;
84             }
85             sub value_to_i_floor {
86 54     54 1 112 my ($self, $value) = @_;
87 54         63 return _floor($value);
88             }
89             sub value_to_i_ceil {
90 67     67 1 101 my ($self, $value) = @_;
91 67         76 return _ceil($value);
92             }
93             sub value_to_i_estimate {
94 32     32 1 439 my ($self, $value) = @_;
95 32         26 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   4230 my ($x) = @_;
110             ### _floor(): "$x", $x
111 331         334 my $int = int($x);
112 331 100       916 if ($x == $int) {
113             ### is an integer ...
114 173         1259 return $x;
115             }
116 158         140 $x -= $int;
117             ### frac: "$x"
118 158 100       176 if ($x >= 0) {
119             ### frac is non-negative ...
120 130         186 return $int;
121             } else {
122             ### frac is negative ...
123 28         54 return $int-1;
124             }
125             }
126              
127             # _ceil() trying to work with BigRat/BigFloat too.
128             #
129             sub _ceil {
130 67     67   46 my ($x) = @_;
131             ### _ceil(): "$x", $x
132              
133 67         66 my $int = int($x);
134 67 100       208 if ($x == $int) {
135             ### is an integer ...
136 56         275 return $x;
137             }
138 11         10 $x -= $int;
139             ### frac: "$x"
140 11 100       18 if ($x > 0) {
141             ### frac is positive ...
142 6         14 return $int+1;
143             } else {
144             ### frac is non-negative ...
145 5         14 return $int;
146             }
147             }
148              
149             1;
150             __END__