| 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 <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||||||
| 20 | # bit_join_lowtohigh | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | package Math::PlanePath::Base::Digits; | ||||||||||||||||||||||||||||||||
| 24 | 63 | ▶ | use 5.004; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | 63 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 63 | ▶ | use vars '$VERSION','@ISA','@EXPORT_OK'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | 63 | $VERSION = 128; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 63 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | 63 | @ISA = ('Exporter'); | |||||||||||||||||||||||||||||||
| 32 | 63 | @EXPORT_OK = ('parameter_info_array', | |||||||||||||||||||||||||||||||
| 33 | 'bit_split_lowtohigh', | ||||||||||||||||||||||||||||||||
| 34 | 'digit_split_lowtohigh', | ||||||||||||||||||||||||||||||||
| 35 | 'digit_join_lowtohigh', | ||||||||||||||||||||||||||||||||
| 36 | 'round_down_pow', | ||||||||||||||||||||||||||||||||
| 37 | 'round_up_pow'); | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||
| 40 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 63 | use constant parameter_info_radix2 => { name => 'radix', | |||||||||||||||||||||||||||||||
| 44 | share_key => 'radix_2', | ||||||||||||||||||||||||||||||||
| 45 | display => 'Radix', | ||||||||||||||||||||||||||||||||
| 46 | type => 'integer', | ||||||||||||||||||||||||||||||||
| 47 | minimum => 2, | ||||||||||||||||||||||||||||||||
| 48 | default => 2, | ||||||||||||||||||||||||||||||||
| 49 | width => 3, | ||||||||||||||||||||||||||||||||
| 50 | description => 'Radix (number base).', | ||||||||||||||||||||||||||||||||
| 51 | 63 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | 63 | ▶ | use constant parameter_info_array => [ parameter_info_radix2() ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # ENHANCE-ME: Sometimes the $pow value is not wanted, | ||||||||||||||||||||||||||||||||
| 58 | # eg. SierpinskiArrowhead, though that tends to be approximation code rather | ||||||||||||||||||||||||||||||||
| 59 | # than exact range calculations etc. | ||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||
| 61 | sub round_down_pow { | ||||||||||||||||||||||||||||||||
| 62 | 65989 | ▶ | my ($n, $base) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
round_down_pow: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | ### round_down_pow(): "$n base $base" | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # only for integer bases | ||||||||||||||||||||||||||||||||
| 66 | ### assert: $base == int($base) | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 65989 | ▶ | if ($n < $base) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 26988 | return (1, 0); | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | # Math::BigInt and Math::BigRat overloaded log() return NaN, use integer | ||||||||||||||||||||||||||||||||
| 73 | # based blog() | ||||||||||||||||||||||||||||||||
| 74 | 39001 | ▶ | if (ref $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 9 | ▶ | if ($n->isa('Math::BigRat')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 0 | $n = int($n); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | 9 | ▶ | if ($n->isa('Math::BigInt') || $n->isa('Math::BigInt::Lite')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n->isa("Math::BigInt") or $n->isa("Math::BigInt::Lite")
MC/DC50%1/2
$n->isa("Math::BigInt") or $n->isa("Math::BigInt::Lite")
$n->isa("Math::BigInt")
$n->isa("Math::BigInt::Lite")
Truth table66%2/3
A or B
A= $n->isa("Math::BigInt")B= $n->isa("Math::BigInt::Lite")
| |||||||||||||||||||||||||||||||||
| 79 | ### use blog() ... | ||||||||||||||||||||||||||||||||
| 80 | 7 | my $exp = $n->copy->blog($base); | |||||||||||||||||||||||||||||||
| 81 | ### exp: "$exp" | ||||||||||||||||||||||||||||||||
| 82 | 7 | return (Math::BigInt->new(1)->blsft($exp,$base), | |||||||||||||||||||||||||||||||
| 83 | $exp); | ||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 38994 | my $exp = int(log($n)/log($base)); | |||||||||||||||||||||||||||||||
| 88 | 38994 | my $pow = $base**$exp; | |||||||||||||||||||||||||||||||
| 89 | ### n: ref($n)." $n" | ||||||||||||||||||||||||||||||||
| 90 | ### exp: ref($exp)." $exp" | ||||||||||||||||||||||||||||||||
| 91 | ### pow: ref($pow)." $pow" | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # check how $pow actually falls against $n, not sure should trust float | ||||||||||||||||||||||||||||||||
| 94 | # rounding in log()/log($base) | ||||||||||||||||||||||||||||||||
| 95 | # Crib: $n as first arg in case $n==BigFloat and $pow==BigInt | ||||||||||||||||||||||||||||||||
| 96 | 38994 | ▶ | if ($n < $pow) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 97 | ### hmm, int(log) too big, decrease ... | ||||||||||||||||||||||||||||||||
| 98 | 2 | $exp -= 1; | |||||||||||||||||||||||||||||||
| 99 | 2 | $pow = $base**$exp; | |||||||||||||||||||||||||||||||
| 100 | } elsif ($n >= $base*$pow) { | ||||||||||||||||||||||||||||||||
| 101 | ### hmm, int(log) too small, increase ... | ||||||||||||||||||||||||||||||||
| 102 | 15 | $exp += 1; | |||||||||||||||||||||||||||||||
| 103 | 15 | $pow *= $base; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | ### result ... | ||||||||||||||||||||||||||||||||
| 107 | ### pow: "$pow" | ||||||||||||||||||||||||||||||||
| 108 | ### exp: "$exp" | ||||||||||||||||||||||||||||||||
| 109 | ### $exp | ||||||||||||||||||||||||||||||||
| 110 | 38994 | return ($pow, $exp); | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | sub round_up_pow { | ||||||||||||||||||||||||||||||||
| 114 | 118 | ▶ | my ($n, $base) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
round_up_pow: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 115 | ### round_up_pow(): "$n base $base" | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # only for integer bases | ||||||||||||||||||||||||||||||||
| 118 | ### assert: $base == int($base) | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 118 | ▶ | if ($n < 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | 1 | return (1, 0); | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # Math::BigInt and Math::BigRat overloaded log() return NaN, use integer | ||||||||||||||||||||||||||||||||
| 125 | # based blog() | ||||||||||||||||||||||||||||||||
| 126 | 117 | ▶ | if (ref $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | ### $n | ||||||||||||||||||||||||||||||||
| 128 | 5 | ▶ | if ($n->isa('Math::BigRat')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 0 | $n = int($n); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | if ($n->isa('Math::BigInt') || $n->isa('Math::BigInt::Lite')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n->isa("Math::BigInt") or $n->isa("Math::BigInt::Lite")
MC/DC0%0/2
$n->isa("Math::BigInt") or $n->isa("Math::BigInt::Lite")
$n->isa("Math::BigInt")
$n->isa("Math::BigInt::Lite")
Truth table33%1/3
A or B
A= $n->isa("Math::BigInt")B= $n->isa("Math::BigInt::Lite")
| |||||||||||||||||||||||||||||||||
| 132 | ### use blog(): ref $n | ||||||||||||||||||||||||||||||||
| 133 | 5 | my $exp = $n->copy->blog($base); | |||||||||||||||||||||||||||||||
| 134 | ### exp: $exp | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 5 | my $pow = (ref $n)->new(1)->blsft($exp,$base); | |||||||||||||||||||||||||||||||
| 137 | # Crib: must have $n first to have Math::BigInt::Lite method preferred | ||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | if ($n > $pow) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | ### blog too small, increase ... | ||||||||||||||||||||||||||||||||
| 140 | 3 | $pow *= $base; | |||||||||||||||||||||||||||||||
| 141 | 3 | $exp += 1; | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | 5 | return ($pow, $exp); | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 112 | my $exp = int(log($n)/log($base) + 1); | |||||||||||||||||||||||||||||||
| 148 | 112 | my $pow = $base**$exp; | |||||||||||||||||||||||||||||||
| 149 | ### n: ref($n)." $n" | ||||||||||||||||||||||||||||||||
| 150 | ### exp: ref($exp)." $exp" | ||||||||||||||||||||||||||||||||
| 151 | ### pow: ref($pow)." $pow" | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | # check how $pow actually falls against $n, not sure should trust float | ||||||||||||||||||||||||||||||||
| 154 | # rounding in log()/log($base) | ||||||||||||||||||||||||||||||||
| 155 | # Crib: $n as first arg in case $n==BigFloat and $pow==BigInt | ||||||||||||||||||||||||||||||||
| 156 | 112 | ▶ | if ($exp > 0 && $n <= $pow/$base) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$exp > 0 and $n <= $pow / $base
MC/DC50%1/2
$exp > 0 and $n <= $pow / $base
$exp > 0
$n <= $pow / $base
Truth table66%2/3
A and B
A= $exp > 0B= $n <= $pow / $base
| |||||||||||||||||||||||||||||||||
| 157 | ### hmm, int(log) too big, decrease... | ||||||||||||||||||||||||||||||||
| 158 | 28 | $exp -= 1; | |||||||||||||||||||||||||||||||
| 159 | 28 | $pow = $base**$exp; | |||||||||||||||||||||||||||||||
| 160 | } elsif ($n > $pow) { | ||||||||||||||||||||||||||||||||
| 161 | ### hmm, int(log)+1 too small, increase... | ||||||||||||||||||||||||||||||||
| 162 | 1 | $exp += 1; | |||||||||||||||||||||||||||||||
| 163 | 1 | $pow *= $base; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | 112 | return ($pow, $exp); | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||||||
| 170 | 63 | my %binary_to_base4 = ('00' => '0', | |||||||||||||||||||||||||||||||
| 171 | '01' => '1', | ||||||||||||||||||||||||||||||||
| 172 | '10' => '2', | ||||||||||||||||||||||||||||||||
| 173 | '11' => '3'); | ||||||||||||||||||||||||||||||||
| 174 | 63 | my @bigint_coderef; | |||||||||||||||||||||||||||||||
| 175 | $bigint_coderef[4] = sub { | ||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | (my $str = $_[0]->as_bin) =~ s/^0b//; # strip leading 0b | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 177 | 4 | ▶ | if (length($str) & 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 1 | $str = "0$str"; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | 4 | $str =~ s/(..)/$binary_to_base4{$1}/ge; | |||||||||||||||||||||||||||||||
| 181 | 4 | return reverse split //, $str; | |||||||||||||||||||||||||||||||
| 182 | 63 | }; | |||||||||||||||||||||||||||||||
| 183 | $bigint_coderef[8] = sub { | ||||||||||||||||||||||||||||||||
| 184 | 2 | ▶ | (my $str = $_[0]->as_oct) =~ s/^0//; # strip leading 0 | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 185 | 2 | return reverse split //, $str; | |||||||||||||||||||||||||||||||
| 186 | 63 | }; | |||||||||||||||||||||||||||||||
| 187 | $bigint_coderef[10] = sub { | ||||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | return reverse split //, $_[0]->bstr; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 189 | 63 | }; | |||||||||||||||||||||||||||||||
| 190 | $bigint_coderef[16] = sub { | ||||||||||||||||||||||||||||||||
| 191 | 2 | ▶ | (my $str = $_[0]->as_hex) =~ s/^0x//; # strip leading 0x | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 192 | 2 | return reverse map {hex} split //, $str; | |||||||||||||||||||||||||||||||
| 193 | 63 | }; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | # In _divrem() and _digit_split_lowtohigh() divide using rem=n%d then | ||||||||||||||||||||||||||||||||
| 196 | # q=(n-rem)/d so that quotient is an exact division. If it's not exact | ||||||||||||||||||||||||||||||||
| 197 | # then goes to float and loses precision if UV=64bit NV=53bit. | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | sub digit_split_lowtohigh { | ||||||||||||||||||||||||||||||||
| 200 | 359746 | ▶ | my ($n, $radix) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
digit_split_lowtohigh: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 201 | ### _digit_split_lowtohigh(): $n | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 359746 | ▶ | $n || return; # don't return '0' from BigInt stringize | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 343511 | ▶ | if ($radix == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 205 | 4740 | return bit_split_lowtohigh($n); | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 338771 | my @ret; | |||||||||||||||||||||||||||||||
| 209 | 338771 | ▶ | if (ref $n && $n->isa('Math::BigInt')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $n and $n->isa("Math::BigInt")
MC/DC100%2/2
ref $n and $n->isa("Math::BigInt")
ref $n
$n->isa("Math::BigInt")
Truth table100%3/3
A and B
A= ref $nB= $n->isa("Math::BigInt")
| |||||||||||||||||||||||||||||||||
| 210 | 19 | ▶ | if (my $coderef = $bigint_coderef[$radix]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | 10 | return $coderef->($_[0]); | |||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | 9 | $n = $n->copy; # for bdiv() modification | |||||||||||||||||||||||||||||||
| 214 | 9 | ▶ | do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 274 | (undef, my $digit) = $n->bdiv($radix); | |||||||||||||||||||||||||||||||
| 216 | 274 | push @ret, $digit; | |||||||||||||||||||||||||||||||
| 217 | } while ($n); | ||||||||||||||||||||||||||||||||
| 218 | 9 | ▶ | if ($radix < 1_000_000) { # plain scalars if fit | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | 9 | foreach (@ret) { | |||||||||||||||||||||||||||||||
| 220 | 274 | $_ = $_->numify; # mutate array | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | } else { | ||||||||||||||||||||||||||||||||
| 225 | 338752 | ▶ | do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 1424416 | my $digit = $n % $radix; | |||||||||||||||||||||||||||||||
| 227 | 1424416 | push @ret, $digit; | |||||||||||||||||||||||||||||||
| 228 | 1424416 | $n = int(($n - $digit) / $radix); | |||||||||||||||||||||||||||||||
| 229 | } while ($n > 0); | ||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | 338761 | return @ret; # array[0] low digit | |||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | # 2**32 on a 32-bit UV, or 2**64 on 64-bit | ||||||||||||||||||||||||||||||||
| 237 | 63 | ▶ | use constant 1.02 _UV_MAX_PLUS_1 => ((~0 >> 1) + 1) * 2.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | sub bit_split_lowtohigh { | ||||||||||||||||||||||||||||||||
| 240 | 19485 | ▶ | my ($n) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
bit_split_lowtohigh: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 241 | 19485 | my @ret; | |||||||||||||||||||||||||||||||
| 242 | 19485 | ▶ | if ($n >= 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | 19028 | ▶ | if (ref $n && $n->isa('Math::BigInt')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $n and $n->isa("Math::BigInt")
MC/DC100%2/2
ref $n and $n->isa("Math::BigInt")
ref $n
$n->isa("Math::BigInt")
Truth table100%3/3
A and B
A= ref $nB= $n->isa("Math::BigInt")
| |||||||||||||||||||||||||||||||||
| 244 | 10 | (my $str = $n->as_bin) =~ s/^0b//; # strip leading 0b | |||||||||||||||||||||||||||||||
| 245 | 10 | return reverse split //, $str; | |||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||
| 247 | 19018 | ▶ | if ($n <= _UV_MAX_PLUS_1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 248 | 19017 | return reverse split //, sprintf('%b',$n); | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | 257 | my $digit = $n % 2; | |||||||||||||||||||||||||||||||
| 252 | 257 | push @ret, $digit; | |||||||||||||||||||||||||||||||
| 253 | 257 | $n = int(($n - $digit) / 2); | |||||||||||||||||||||||||||||||
| 254 | } while ($n); | ||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||
| 256 | 458 | return @ret; # array[0] low digit | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 261 | # $aref->[0] low digit | ||||||||||||||||||||||||||||||||
| 262 | # ENHANCE-ME: BigInt new(), from_bin(), from_oct(), from_hex() | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | sub digit_join_lowtohigh { | ||||||||||||||||||||||||||||||||
| 265 | 263537 | ▶ | my ($aref, $radix, $zero) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
digit_join_lowtohigh: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | ### digit_join_lowtohigh() ... | ||||||||||||||||||||||||||||||||
| 268 | ### $aref | ||||||||||||||||||||||||||||||||
| 269 | ### $radix | ||||||||||||||||||||||||||||||||
| 270 | ### $zero | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 263537 | ▶ | my $n = (defined $zero ? $zero : 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 273 | 263537 | foreach my $digit (reverse @$aref) { # high to low | |||||||||||||||||||||||||||||||
| 274 | ### $n | ||||||||||||||||||||||||||||||||
| 275 | 974502 | $n *= $radix; | |||||||||||||||||||||||||||||||
| 276 | 974502 | $n += $digit; | |||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | ### $n | ||||||||||||||||||||||||||||||||
| 279 | 263537 | return $n; | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 63 | 1; | |||||||||||||||||||||||||||||||
| 284 | __END__ | ||||||||||||||||||||||||||||||||