| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 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 <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | package Math::NumSeq::All; | ||||||||||
| 19 | 3 | ▶ | use 5.004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 3 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 3 | $VERSION = 75; | |||||||||
| 24 | 3 | ▶ | use Math::NumSeq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 3 | @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 | ▶ | use constant description => Math::NumSeq::__('All integers 0,1,2,3,etc.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 3 | ▶ | use constant default_i_start => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 3 | ▶ | use constant characteristic_increasing => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 3 | ▶ | use constant characteristic_integer => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 3 | my %oeis_anum = (0 => 'A001477', # non-negatives, starting 0 | |||||||||
| 40 | 1 => 'A000027'); # natural numbers starting 1 | ||||||||||
| 41 | 2 | ▶ | sub oeis_anum { | ||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 2 | my ($self) = @_; | |||||||||
| 43 | 2 | return $oeis_anum{$self->i_start}; | |||||||||
| 44 | } | ||||||||||
| 45 | 2 | ▶ | sub values_min { | ||||||||
|
Subroutine
values_min: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 2 | my ($self) = @_; | |||||||||
| 47 | 2 | return $self->i_start; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 10 | ▶ | sub rewind { | ||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 51 | 10 | my ($self) = @_; | |||||||||
| 52 | 10 | $self->seek_to_i($self->i_start); | |||||||||
| 53 | } | ||||||||||
| 54 | 46 | ▶ | sub seek_to_i { | ||||||||
|
Subroutine
seek_to_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | 46 | my ($self, $i) = @_; | |||||||||
| 56 | 46 | $self->{'i'} = $i; | |||||||||
| 57 | } | ||||||||||
| 58 | 22 | ▶ | sub seek_to_value { | ||||||||
|
Subroutine
seek_to_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 22 | my ($self, $value) = @_; | |||||||||
| 60 | 22 | $self->seek_to_i($self->value_to_i_ceil($value)); | |||||||||
| 61 | } | ||||||||||
| 62 | 107 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | 107 | my ($self) = @_; | |||||||||
| 64 | 107 | my $i = $self->{'i'}++; | |||||||||
| 65 | 107 | return ($i, $i); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 48 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 48 | my ($self, $i) = @_; | |||||||||
| 70 | 48 | return $i; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 40 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 74 | 40 | my ($self, $value) = @_; | |||||||||
| 75 | 40 | return ($value == int($value)); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 40 | ▶ | sub value_to_i { | ||||||||
|
Subroutine
value_to_i: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 79 | 40 | my ($self, $value) = @_; | |||||||||
| 80 | 40 | ▶ | if ($value == int($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 32 | return $value; | |||||||||
| 82 | } | ||||||||||
| 83 | 8 | return undef; | |||||||||
| 84 | } | ||||||||||
| 85 | 54 | ▶ | sub value_to_i_floor { | ||||||||
|
Subroutine
value_to_i_floor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 86 | 54 | my ($self, $value) = @_; | |||||||||
| 87 | 54 | return _floor($value); | |||||||||
| 88 | } | ||||||||||
| 89 | 67 | ▶ | sub value_to_i_ceil { | ||||||||
|
Subroutine
value_to_i_ceil: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 67 | my ($self, $value) = @_; | |||||||||
| 91 | 67 | return _ceil($value); | |||||||||
| 92 | } | ||||||||||
| 93 | 32 | ▶ | sub value_to_i_estimate { | ||||||||
|
Subroutine
value_to_i_estimate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 94 | 32 | my ($self, $value) = @_; | |||||||||
| 95 | 32 | 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 | 331 | ▶ | sub _floor { | ||||||||
|
Subroutine
_floor: called CC=3 SCAR=11.0
| |||||||||||
| 109 | 331 | my ($x) = @_; | |||||||||
| 110 | ### _floor(): "$x", $x | ||||||||||
| 111 | 331 | my $int = int($x); | |||||||||
| 112 | 331 | ▶ | if ($x == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | ### is an integer ... | ||||||||||
| 114 | 173 | return $x; | |||||||||
| 115 | } | ||||||||||
| 116 | 158 | $x -= $int; | |||||||||
| 117 | ### frac: "$x" | ||||||||||
| 118 | 158 | ▶ | if ($x >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | ### frac is non-negative ... | ||||||||||
| 120 | 130 | return $int; | |||||||||
| 121 | } else { | ||||||||||
| 122 | ### frac is negative ... | ||||||||||
| 123 | 28 | return $int-1; | |||||||||
| 124 | } | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | # _ceil() trying to work with BigRat/BigFloat too. | ||||||||||
| 128 | # | ||||||||||
| 129 | 67 | ▶ | sub _ceil { | ||||||||
|
Subroutine
_ceil: called CC=3 SCAR=11.0
| |||||||||||
| 130 | 67 | my ($x) = @_; | |||||||||
| 131 | ### _ceil(): "$x", $x | ||||||||||
| 132 | |||||||||||
| 133 | 67 | my $int = int($x); | |||||||||
| 134 | 67 | ▶ | if ($x == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | ### is an integer ... | ||||||||||
| 136 | 56 | return $x; | |||||||||
| 137 | } | ||||||||||
| 138 | 11 | $x -= $int; | |||||||||
| 139 | ### frac: "$x" | ||||||||||
| 140 | 11 | ▶ | if ($x > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | ### frac is positive ... | ||||||||||
| 142 | 6 | return $int+1; | |||||||||
| 143 | } else { | ||||||||||
| 144 | ### frac is non-negative ... | ||||||||||
| 145 | 5 | return $int; | |||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 3 | 1; | |||||||||
| 150 | __END__ | ||||||||||