| 1 | package Math::BaseArith; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our($debug) = 0; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 16 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 17 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # This allows declaration use Math::BaseArith ':all'; | ||||||||||||||||||||||||||||
| 20 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 21 | # will save memory. | ||||||||||||||||||||||||||||
| 22 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 23 | encode | ||||||||||||||||||||||||||||
| 24 | decode | ||||||||||||||||||||||||||||
| 25 | $Math::BaseArith::debug | ||||||||||||||||||||||||||||
| 26 | ) ] ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 31 | encode | ||||||||||||||||||||||||||||
| 32 | decode | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 1 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ####################################################################### | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub encode { | ||||||||||||||||||||||||||||
| 39 | 16 | ▶ | croak "Function called in void context" unless defined wantarray; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=14 SCAR=26.8
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 16 | my $value = shift; # value to be encoded | |||||||||||||||||||||||||||
| 42 | 16 | my $b_listRef = shift; # list of base values | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 16 | my @b_list = @$b_listRef; # copy the base value list | |||||||||||||||||||||||||||
| 45 | 16 | my @r_list; | |||||||||||||||||||||||||||
| 46 | 16 | my @radix_list = (1); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 16 | ▶ | print STDERR "encode($value ,[@$b_listRef])" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | if $Math::BaseArith::debug >= 1; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 16 | my $r = 0; | |||||||||||||||||||||||||||
| 52 | 16 | my $b = 1; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Compute the radix divisors from the base list, and put in reverse order | ||||||||||||||||||||||||||||
| 55 | # [1760,3,12] miles/yards/feet/inches becomes [63360,5280,1760] | ||||||||||||||||||||||||||||
| 56 | # [2,2,2,2] becomes [16,8,4,2] | ||||||||||||||||||||||||||||
| 57 | 16 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 43 | $b *= pop @b_list; | |||||||||||||||||||||||||||
| 59 | 43 | unshift @radix_list, $b; | |||||||||||||||||||||||||||
| 60 | } while @b_list; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 16 | my $i = 0; | |||||||||||||||||||||||||||
| 63 | 16 | foreach $b (@radix_list) { | |||||||||||||||||||||||||||
| 64 | 59 | $i++; | |||||||||||||||||||||||||||
| 65 | 59 | ▶ | if ($b > $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | printf STDERR "%10d%10d%10d%10d\n", $b,$value,$r,$value%$b | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | if $Math::BaseArith::debug >= 2; | ||||||||||||||||||||||||||||
| 68 | 16 | ▶ | push @r_list, 0 if $i > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 16 | next; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 43 | ▶ | my $r = $b ? int($value/$b) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 43 | ▶ | printf STDERR "%10d%10d%10d%10d\n", $b,$value,$r,$b?$value%$b:0 if $Math::BaseArith::debug >= 2; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | 43 | push @r_list, $r; | |||||||||||||||||||||||||||
| 74 | 43 | ▶ | $value %= $b if $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 16 | ▶ | shift @r_list while (scalar(@r_list) > scalar(@$b_listRef)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 16 | ▶ | return wantarray ? @r_list : \@r_list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ####################################################################### | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub decode { | ||||||||||||||||||||||||||||
| 85 | 10 | ▶ | my $r_listRef = shift; # list of representation values | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 10 | my $b_listRef = shift; # list of base values | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 10 | ▶ | print STDERR "decode([@$r_listRef],[@$b_listRef])" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | if $Math::BaseArith::debug >= 1; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 10 | ▶ | if ( scalar(@$r_listRef) > scalar(@$b_listRef) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @$r_listRef > scalar @$b_listRef and scalar @$b_listRef != 1
MC/DC100%2/2
scalar @$r_listRef > scalar @$b_listRef and scalar @$b_listRef != 1
scalar @$r_listRef > scalar @$b_listRef
scalar @$b_listRef != 1
Truth table100%3/3
A and B
A= scalar @$r_listRef > scalar @$b_listRefB= scalar @$b_listRef != 1
| |||||||||||||||||||||||||||||
| 92 | scalar(@$b_listRef) != 1 ) | ||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 1 | carp "length error"; | |||||||||||||||||||||||||||
| 95 | 1 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 9 | my $value = 0; | |||||||||||||||||||||||||||
| 99 | 9 | my $b = 1; | |||||||||||||||||||||||||||
| 100 | 9 | my $base = 1; | |||||||||||||||||||||||||||
| 101 | 9 | my $r; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 9 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 31 | $r = pop @$r_listRef; | |||||||||||||||||||||||||||
| 105 | 31 | $value += $r * $base; | |||||||||||||||||||||||||||
| 106 | 31 | ▶ | printf STDERR "%10d%10d%10d%10d\n", $r,$b,$base,$value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | if $Math::BaseArith::debug >= 2; | ||||||||||||||||||||||||||||
| 108 | 31 | ▶ | $b = pop @$b_listRef || $b; | ||||||||||||||||||||||||||
|
Condition66%2/3
pop @$b_listRef || $b
MC/DC0%0/2
pop @$b_listRef || $b
pop @$b_listRef
$b
Truth table66%2/3
A || B
A= pop @$b_listRefB= $b
| |||||||||||||||||||||||||||||
| 109 | 31 | $base *= $b; | |||||||||||||||||||||||||||
| 110 | } while @$r_listRef; | ||||||||||||||||||||||||||||
| 111 | 9 | $value; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||