blib/lib/Math/BaseCalc.pm

filter: Statementstmt 100% 65 / 65 Branchbran 92.3% 24 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 98.2% 112 / 114 CC 17 SCAR 16.9
CC
17
Cov
98%
from_base
8.0
digits
7.0
to_base
4.0
CRAP 5.4
↑ Summary Build »
1 package Math::BaseCalc;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 $VERSION = '1.014';
7  
8 sub new {
9 11 ▶ my ($pack, %opts) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
10 11 my $self = bless {}, $pack;
11 11 $self->{has_dash} = 0;
12 11 $self->digits($opts{digits});
13 11 return $self;
14 }
15  
16 sub digits {
17 23 ▶ my $self = shift;
Subroutine digits: called CC=7 SCAR=19.5
Pod documented
18 23 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 0 23
19 # Set the value
20  
21  
22 23 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 16 7 23
23 16 $self->{digits} = [ @{ shift() } ];
24 } else {
25 7 my $name = shift;
26 7 my %digitsets = $self->_digitsets;
27 7 ▶ croak "Unrecognized digit set '$name'" unless exists $digitsets{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $digitsets{$name} 0 7 7
28 7 $self->{digits} = $digitsets{$name};
29 }
30 23 foreach my $digit (@{$self->{digits}}) {
31 256 ▶ if ($digit eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($digit eq '-') { } 2 254 256
elsif ($digit eq '.') { } 1 253 254
32 2 $self->{has_dash} = 1;
33 } elsif ($digit eq '.') {
34 1 $self->{has_dot} = 1;
35 }
36 }
37  
38 23 $self->{trans} = {};
39 # Build the translation table back to numbers
40 23 @{$self->{trans}}{@{$self->{digits}}} = 0..$#{$self->{digits}};
41  
42 }
43 23 return @{$self->{digits}};
44 }
45  
46  
47 sub _digitsets {
48 return (
49 7 ▶ 'bin' => [0,1],
Subroutine _digitsets: called CC=1 SCAR=0.0
50 'hex' => [0..9,'a'..'f'],
51 'HEX' => [0..9,'A'..'F'],
52 'oct' => [0..7],
53 '64' => ['A'..'Z','a'..'z',0..9,'+','/'],
54 '62' => [0..9,'a'..'z','A'..'Z'],
55 );
56 }
57  
58 sub from_base {
59 67 ▶ my $self = shift;
Subroutine from_base: called CC=8 SCAR=20.8
Pod documented
60 67 ▶ return -1*$self->from_base(substr($_[0],1)) if !$self->{has_dash} && $_[0] =~ /^-/; # Handle negative numbers
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"has_dash"} and $_[0] =~ /^-/ 3 64 67
Condition100%3/3
not $self->{"has_dash"} and $_[0] =~ /^-/
!ll&&!rl&&r
12523
MC/DC100%2/2
not $self->{"has_dash"} and $_[0] =~ /^-/
not $self->{"has_dash"} $_[0] =~ /^-/
Truth table100%3/3
not(A) and B
A= not $self->{"has_dash"}B= $_[0] =~ /^-/
ABresult
0X0
100
111
61 64 my $str = shift;
62 64 my $dignum = @{$self->{digits}};
63  
64 # Deal with stuff after the decimal point
65 64 my $add_in = 0;
66 64 ▶ if (!$self->{has_dot} && $str =~ s/\.(.+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"has_dot"} and $str =~ s/\.(.+)//) 2 62 64
Condition100%3/3
not $self->{"has_dot"} and $str =~ s/\.(.+)//
!ll&&!rl&&r
11512
MC/DC100%2/2
not $self->{"has_dot"} and $str =~ s/\.(.+)//
not $self->{"has_dot"} $str =~ s/\.(.+)//
Truth table100%3/3
not(A) and B
A= not $self->{"has_dot"}B= $str =~ s/\.(.+)//
ABresult
0X0
100
111
67 2 $add_in = $self->from_base(reverse $1)/$dignum**length($1);
68 }
69  
70 64 $str = reverse $str;
71 64 my $result = 0;
72 64 my $trans = $self->{trans};
73 64 ▶ while (length $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 248 62 310
74 ## no critic
75 248 ▶ return undef unless exists $trans->{substr($str,0,1)};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$trans{substr($str, 0, 1)} 2 246 248
76 # For large numbers, force result to be an integer (not a float)
77 246 $result = int($result*$dignum + $trans->{chop $str});
78 }
79  
80 # The bizarre-looking next line is necessary for proper handling of very large numbers
81 62 ▶ return $add_in ? $result + $add_in : $result;
Branch: 1 branch
BranchTrueFalseTotal
$add_in ? : 2 60 62
82 }
83  
84 sub to_base {
85 57 ▶ my ($self,$num) = @_;
Subroutine to_base: called CC=4 SCAR=13.9
Pod documented
86 57 ▶ return '-'.$self->to_base(-1*$num) if $num<0; # Handle negative numbers
Branch: 1 branch
BranchTrueFalseTotal
if $num < 0 1 56 57
87  
88 56 my $dignum = @{$self->{digits}};
89  
90 56 my $result = '';
91 56 ▶ while ($num>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 204 56 260
92 204 substr($result,0,0) = $self->{digits}[ $num % $dignum ];
93 6 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 204 $num /= $dignum;
95 #$num = (($num - ($num % $dignum))/$dignum); # An alternative to the above
96 }
97 56 ▶ return length $result ? $result : $self->{digits}[0];
Branch: 1 branch
BranchTrueFalseTotal
length $result ? : 52 4 56
98 }
99  
100  
101 6 1;
102 __END__
↑ Summary Build »