| 1 | package Math::Fleximal; | ||||||||||||||||||||||||||||
| 2 | 1 | $VERSION = 0.06; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub abs { | ||||||||||||||||||||||||||
|
Subroutine
abs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 8 | 0 | my $self = (shift)->dup(); | |||||||||||||||||||||||||||
| 9 | 0 | $self->{sign} = 1; | |||||||||||||||||||||||||||
| 10 | 0 | return $self; | |||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Only do with positive result! (Else normalize bombs.) | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub abs_sub_from { | ||||||||||||||||||||||||||
|
Subroutine
abs_sub_from: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | my $values = (shift)->{values}; | |||||||||||||||||||||||||||
| 16 | 0 | my $decs = (shift)->{values}; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | foreach my $i (0..$#$decs) { | |||||||||||||||||||||||||||
| 19 | 0 | $values->[$i] -= $decs->[$i]; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub abs_add_to { | ||||||||||||||||||||||||||
|
Subroutine
abs_add_to: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $values = (shift)->{values}; | |||||||||||||||||||||||||||
| 25 | 0 | my $incs = (shift)->{values}; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | foreach my $i (0..$#$incs) { | |||||||||||||||||||||||||||
| 28 | 0 | $values->[$i] += $incs->[$i]; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | my $sum = $self->dup(); | |||||||||||||||||||||||||||
| 35 | 0 | foreach (@_) { | |||||||||||||||||||||||||||
| 36 | 0 | $sum = $sum->plus($_); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | return $sum; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub array2hash { | ||||||||||||||||||||||||||
|
Subroutine
array2hash: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 0 | my %pos; | |||||||||||||||||||||||||||
| 43 | 0 | $pos{$_[$_]} = $_ foreach 0..$#_; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | return wantarray ? %pos : \%pos; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub base_10 { | ||||||||||||||||||||||||||
|
Subroutine
base_10: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 0 | my $proto = __PACKAGE__->new(0); | |||||||||||||||||||||||||||
| 50 | 0 | return $proto->dup($self)->to_str(); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub change_flex { | ||||||||||||||||||||||||||
|
Subroutine
change_flex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 0 | my $new_flex = shift; | |||||||||||||||||||||||||||
| 56 | 0 | my $proto = __PACKAGE__->new($new_flex->[0], $new_flex); | |||||||||||||||||||||||||||
| 57 | 0 | $proto->dup($self); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub cmp { | ||||||||||||||||||||||||||
|
Subroutine
cmp: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | my $other = $self->dup(shift); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($self->{sign} != $other->{sign}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | return $self->{sign}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | return ( | ||||||||||||||||||||||||||||
| 68 | cmp_vec($self->{values}, $other->{values}) | ||||||||||||||||||||||||||||
| 69 | * $self->{sign} | ||||||||||||||||||||||||||||
| 70 | 0 | ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub cmp_vec { | ||||||||||||||||||||||||||
|
Subroutine
cmp_vec: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $first = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $second = shift; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $cmp = @$first <=> @$second; | |||||||||||||||||||||||||||
| 79 | 0 | my $i = @$first; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | while ($i and not $cmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i and not $cmp
MC/DC0%0/2
$i and not $cmp
$i
not $cmp
Truth table0%0/3
A and not(B)
A= $iB= not $cmp
| |||||||||||||||||||||||||||||
| 82 | 0 | $i--; | |||||||||||||||||||||||||||
| 83 | 0 | $cmp = $first->[$i] <=> $second->[$i]; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return $cmp; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub div { | ||||||||||||||||||||||||||
|
Subroutine
div: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 0 | my @remain; | |||||||||||||||||||||||||||
| 92 | 0 | foreach (@_) { | |||||||||||||||||||||||||||
| 93 | 0 | ($self, my $rem) = $self->divide($_); | |||||||||||||||||||||||||||
| 94 | 0 | push @remain, $rem; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | wantarray ? ($self, @remain) : $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub divide { | ||||||||||||||||||||||||||
|
Subroutine
divide: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 0 | my $denom = $self->dup(shift); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | unless (@{$denom->{values}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | croak("Cannot divide by zero!"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Base 2 is convenient... | ||||||||||||||||||||||||||||
| 108 | 0 | my @doubles = $denom->abs(); | |||||||||||||||||||||||||||
| 109 | 0 | my $remain = $self->abs(); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | while ($doubles[-1]->cmp($remain) < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | push @doubles, $doubles[-1]->plus($doubles[-1]); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $ans = ''; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | while (@doubles) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | my $double = pop (@doubles); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($double->cmp($remain) <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $ans .= "1"; | |||||||||||||||||||||||||||
| 119 | 0 | $remain = $remain->minus($double); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||
| 122 | 0 | $ans .= "0"; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Convert answer | ||||||||||||||||||||||||||||
| 127 | 0 | $ans = __PACKAGE__->new($ans, [0, 1]); | |||||||||||||||||||||||||||
| 128 | # Handle differing sign without exact division... | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$remain->cmp(0) and $self->{"sign"} == -1
MC/DC0%0/2
$remain->cmp(0) and $self->{"sign"} == -1
$remain->cmp(0)
$self->{"sign"} == -1
Truth table0%0/3
A and B
A= $remain->cmp(0)B= $self->{"sign"} == -1
| |||||||||||||||||||||||||||||
| 130 | $remain->cmp(0) and | ||||||||||||||||||||||||||||
| 131 | $self->{sign} == -1 | ||||||||||||||||||||||||||||
| 132 | ) { | ||||||||||||||||||||||||||||
| 133 | 0 | $remain = $denom->abs->minus($remain); | |||||||||||||||||||||||||||
| 134 | 0 | $ans = $ans->plus(1); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | $ans->{sign} = $self->{sign} * $denom->{sign}; | |||||||||||||||||||||||||||
| 137 | 0 | return ($self->dup($ans), $remain) | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub dup { | ||||||||||||||||||||||||||
|
Subroutine
dup: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 0 | my $copy = bless +{ %$self }, ref($self); | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $val = @_ ? shift : $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | return $copy->set_value($val); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub gcd { | ||||||||||||||||||||||||||
|
Subroutine
gcd: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | 0 | my $zero = $self->zero(); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | foreach (@_) { | |||||||||||||||||||||||||||
| 152 | 0 | my $other = $self->dup($_); | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | while ($other->cmp($zero)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ($self, $other) = ($other, $self->mod($other)); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | return $self; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub make_mybase { | ||||||||||||||||||||||||||
|
Subroutine
make_mybase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 0 | return map $self->dup($_), @_; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub minus { | ||||||||||||||||||||||||||
|
Subroutine
minus: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 0 | my $other = $self->dup(shift); | |||||||||||||||||||||||||||
| 169 | 0 | $other->{sign} = - $other->{sign}; | |||||||||||||||||||||||||||
| 170 | 0 | return $self->add($other); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub mod { | ||||||||||||||||||||||||||
|
Subroutine
mod: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 0 | my @remain = div(@_); | |||||||||||||||||||||||||||
| 175 | 0 | shift @remain; | |||||||||||||||||||||||||||
| 176 | 0 | ▶ | wantarray ? @remain : $remain[-1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub mul { | ||||||||||||||||||||||||||
|
Subroutine
mul: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 0 | my $prod = (shift)->dup(); | |||||||||||||||||||||||||||
| 181 | 0 | foreach (@_) { | |||||||||||||||||||||||||||
| 182 | 0 | $prod = $prod->times($_); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | return $prod; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | my %default = ( | |||||||||||||||||||||||||||
| 189 | '+' => '+', | ||||||||||||||||||||||||||||
| 190 | '-' => '-', | ||||||||||||||||||||||||||||
| 191 | 'show_+' => 0, | ||||||||||||||||||||||||||||
| 192 | strip => qr/[\s\.,_]/, | ||||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | my $self = bless {sign => 1, value => [], %default}, shift; | |||||||||||||||||||||||||||
| 196 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | my $flex = $self->{flex} = shift || [0..9]; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || [0..9]
MC/DC0%0/1
shift() || [0..9]
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | $self->{base} = @$flex; | |||||||||||||||||||||||||||
| 201 | 0 | $self->{match_fleck} = ret_match_any(@$flex); | |||||||||||||||||||||||||||
| 202 | 0 | $self->{fleck_lookup} = array2hash(@$flex); | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | foreach my $key (keys %$args) { | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if (exists $default{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | $self->{$key} = $args->{$key}; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | else { | ||||||||||||||||||||||||||||
| 209 | 0 | my $valid = join ", ", map "'$_'", sort keys %default; | |||||||||||||||||||||||||||
| 210 | 0 | croak("Unknown parameter '$valid'. Allowed: ($valid)"); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | return $self->set_value($value); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # values assumed to work out nonnegative | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub normalize { | ||||||||||||||||||||||||||
|
Subroutine
normalize: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 219 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 220 | 0 | my $base = $self->{base}; | |||||||||||||||||||||||||||
| 221 | 0 | my $values = $self->{values}; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # We need to have a valid base rep | ||||||||||||||||||||||||||||
| 224 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 225 | 0 | my $carry = 0; | |||||||||||||||||||||||||||
| 226 | 0 | ▶ | while ($carry or $i < @$values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$carry or $i < @$values
MC/DC0%0/2
$carry or $i < @$values
$carry
$i < @$values
Truth table0%0/3
A or B
A= $carryB= $i < @$values
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | $carry += $values->[$i] if ($values->[$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | while ($carry < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | $carry += $base; | |||||||||||||||||||||||||||
| 230 | 0 | $values->[$i + 1]--; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 0 | $values->[$i] = $carry % $base; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | $carry /= $base; | |||||||||||||||||||||||||||
| 235 | 0 | ++$i; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Deal with leading 0's and 0... | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | pop(@$values) while @$values and not $values->[-1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$values and not $values->[-1]
MC/DC0%0/2
@$values and not $values->[-1]
@$values
not $values->[-1]
Truth table0%0/3
A and not(B)
A= @$valuesB= not $values->[-1]
| |||||||||||||||||||||||||||||
| 240 | 0 | ▶ | $self->{sign} = 1 if not @$values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 0 | return $self; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub one { | ||||||||||||||||||||||||||
|
Subroutine
one: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | 0 | my $num = (shift)->dup(); | |||||||||||||||||||||||||||
| 246 | 0 | $num->{sign} = 1; | |||||||||||||||||||||||||||
| 247 | 0 | $num->{values} = [1]; | |||||||||||||||||||||||||||
| 248 | 0 | return $num; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub parse_rep { | ||||||||||||||||||||||||||
|
Subroutine
parse_rep: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 252 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 253 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | $str =~ s/$self->{strip}//g; | |||||||||||||||||||||||||||
| 256 | 0 | my $sign = 1; | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ($str =~ /^\Q$self->{"-"}\E/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | $sign = -1; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | else { | ||||||||||||||||||||||||||||
| 261 | 0 | $str =~ /^\Q$self->{"+"}\E/g; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my @values; | |||||||||||||||||||||||||||
| 265 | 0 | my $match_fleck = $self->{match_fleck}; | |||||||||||||||||||||||||||
| 266 | 0 | my $fleck_lookup = $self->{fleck_lookup}; | |||||||||||||||||||||||||||
| 267 | 0 | my $last_pos = pos($str); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | ▶ | while ($str =~ /\G($match_fleck)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | push @values, $fleck_lookup->{$1}; | |||||||||||||||||||||||||||
| 271 | 0 | $last_pos = pos($str); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | croak( | ||||||||||||||||||||||||||||
| 275 | 0 | ▶ | "Cannot find any digits in $str.\n" . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 0 | "Current flex: (@{$self->{flex}})\n" | |||||||||||||||||||||||||||
| 277 | ) unless @values; | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 0 | ▶ | carp("'$str' truncated in parse") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | unless $last_pos == length($str); | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | return ($sign, [reverse @values]); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | sub plus { | ||||||||||||||||||||||||||
|
Subroutine
plus: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 286 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 287 | 0 | my $other = $self->dup(shift); | |||||||||||||||||||||||||||
| 288 | 0 | my $sum; | |||||||||||||||||||||||||||
| 289 | 0 | ▶ | if ($self->{sign} == $other->{sign}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 290 | 0 | $sum = $self->dup(); | |||||||||||||||||||||||||||
| 291 | 0 | abs_add_to($sum, $other); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | elsif (0 < cmp_vec($self->{values}, $other->{values})) { | ||||||||||||||||||||||||||||
| 294 | 0 | $sum = $self->dup(); | |||||||||||||||||||||||||||
| 295 | 0 | $sum->abs_sub_from($other); | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | else { | ||||||||||||||||||||||||||||
| 298 | 0 | $sum = $other->dup(); | |||||||||||||||||||||||||||
| 299 | 0 | $sum->abs_sub_from($self); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | 0 | return $sum->normalize(); | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | sub pow { | ||||||||||||||||||||||||||
|
Subroutine
pow: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 305 | 0 | my $cur_base = shift; | |||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||
| 307 | 0 | my $exp = $cur_base->dup(shift); | |||||||||||||||||||||||||||
| 308 | 0 | ▶ | unless (1 == $exp->{sign}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 0 | $exp = $exp->to_str(); | |||||||||||||||||||||||||||
| 310 | 0 | croak("Cannot handle negative exponent: '$exp'"); | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | my $res = $cur_base->one(); | |||||||||||||||||||||||||||
| 314 | # Base 2 is easier | ||||||||||||||||||||||||||||
| 315 | 0 | $exp = $exp->change_flex([0, 1]); | |||||||||||||||||||||||||||
| 316 | 0 | foreach my $term (@{$exp->{values}}) { | |||||||||||||||||||||||||||
| 317 | 0 | ▶ | if ($term) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | $res = $res->times($cur_base); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 0 | $cur_base = $cur_base->times($cur_base); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 0 | $cur_base = $res; | |||||||||||||||||||||||||||
| 324 | 0 | redo; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | else { | ||||||||||||||||||||||||||||
| 327 | 0 | return $res; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub ret_match_any { | ||||||||||||||||||||||||||
|
Subroutine
ret_match_any: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 333 | # Hack to match longest token possible | ||||||||||||||||||||||||||||
| 334 | 0 | my @toks = reverse sort @_; | |||||||||||||||||||||||||||
| 335 | 0 | my $str = join "|", map quotemeta($_), @_; | |||||||||||||||||||||||||||
| 336 | 0 | return qr/$str/; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub set_value { | ||||||||||||||||||||||||||
|
Subroutine
set_value: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 341 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 342 | 0 | ▶ | if (ref($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | if ($self->{base} == $value->{base}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 0 | $self->{values} = [ @{ $value->{values} } ]; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | else { | ||||||||||||||||||||||||||||
| 347 | 0 | my $factor = $value->{base}; | |||||||||||||||||||||||||||
| 348 | 0 | my $converted = $self->zero(); | |||||||||||||||||||||||||||
| 349 | 0 | my $scale = $self->one(); | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | foreach (@{ $value->{values} }) { | |||||||||||||||||||||||||||
| 352 | 0 | $converted = $converted->plus($scale->times_const($_)); | |||||||||||||||||||||||||||
| 353 | 0 | $scale = $scale->times_const($factor); | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 0 | $self->{values} = $converted->{values}; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 0 | $self->{sign} = $value->{sign}; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | else { | ||||||||||||||||||||||||||||
| 360 | 0 | @$self{'sign', 'values'} = $self->parse_rep($value); | |||||||||||||||||||||||||||
| 361 | 0 | $self->normalize(); | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | 0 | return $self; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | sub subtr { | ||||||||||||||||||||||||||
|
Subroutine
subtr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 367 | 0 | my $result = (shift)->dup(); | |||||||||||||||||||||||||||
| 368 | 0 | $result = $result->minus($_) foreach @_; | |||||||||||||||||||||||||||
| 369 | 0 | return $result; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub times { | ||||||||||||||||||||||||||
|
Subroutine
times: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 373 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 374 | 0 | my $other = $self->dup(shift); | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | my $result = $self->zero(); | |||||||||||||||||||||||||||
| 377 | 0 | my @leading_zeros = (); | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # Prevents possible sign bug on 0 | ||||||||||||||||||||||||||||
| 380 | 0 | ▶ | unless (@{$self->{values}} and @{$other->{values}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@{$self->{"values"};} and @{$other->{"values"};}
MC/DC0%0/2
@{$self->{"values"};} and @{$other->{"values"};}
@{$self->{"values"};}
@{$other->{"values"};}
Truth table0%0/3
A and B
A= @{$self->{"values"};}B= @{$other->{"values"};}
| |||||||||||||||||||||||||||||
| 381 | 0 | return $result; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | foreach (@{ $other->{values} }) { | |||||||||||||||||||||||||||
| 385 | 0 | my $tmp = $self->times_const($_); | |||||||||||||||||||||||||||
| 386 | 0 | unshift @{$tmp->{values}}, @leading_zeros; | |||||||||||||||||||||||||||
| 387 | 0 | $result = $result->plus($tmp); | |||||||||||||||||||||||||||
| 388 | 0 | push @leading_zeros, 0; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | $result->{sign} = $self->{sign} * $other->{sign}; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | $result; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | ▶ | sub times_const { | ||||||||||||||||||||||||||
|
Subroutine
times_const: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 397 | 0 | my $result = (shift)->dup(); | |||||||||||||||||||||||||||
| 398 | 0 | my $const = shift; | |||||||||||||||||||||||||||
| 399 | 0 | ▶ | if ($const < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 0 | $const *= -1; | |||||||||||||||||||||||||||
| 401 | 0 | $result->{sign} = - $result->{sign}; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | 0 | foreach my $term (@{$result->{values}}) { | |||||||||||||||||||||||||||
| 404 | 0 | $term *= $const; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 0 | $result->normalize(); | |||||||||||||||||||||||||||
| 407 | 0 | return $result; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | sub to_str { | ||||||||||||||||||||||||||
|
Subroutine
to_str: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 412 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 413 | 0 | my $flex = $self->{flex}; | |||||||||||||||||||||||||||
| 414 | 0 | my @vals = @{$self->{values}}; | |||||||||||||||||||||||||||
| 415 | 0 | ▶ | push @vals, 0 unless @vals; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | 0 | ▶ | my $p = $self->{'show_+'} ? $self->{'+'} : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | return join "", | ||||||||||||||||||||||||||||
| 418 | 0 | ▶ | (1 == $self->{sign} ? $p : $self->{'-'}), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | map $flex->[$_], reverse @vals; | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 0 | ▶ | sub zero { | ||||||||||||||||||||||||||
|
Subroutine
zero: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 423 | 0 | my $num = (shift)->dup(); | |||||||||||||||||||||||||||
| 424 | 0 | $num->{sign} = 1; | |||||||||||||||||||||||||||
| 425 | 0 | $num->{values} = []; | |||||||||||||||||||||||||||
| 426 | 0 | return $num; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 1 | 1; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | __END__ | ||||||||||||||||||||||||||||