blib/lib/Math/FractionManip.pm

filter: Statementstmt 80.8% 430 / 532 Branchbran 61.9% 166 / 268 Conditioncond 55.2% 58 / 105 MC/DCmcdc 31.5% 18 / 57 Subroutinesub 77.5% 38 / 49 Podpod 100% 32 / 32 totaltotal 71.1% 742 / 1043 CC 177 SCAR 26.1
CC
177
Cov
72%
tags
38.6
is_tag
28.3
new
22.6
CRAP 13.5
↑ Summary
1 package Math::FractionManip;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Manipulate fractions
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use warnings;
8  
9 2 our $VERSION = '0.5504';
10  
11 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Math::BigFloat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 use overload
15 2 ▶ '+' => 'add',
Subroutine BEGIN: called CC=1 SCAR=0.0
16 '-' => 'sub',
17 '*' => 'mul',
18 '/' => 'div',
19 'abs' => 'abs',
20 '**' => 'pow',
21 'sqrt' => 'sqrt',
22 '<=>' => 'cmp',
23 '""' => 'string',
24 '0+' => 'decimal',
25 2 fallback => 1;
26  
27 2 my %DEF = (
28 CURRENT => { TAGS => [qw/ NORMAL REDUCE SMALL AUTO /], DIGITS => undef, SYSTEM => 1, NAME => 'DEFAULT' },
29 DEFAULT => { TAGS => [qw/ NORMAL REDUCE SMALL AUTO /], DIGITS => undef, READONLY => 1, SYSTEM => 1 },
30 BLANK => { TAGS => ['', '', ''], DIGITS => '', READONLY => 1, SYSTEM => 1 },
31 );
32  
33 2 my ($OUTFORMAT, $REDUCE, $SIZE, $AUTO, $INTERNAL, $RED_STATE) = (0 .. 5);
34 2 my $TAG_END = 3; #Last index of tags meant to be kept.
35  
36 2 my %TAGS = (
37 NORMAL => [$OUTFORMAT, 'NORMAL'],
38 MIXED => [$OUTFORMAT, 'MIXED'],
39 MIXED_RAW => [$OUTFORMAT, 'MIXED_RAW'],
40 RAW => [$OUTFORMAT, 'RAW'],
41 DEF_MIXED => [$OUTFORMAT, undef],
42 REDUCE => [$REDUCE, 'REDUCE'],
43 NO_REDUCE => [$REDUCE, 'NO_REDUCE'],
44 DEF_REDUCE => [$REDUCE, undef],
45 SMALL => [$SIZE, 'SMALL'],
46 BIG => [$SIZE, 'BIG'],
47 DEF_BIG => [$SIZE, undef],
48 AUTO => [$AUTO, 'AUTO'],
49 NO_AUTO => [$AUTO, 'NO_AUTO'],
50 DEF_AUTO => [$AUTO, undef],
51 CONVERTED => [$INTERNAL, 'CONVERTED'],
52 IS_REDUCED => [$RED_STATE, 'IS_REDUCED'],
53 );
54  
55 2 my @DEF_TAG = qw(DEF_MIXED DEF_REDUCE DEF_BIG DEF_AUTO);
56  
57 2 my $ID = 01;
58  
59 64 ▶sub new {
Subroutine new: called CC=22 SCAR=31.2
Pod documented
60 64 my $proto = shift;
61 64 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0640
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
62 64 my ($self, @frac, @tags, $tag, $decimal, $p1, $p2, $p3);
63 64 ▶ if (_is_decimal($_[0]) and _is_decimal($_[1]) and _is_decimal($_[2])) {
Branch: 5 branches
BranchTrueFalseTotal
if (_is_decimal($_[0]) and _is_decimal($_[1]) and _is_decimal($_[2])) { } 2 62 64
elsif (_is_decimal($_[0]) and _is_decimal($_[1])) { } 50 12 62
elsif (_is_decimal($_[0])) { } 5 7 12
elsif ($_[0] =~ m[\s*([\+\-]?)\s*([0-9e\.\+\-]+)\s+([0-9e\.\+\-]+)\s*/\s*([0-9e\.\+\-]+)]) { } 2 5 7
elsif ($_[0] =~ m[\s*([0-9e\.\+\-]+)\s*/\s*([0-9e\.\+\-]+)]) { } 5 0 5
Condition100%6/6
_is_decimal($_[0]) and _is_decimal($_[1]) and _is_decimal($_[2])
!ll&&!rl&&r
12502
_is_decimal($_[0]) and _is_decimal($_[1])
!ll&&!rl&&r
7552
MC/DC100%3/3
_is_decimal($_[0]) and _is_decimal($_[1]) and _is_decimal($_[2])
_is_decimal($_[0]) _is_decimal($_[1]) _is_decimal($_[2])
Truth table100%4/4
A and B and C
A= _is_decimal($_[0])B= _is_decimal($_[1])C= _is_decimal($_[2])
ABCresult
0XX0
10X0
1100
1111
64 2 my $sign = $_[0] / abs($_[0]);
65 2 @tags = _tags(@_[3 .. $#_]);
66 2 ($decimal, $p1, $p2, $p3) = _fix_num(\@tags, @_[0 .. 2]);
67 2 ($p1, $p2, $p3) = (abs($p1), abs($p2), abs($p3));
68 2 @frac = ($p1 * $p3 + $p2, $sign * $p3);
69 2 ▶ @frac = _de_decimal(@frac, \@tags) if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 0 2 2
70 }
71 ▶ elsif (_is_decimal($_[0]) and _is_decimal($_[1])) {
Condition100%3/3
_is_decimal($_[0]) and _is_decimal($_[1])
!ll&&!rl&&r
7550
MC/DC100%2/2
_is_decimal($_[0]) and _is_decimal($_[1])
_is_decimal($_[0]) _is_decimal($_[1])
Truth table100%3/3
A and B
A= _is_decimal($_[0])B= _is_decimal($_[1])
ABresult
0X0
100
111
72 50 @tags = _tags(@_[2 .. $#_]);
73 50 ($decimal, @frac) = _fix_num(\@tags, @_[0 .. 1]);
74 50 ▶ @frac = _de_decimal(@frac, \@tags) if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 3 47 50
75 50 @frac = _simplify_sign(@frac);
76 }
77 elsif (_is_decimal($_[0])) {
78 {
79 5 @tags = _tags(@_[1 .. $#_]);
80 5 ($decimal, $p1) = _fix_num(\@tags, $_[0]);
81 5 ▶ @frac = ($p1, 1), last if not $decimal;
Branch: 1 branch
BranchTrueFalseTotal
unless $decimal 2 3 5
82 3 (@frac[0 .. 1], $tag) = _from_decimal($p1);
83 3 @tags = _tags(@tags, $tag);
84 3 ($decimal, @frac) = _fix_num(\@tags, @frac);
85 3 ▶ @frac = _de_decimal(@frac, \@tags) if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 0 3 3
86 }
87 }
88 elsif ($_[0] =~ /\s*([\+\-]?)\s*([0-9e\.\+\-]+)\s+([0-9e\.\+\-]+)\s*\/\s*([0-9e\.\+\-]+)/) {
89 2 my $sign = $1 . '1';
90 2 @tags = _tags(@_[1 .. $#_]);
91 2 ($decimal, $p1, $p2, $p3) = _fix_num(\@tags, $2, $3, $4);
92 2 ($p1, $p2, $p3) = (abs($p1), abs($p2), abs($p3));
93 2 @frac = ($p1 * $p3 + $p2, $sign * $p3);
94 2 ▶ @frac = _de_decimal($p1 * $p3 + $p2, $sign * $p3, \@tags) if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 0 2 2
95 }
96 elsif ($_[0] =~ /\s*([0-9e\.\+\-]+)\s*\/\s*([0-9e\.\+\-]+)/) {
97 5 @tags = _tags(@_[1 .. $#_]);
98 5 ($decimal, @frac) = _fix_num(\@tags, $1, $2);
99 5 ▶ @frac = _de_decimal(@frac, \@tags) if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 0 5 5
100 5 @frac = _simplify_sign(@frac);
101 }
102 else {
103 0 croak("\"$_[0]\" is of unknown format");
104 }
105 64 ▶ croak("Can not have 0 as the denominator") if $frac[1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frac[1] == 0 0 64 64
106 64 ▶ if (_tag($REDUCE, \@tags) ne 'NO_REDUCE' and _tag($RED_STATE, \@tags) ne 'IS_REDUCED') {
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($REDUCE, \@tags) ne "NO_REDUCE" and _tag($RED_STATE, \@tags) ne "IS_REDUCED") 33 31 64
Condition100%3/3
_tag($REDUCE, \@tags) ne "NO_REDUCE" and _tag($RED_STATE, \@tags) ne "IS_REDUCED"
!ll&&!rl&&r
121933
MC/DC100%2/2
_tag($REDUCE, \@tags) ne "NO_REDUCE" and _tag($RED_STATE, \@tags) ne "IS_REDUCED"
_tag($REDUCE, \@tags) ne "NO_REDUCE" _tag($RED_STATE, \@tags) ne "IS_REDUCED"
Truth table100%3/3
A and B
A= _tag($REDUCE, \@tags) ne "NO_REDUCE"B= _tag($RED_STATE, \@tags) ne "IS_REDUCED"
ABresult
0X0
100
111
107 33 my $not_reduced;
108 33 ($not_reduced, @frac) = _reduce(@frac);
109 33 ▶ @frac = _fix_auto('DOWN', \@tags, @frac) if $not_reduced and _tag($AUTO, \@tags) eq 'AUTO';
Branch: 1 branch
BranchTrueFalseTotal
if $not_reduced and _tag($AUTO, \@tags) eq "AUTO" 6 27 33
Condition66%2/3
$not_reduced and _tag($AUTO, \@tags) eq "AUTO"
!ll&&!rl&&r
2706
MC/DC50%1/2
$not_reduced and _tag($AUTO, \@tags) eq "AUTO"
$not_reduced _tag($AUTO, \@tags) eq "AUTO"
Truth table66%2/3
A and B
A= $not_reducedB= _tag($AUTO, \@tags) eq "AUTO"
ABresult
0X0
100
111
110 }
111 64 ▶ @tags[$RED_STATE] = undef if _tag($RED_STATE, \@tags) eq 'IS_REDUCED';
Branch: 1 branch
BranchTrueFalseTotal
if _tag($RED_STATE, \@tags) eq "IS_REDUCED" 19 45 64
112 64 $self->{frac} = \@frac;
113 64 $self->{tags} = \@tags;
114 64 bless($self, $class);
115 64 return $self;
116 }
117  
118 40 ▶sub string {
Subroutine string: called CC=10 SCAR=24.5
Pod documented
119 40 my $self = shift;
120 40 my @frac;
121 40 my $mixed = _tag($OUTFORMAT, [$_[0]], $self->{tags});
122 40 ▶ if ($mixed eq 'MIXED') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mixed eq 'MIXED') { } 14 26 40
elsif ($mixed eq 'MIXED_RAW') { } 0 26 26
elsif ($mixed eq 'RAW') { } 0 26 26
123 14 @frac = $self->list('MIXED');
124 14 my $string = "";
125 14 ▶ $string .= "$frac[0]" if $frac[0] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frac[0] != 0 7 7 14
126 14 ▶ $string .= " " if $frac[0] != 0 and $frac[1] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frac[0] != 0 and $frac[1] != 0 6 8 14
Condition100%3/3
$frac[0] != 0 and $frac[1] != 0
!ll&&!rl&&r
716
MC/DC100%2/2
$frac[0] != 0 and $frac[1] != 0
$frac[0] != 0 $frac[1] != 0
Truth table100%3/3
A and B
A= $frac[0] != 0B= $frac[1] != 0
ABresult
0X0
100
111
127 14 ▶ $string .= "$frac[1]/$frac[2]" if $frac[1] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frac[1] != 0 13 1 14
128 14 ▶ $string = "0" if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 0 14 14
129 14 return $string;
130 }
131 elsif ($mixed eq 'MIXED_RAW') {
132 0 @frac = $self->list('MIXED');
133 0 return "$frac[0] $frac[1]/$frac[2]";
134 }
135 elsif ($mixed eq 'RAW') {
136 0 @frac = $self->list;
137 0 ▶ return ($frac[0] >= 0 ? '+' : '') . "$frac[0]/$frac[1]";
Branch: 1 branch
BranchTrueFalseTotal
$frac[0] >= 0 ? : 0 0 0
138 }
139 else {
140 26 @frac = $self->list;
141 26 return "$frac[0]/$frac[1]";
142 }
143 }
144  
145 40 ▶sub list {
Subroutine list: called CC=4 SCAR=13.9
Pod documented
146 40 my $self = shift;
147 40 my @frac = @{ $self->{frac} };
148 40 ▶ if ($_[0] eq "MIXED") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "MIXED") 14 26 40
149 14 my $whole = $frac[0] / $frac[1];
150 14 ▶ $whole = int($whole) if not ref($frac[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $frac[0] 12 2 14
151 14 $frac[0] = abs($frac[0] - $frac[1] * $whole);
152 14 @frac = ($whole, @frac);
153 }
154 40 foreach (@frac) { s/^\+//; }
155 40 return @frac;
156 }
157  
158 0 ▶sub reduce {
Subroutine reduce: not called CC=1 SCAR=6.9
Pod documented
159 0 my $self = shift;
160 0 my ($undef, @frac) = _reduce(@{ $self->{frac} });
161 0 return Math::FractionManip->new(@frac, @{ $self->{tags} });
162 }
163  
164  
165 2 ▶sub decimal {
Subroutine decimal: called CC=3 SCAR=13.3
Pod documented
166 2 my $self = shift;
167 2 my @frac = @{ $self->{frac} };
168 2 ▶ return $frac[0] / $frac[1] if not ref($frac[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $frac[0] 2 0 2
169 0 ▶ return Math::BigFloat->new(Math::BigFloat::bdiv($frac[0], $frac[1], $DEF{CURRENT}{DIGITS})) if ref($frac[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac[0] 0 0 0
170 }
171  
172 1 ▶sub num {
Subroutine num: called CC=3 SCAR=13.3
Pod documented
173 1 my $self = shift;
174 1 my @frac = @{ $self->{frac} };
175 1 ▶ return $frac[0] / $frac[1] if not ref $frac[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $frac[0] 1 0 1
176 0 ▶ return Math::BigFloat->new(Math::BigFloat::bdiv($frac[0], $frac[1], $DEF{CURRENT}{DIGITS})) if ref($frac[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac[0] 0 0 0
177 }
178  
179 ## For the next three methods:
180 # If used on the object use the tags of the object
181 # If given a class use the dafault tags,
182 # .... if a default set is specified then return for that set.
183  
184 6 ▶sub is_tag {
Subroutine is_tag: called CC=13 SCAR=33.4
Pod documented
185 6 my $self = shift;
186 6 my $tag = shift;
187 6 ▶ my $default = 1 if $_[0] eq 'INC_DEF';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "INC_DEF" 0 6 6
188 6 my $is_tag = 0;
189 6 my @tags;
190 {
191 6 ▶ $is_tag = 0, last if not $TAGS{$tag}; #if there is no such tag ret=0
Branch: 1 branch
BranchTrueFalseTotal
unless $TAGS{$tag} 0 6 6
192 6 my ($num, $tag) = @{ $TAGS{$tag} };
193 6 ▶ if (ref($self) eq "Math::FractionManip") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Math::FractionManip') { } 6 0 6
194 6 @tags = @{ $self->{tags} };
195 6 ▶ $is_tag = 1, last if $tags[$num] eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tags[$num] eq $tag 3 3 6
196 3 ▶ $is_tag = undef, last if $tags[$num] eq undef and not $default;
Branch: 1 branch
BranchTrueFalseTotal
if $tags[$num] eq undef and not $default 0 3 3
Condition33%1/3
$tags[$num] eq undef and not $default
!ll&&!rl&&r
300
MC/DC0%0/2
$tags[$num] eq undef and not $default
$tags[$num] eq undef not $default
Truth table33%1/3
A and not(B)
A= $tags[$num] eq undefB= not $default
ABresult
0X0
100
111
197 3 ▶ $is_tag = -1, last if $DEF{CURRENT}{TAGS}[$num] eq $tag
Branch: 1 branch
BranchTrueFalseTotal
if $DEF{"CURRENT"}{"TAGS"}[$num] eq $tag and $tags[$num] eq undef and $default 0 3 3
Condition33%2/6
$DEF{"CURRENT"}{"TAGS"}[$num] eq $tag and $tags[$num] eq undef and $default
!ll&&!rl&&r
300
$DEF{"CURRENT"}{"TAGS"}[$num] eq $tag and $tags[$num] eq undef
!ll&&!rl&&r
300
MC/DC0%0/3
$DEF{"CURRENT"}{"TAGS"}[$num] eq $tag and $tags[$num] eq undef and $default
$DEF{"CURRENT"}{"TAGS"}[$num] eq $tag $tags[$num] eq undef $default
Truth table25%1/4
A and B and C
A= $DEF{"CURRENT"}{"TAGS"}[$num] eq $tagB= $tags[$num] eq undefC= $default
ABCresult
0XX0
10X0
1100
1111
198 and $tags[$num] eq undef and $default;
199 3 $is_tag = 0;
200 }
201 else {
202 0 my $set;
203 0 ▶ $set = 'CURRENT' unless $set = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $set = $_[0] 0 0 0
204 0 ▶ $set = 'BLANK' unless exists $DEF{$set};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DEF{$set} 0 0 0
205 0 ▶ $is_tag = 1, last if $DEF{$set}{TAGS}[$num] eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $DEF{$set}{"TAGS"}[$num] eq $tag 0 0 0
206 0 $is_tag = 0;
207 }
208 }
209 6 return $is_tag;
210 }
211  
212 5 ▶sub tags {
Subroutine tags: called CC=11 SCAR=36.5
Pod documented
213 5 my $self = shift;
214 5 my @tags;
215 5 ▶ if (ref($self) eq "Math::FractionManip") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self eq 'Math::FractionManip') { } 0 5 5
elsif (ref $self ne 'Math::FractionManip') { } 5 0 5
216 0 ▶ my $inc_def = 1 if $_[0] eq 'INC_DEF';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "INC_DEF" 0 0 0
217 0 @tags = @{ $self->{tags} }[0 .. $TAG_END];
218 0 my $num;
219 0 foreach $num (0 .. $#tags) {
220 0 ▶ $tags[$num] = $DEF_TAG[$num] if $tags[$num] eq undef and not $inc_def;
Branch: 1 branch
BranchTrueFalseTotal
if $tags[$num] eq undef and not $inc_def 0 0 0
Condition0%0/3
$tags[$num] eq undef and not $inc_def
!ll&&!rl&&r
000
MC/DC0%0/2
$tags[$num] eq undef and not $inc_def
$tags[$num] eq undef not $inc_def
Truth table0%0/3
A and not(B)
A= $tags[$num] eq undefB= not $inc_def
ABresult
0X0
100
111
221 0 ▶ $tags[$num] = $DEF{CURRENT}{TAGS}[$num] if $tags[$num] eq undef and $inc_def;
Branch: 1 branch
BranchTrueFalseTotal
if $tags[$num] eq undef and $inc_def 0 0 0
Condition0%0/3
$tags[$num] eq undef and $inc_def
!ll&&!rl&&r
000
MC/DC0%0/2
$tags[$num] eq undef and $inc_def
$tags[$num] eq undef $inc_def
Truth table0%0/3
A and B
A= $tags[$num] eq undefB= $inc_def
ABresult
0X0
100
111
222 }
223 }
224 elsif (ref($self) ne "Math::FractionManip") {
225 5 my $set;
226 5 ▶ $set = 'CURRENT' unless $set = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $set = $_[0] 3 2 5
227 5 ▶ $set = 'BLANK' unless exists $DEF{$set};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DEF{$set} 0 5 5
228 5 @tags = @{ $DEF{$set}{TAGS} };
229 }
230 5 return @tags;
231 }
232  
233 5 ▶sub digits {
Subroutine digits: called CC=3 SCAR=11.0
Pod documented
234 5 my $self = shift;
235 5 my $set;
236 5 ▶ $set = 'CURRENT' unless $set = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $set = $_[0] 3 2 5
237 5 ▶ $set = 'BLANK' unless exists $DEF{$set};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DEF{$set} 0 5 5
238 5 return $DEF{$set}{DIGITS};
239 }
240  
241 ###############################
242 # These methods are used for managing default sets.
243  
244 0 ▶sub sets {
Subroutine sets: not called CC=1 SCAR=6.9
Pod documented
245 0 my $self = shift;
246 0 return keys %DEF;
247 }
248  
249 1 ▶sub name_set {
Subroutine name_set: called CC=3 SCAR=11.7
Pod documented
250 1 shift;
251 1 ▶ return $DEF{CURRENT}{NAME} if not $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 1 1
252 1 ▶ $DEF{CURRENT}{NAME} = $_[0] if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 1 0 1
253 }
254  
255 0 ▶sub exists_set {
Subroutine exists_set: not called CC=1 SCAR=6.9
Pod documented
256 0 return exists $DEF{ $_[1] };
257 }
258  
259 1 ▶sub use_set {
Subroutine use_set: called CC=3 SCAR=12.3
Pod documented
260 1 my $self = shift;
261 1 my $name = shift;
262 1 ▶ if (exists $DEF{$name} and not $DEF{$name}{READONLY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DEF{$name} and not $DEF{$name}{'READONLY'}) { } 1 0 1
Condition33%1/3
exists $DEF{$name} and not $DEF{$name}{"READONLY"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $DEF{$name} and not $DEF{$name}{"READONLY"}
exists $DEF{$name} not $DEF{$name}{"READONLY"}
Truth table33%1/3
A and not(B)
A= exists $DEF{$name}B= not $DEF{$name}{"READONLY"}
ABresult
0X0
100
111
263 1 $DEF{CURRENT} = $DEF{$name};
264 1 return $name;
265 }
266 else {
267 0 return undef;
268 }
269 }
270  
271 0 ▶sub temp_set {
Subroutine temp_set: not called CC=2 SCAR=17.9
Pod documented
272 0 my $self = shift;
273 0 my $name = shift;
274 0 ▶ if (not $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name) { } 0 0 0
275 0 $ID++;
276 0 $name = "\cI\cD$ID";
277 0 $self->copy_set('CURRENT', $name);
278 0 $self->copy_set('DEFAULT', 'CURRENT');
279 0 return $name;
280 }
281 else { #if $name;
282 0 my $return = $self->copy_set($name, 'CURRENT');
283 0 $self->del_set($name);
284 0 return $return;
285 }
286 }
287  
288 2 ▶sub load_set {
Subroutine load_set: called CC=3 SCAR=12.0
Pod documented
289 2 my $self = shift;
290 2 ▶ if (exists $DEF{ $_[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DEF{$_[0]}) { } 2 0 2
291 2 ▶ $self->copy_set($_[0], 'CURRENT') if exists $DEF{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
if exists $DEF{$_[0]} 2 0 2
292 2 return $_[0];
293 }
294 else {
295 0 return undef;
296 }
297 }
298  
299 1 ▶sub save_set {
Subroutine save_set: called CC=5 SCAR=18.9
Pod documented
300 1 my $self = shift;
301 1 my $name;
302 1 ▶ $name = $DEF{CURRENT}{NAME} unless $name = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless $name = shift() 1 0 1
303 1 ▶ ++$ID, $name = "\cI\cD:$ID" if not $name or $name eq 'RAND';
Branch: 1 branch
BranchTrueFalseTotal
if not $name or $name eq "RAND" 0 1 1
Condition33%1/3
not $name or $name eq "RAND"
l!l&&r!l&&!r
001
MC/DC0%0/2
not $name or $name eq "RAND"
not $name $name eq "RAND"
Truth table33%1/3
not(A) or B
A= not $nameB= $name eq "RAND"
ABresult
1X1
011
000
304 1 ▶ return $self->copy_set('CURRENT', $name) && $name;
Condition33%1/3
$self->copy_set('CURRENT', $name) && $name
!ll&&!rl&&r
010
MC/DC0%0/2
$self->copy_set('CURRENT', $name) && $name
$self->copy_set('CURRENT', $name) $name
Truth table33%1/3
A && B
A= $self->copy_set('CURRENT', $name)B= $name
ABresult
0X0
100
111
305 }
306  
307 3 ▶sub copy_set {
Subroutine copy_set: called CC=6 SCAR=18.9
Pod documented
308 3 shift;
309 3 my ($name1, $name2) = @_;
310 3 ▶ if ($DEF{$name2}{READONLY} or $name2 eq 'BLANK' or not exists $DEF{$name1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEF{$name2}{'READONLY'} or $name2 eq 'BLANK' or not exists $DEF{$name1}) { } 0 3 3
Condition33%2/6
$DEF{$name2}{"READONLY"} or $name2 eq "BLANK" or not exists $DEF{$name1}
l!l&&r!l&&!r
003
$DEF{$name2}{"READONLY"} or $name2 eq "BLANK"
l!l&&r!l&&!r
003
MC/DC0%0/3
$DEF{$name2}{"READONLY"} or $name2 eq "BLANK" or not exists $DEF{$name1}
$DEF{$name2}{"READONLY"} $name2 eq "BLANK" not exists $DEF{$name1}
Truth table25%1/4
A or B or not(C)
A= $DEF{$name2}{"READONLY"}B= $name2 eq "BLANK"C= not exists $DEF{$name1}
ABCresult
1XX1
01X1
0011
0000
311 0 return 0;
312 }
313 else {
314 3 $DEF{$name2} = {}; # kill any links from use;
315 3 $DEF{$name2}{TAGS} = [@{ $DEF{$name1}{TAGS} }];
316 3 $DEF{$name2}{DIGITS} = $DEF{$name1}{DIGITS};
317 3 ▶ $DEF{$name2}{NAME} = $name2 unless $name2 eq 'CURRENT';
Branch: 1 branch
BranchTrueFalseTotal
unless $name2 eq "CURRENT" 1 2 3
318 3 ▶ $DEF{$name2}{NAME} = $name1 if $name2 eq 'CURRENT';
Branch: 1 branch
BranchTrueFalseTotal
if $name2 eq "CURRENT" 2 1 3
319 3 return 1;
320 }
321 }
322  
323 0 ▶sub del_set {
Subroutine del_set: not called CC=3 SCAR=24.8
Pod documented
324 0 ▶ if (exists $DEF{ $_[1] } and not $DEF{ $_[1] }{SYSTEM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DEF{$_[1]} and not $DEF{$_[1]}{"SYSTEM"}) 0 0 0
Condition0%0/3
exists $DEF{$_[1]} and not $DEF{$_[1]}{"SYSTEM"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $DEF{$_[1]} and not $DEF{$_[1]}{"SYSTEM"}
exists $DEF{$_[1]} not $DEF{$_[1]}{"SYSTEM"}
Truth table0%0/3
A and not(B)
A= exists $DEF{$_[1]}B= not $DEF{$_[1]}{"SYSTEM"}
ABresult
0X0
100
111
325 0 delete $DEF{ $_[1] };
326 0 return $_[1];
327 }
328 }
329  
330 ###############################
331 # All of the modify methods are not meant to return anything, they modify
332 # the object being referenced.
333  
334 # This method works almost like the new method except that it takes an
335 # object as an argement and will modify it instead of creating a new
336 # object, also any tags associated with the object are left in tact
337 # unless a new tag is given to override the old.
338 0 ▶sub modify {
Subroutine modify: not called CC=1 SCAR=6.9
Pod documented
339 0 my $me = shift;
340 0 my $self;
341 0 my @tags = @{ $me->{tags} };
342 0 $self = Math::FractionManip->new(@_, @tags, @_); # The extra @_ is there to override tags
343 0 $me->{frac} = $self->{frac};
344 0 $me->{tags} = $self->{tags};
345 }
346  
347 # XXX This method appears to break things? -GB
348 1 ▶sub modify_digits {
Subroutine modify_digits: called CC=1 SCAR=0.0
Pod documented
349 1 my $self = shift;
350 1 $DEF{CURRENT}{DIGITS} = shift;
351 }
352  
353 0 ▶sub modify_reduce {
Subroutine modify_reduce: not called CC=1 SCAR=6.9
Pod documented
354 0 my $me = shift;
355 0 my $self = $me->reduce;
356 0 $me->{frac} = $self->{frac};
357 0 $me->{tags} = $self->{tags};
358 }
359  
360 0 ▶sub modify_num {
Subroutine modify_num: not called CC=1 SCAR=6.9
Pod documented
361 0 my $self = shift;
362 0 $self->[0] = $_[0];
363 }
364  
365 0 ▶sub modify_den {
Subroutine modify_den: not called CC=1 SCAR=6.9
Pod documented
366 0 my $self = shift;
367 0 $self->[1] = $_[0];
368 }
369  
370 5 ▶sub modify_tag {
Subroutine modify_tag: called CC=9 SCAR=23.1
Pod documented
371 5 my $self = shift;
372 5 my ($return, @return);
373 5 my $newtag;
374 5 foreach $newtag (@_) {
375 7 my $tagnum = _tagnum($newtag);
376 7 ▶ if ($tagnum == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagnum == -1) { } 0 7 7
elsif (ref $self eq 'Math::FractionManip') { } 3 4 7
377 0 push @return, undef;
378 }
379 elsif (ref($self) eq "Math::FractionManip") {
380 3 my @frac = @{ $self->{frac} };
381 3 my @tags = @{ $self->{tags} };
382 3 my @newtags = _tags(@tags, $newtag);
383  
384 # Now transform the Fraction based on the new tag.
385 3 ▶ if ($tagnum == $SIZE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagnum == $SIZE) { } 1 2 3
elsif ($tagnum == $REDUCE) { } 0 2 2
386 1 my $newtag = _tag($SIZE, \@newtags);
387 0 ▶ @frac = map { "$_" + 0 } @frac if $newtag eq 'SMALL';
Branch: 1 branch
BranchTrueFalseTotal
if $newtag eq "SMALL" 0 1 1
388 1 ▶ @frac = map { Math::BigInt->new($_) } @frac if $newtag eq 'BIG';
Branch: 1 branch
BranchTrueFalseTotal
if $newtag eq "BIG" 1 0 1
389 }
390 elsif ($tagnum == $REDUCE) {
391 0 ▶ (undef, @frac) = _reduce(@frac) if _tag($REDUCE, \@newtags) eq 'REDUCE';
Branch: 1 branch
BranchTrueFalseTotal
if _tag($REDUCE, \@newtags) eq "REDUCE" 0 0 0
392 }
393  
394 # Finally Modify the Fraction
395 3 $self->{frac} = \@frac;
396 3 $self->{tags} = \@newtags;
397 }
398 else {
399 4 $DEF{CURRENT}{TAGS}[$tagnum] = $newtag;
400 }
401 7 push @return, $newtag;
402 }
403 5 return @return;
404 }
405  
406 ###############################
407 # These methods are meant to be called with the overload operators.
408  
409 16 ▶sub add {
Subroutine add: called CC=9 SCAR=22.0
Pod documented
410 16 my @frac1 = @{ $_[0]->{frac} };
411 16 my @tags1 = @{ $_[0]->{tags} };
412 16 my (@frac2, @frac, @tags2, $frac);
413 16 my $skipauto = 0;
414 16 ▶ @frac2 = @{ $_[1]->{frac} }, @tags2 = @{ $_[1]->{tags} } if ref($_[1]) eq "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "Math::FractionManip" 10 6 16
415 16 ▶ @frac2 = _from_decimal($_[1]), $tags2[$INTERNAL] = 'CONVERTED' if ref($_[1]) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] ne "Math::FractionManip" 6 10 16
416 16 my @tags = _tags_preserve([@tags1], [@tags2]);
417  
418 LOOP: {
419 16 ▶ if (_tag($REDUCE, \@tags) eq 'NO_REDUCE') {
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($REDUCE, \@tags) eq 'NO_REDUCE') { } 4 13 17
420 4 @frac = ($frac1[0] * $frac2[1] + $frac2[0] * $frac1[1], $frac1[1] * $frac2[1]);
421 }
422 else {
423  
424 # Taken from Knuth v2 (rev 2), p313.
425 # It will always return a reduced fraction.
426 13 my $gcd1 = _gcd($frac1[1], $frac2[1]);
427 13 my $tmp = $frac1[0] * ($frac2[1] / $gcd1) + $frac2[0] * ($frac1[1] / $gcd1);
428 13 my $gcd2 = _gcd($tmp, $gcd1);
429 13 @frac = ($tmp / $gcd2, ($frac1[1] / $gcd1) * ($frac2[1] / $gcd2));
430 13 $tags[$RED_STATE] = 'IS_REDUCED';
431 }
432 17 ▶ if ((_tag($AUTO, \@tags) eq 'AUTO') and (not $skipauto) and
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/) 1 16 17
Condition75%9/12
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
!ll&&!rl&&r
1151
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL"
!ll&&!rl&&r
1016
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto
!ll&&!rl&&r
0116
$frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/
l!l&&r!l&&!r
1015
MC/DC40%2/5
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
_tag($AUTO, \@tags) eq "AUTO" not $skipauto $tags[$SIZE] eq "SMALL" $frac[0] =~ /[eE]/ $frac[1] =~ /[eE]/
Truth table50%3/6
A and not(B) and C and D or E
A= _tag($AUTO, \@tags) eq "AUTO"B= not $skipautoC= $tags[$SIZE] eq "SMALL"D= $frac[0] =~ /[eE]/E= $frac[1] =~ /[eE]/
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
433 ($tags[$SIZE] eq 'SMALL') and ($frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/))
434 {
435 1 (@frac1[0 .. 1], @frac2[0 .. 1]) = map { Math::BigInt->new($_) } (@frac1, @frac2);
436 1 $tags[$SIZE] = 'BIG';
437 1 $skipauto = 1;
438 1 redo LOOP;
439 }
440 }
441 16 return Math::FractionManip->new(@frac, @tags);
442 }
443  
444 1 ▶sub sub {
Subroutine sub: called CC=3 SCAR=11.2
Pod documented
445 1 my ($frac1, $frac2) = ($_[$_[2]], $_[not $_[2]]); # swap if needed
446 1 ▶ $frac1 = Math::FractionManip->new($frac1, 'CONVERTED') if ref($frac1) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac1 ne "Math::FractionManip" 0 1 1
447 1 ▶ $frac2 = Math::FractionManip->new($frac2, 'CONVERTED') if ref($frac2) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac2 ne "Math::FractionManip" 0 1 1
448  
449 1 $frac2 = Math::FractionManip->new($frac2->{frac}[0], -$frac2->{frac}[1], @{ $frac2->{tags} });
450  
451 1 return $frac1 + $frac2;
452 }
453  
454 7 ▶sub mul {
Subroutine mul: called CC=9 SCAR=22.0
Pod documented
455 7 my @frac1 = @{ $_[0]{frac} };
456 7 my @tags1 = @{ $_[0]{tags} };
457 7 my (@frac2, @frac, @tags2);
458 7 ▶ @frac2 = @{ $_[1]->{frac} }, @tags2 = @{ $_[1]->{tags} } if ref($_[1]) eq "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "Math::FractionManip" 3 4 7
459 7 ▶ @frac2 = (_from_decimal($_[1])), $tags2[$INTERNAL] = 'CONVERTED' if ref($_[1]) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] ne "Math::FractionManip" 4 3 7
460 7 my @tags = _tags_preserve([@tags1], [@tags2]);
461 7 my $skipauto = 0;
462 LOOP: {
463 7 ▶ if (_tag($REDUCE, \@tags) eq 'NO_REDUCE') {
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($REDUCE, \@tags) eq 'NO_REDUCE') { } 2 6 8
464 2 @frac = ($frac1[0] * $frac2[0], $frac1[1] * $frac2[1]);
465 }
466 else {
467 6 my ($gcd1, $gcd2) = (_gcd($frac1[0], $frac2[1]), _gcd($frac2[0], $frac1[1]));
468 6 $frac[0] = ($frac1[0] / $gcd1) * ($frac2[0] / $gcd2);
469 6 $frac[1] = ($frac1[1] / $gcd2) * ($frac2[1] / $gcd1);
470 6 $tags[$RED_STATE] = 'IS_REDUCED';
471 }
472 8 ▶ if ((_tag($AUTO, \@tags) eq 'AUTO') and (not $skipauto) and
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/) 1 7 8
Condition75%9/12
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
!ll&&!rl&&r
161
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL"
!ll&&!rl&&r
107
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto
!ll&&!rl&&r
017
$frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/
l!l&&r!l&&!r
106
MC/DC40%2/5
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
_tag($AUTO, \@tags) eq "AUTO" not $skipauto $tags[$SIZE] eq "SMALL" $frac[0] =~ /[eE]/ $frac[1] =~ /[eE]/
Truth table50%3/6
A and not(B) and C and D or E
A= _tag($AUTO, \@tags) eq "AUTO"B= not $skipautoC= $tags[$SIZE] eq "SMALL"D= $frac[0] =~ /[eE]/E= $frac[1] =~ /[eE]/
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
473 ($tags[$SIZE] eq 'SMALL') and ($frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/))
474 {
475 1 (@frac1[0 .. 1], @frac2[0 .. 1]) = map { Math::BigInt->new($_) } (@frac1, @frac2);
476 1 $tags[$SIZE] = 'BIG';
477 1 $skipauto = 1;
478 1 redo LOOP;
479 }
480 }
481 7 return Math::FractionManip->new(@frac, @tags);
482 }
483  
484 1 ▶sub div {
Subroutine div: called CC=3 SCAR=11.2
Pod documented
485 1 my ($frac1, $frac2) = ($_[$_[2]], $_[not $_[2]]); # swap if needed
486 1 ▶ $frac1 = Math::FractionManip->new($frac1, 'CONVERTED') if ref($frac1) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac1 ne "Math::FractionManip" 0 1 1
487 1 ▶ $frac2 = Math::FractionManip->new($frac2, 'CONVERTED') if ref($frac2) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $frac2 ne "Math::FractionManip" 0 1 1
488  
489 1 $frac2 = Math::FractionManip->new($frac2->{frac}[1], $frac2->{frac}[0], @{ $frac2->{tags} });
490  
491 #Makes a copy of the fraction with the num and den switched.
492  
493 1 return $frac1 * $frac2;
494 }
495  
496 2 ▶sub pow {
Subroutine pow: called CC=10 SCAR=29.2
Pod documented
497 2 my (@frac, @frac1, @tags1);
498 2 ▶ @frac1 = @{ $_[$_[2]]->{frac} }, @tags1 = @{ $_[$_[2]]->{tags} } if ref($_[$_[2]]) eq "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$_[2]] eq "Math::FractionManip" 2 0 2
499 2 ▶ @frac1 = _from_decimal($_[$_[2]]) if ref($_[$_[2]]) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$_[2]] ne "Math::FractionManip" 0 2 2
500 2 my $frac2;
501 2 ▶ $frac2 = $_[not $_[2]]->decimal if ref($_[not $_[2]]) eq "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[not $_[2]] eq "Math::FractionManip" 0 2 2
502 2 ▶ $frac2 = $_[not $_[2]] if ref($_[not $_[2]]) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[not $_[2]] ne "Math::FractionManip" 2 0 2
503 2 my @tags = @tags1;
504 2 my $skipauto = 0;
505  
506 LOOP: {
507 2 @frac = ($frac1[0] ** $frac2, $frac1[1] ** $frac2);
508  
509 2 ▶ if ((_tag($AUTO, \@tags) eq 'AUTO') and (not $skipauto) and
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/) 0 2 2
Condition33%4/12
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
!ll&&!rl&&r
020
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL"
!ll&&!rl&&r
002
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto
!ll&&!rl&&r
002
$frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/
l!l&&r!l&&!r
002
MC/DC0%0/5
_tag($AUTO, \@tags) eq "AUTO" and not $skipauto and $tags[$SIZE] eq "SMALL" and $frac[0] =~ /[eE]/ || $frac[1] =~ /[eE]/
_tag($AUTO, \@tags) eq "AUTO" not $skipauto $tags[$SIZE] eq "SMALL" $frac[0] =~ /[eE]/ $frac[1] =~ /[eE]/
Truth table16%1/6
A and not(B) and C and D or E
A= _tag($AUTO, \@tags) eq "AUTO"B= not $skipautoC= $tags[$SIZE] eq "SMALL"D= $frac[0] =~ /[eE]/E= $frac[1] =~ /[eE]/
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
510 ($tags[$SIZE] eq 'SMALL') and ($frac[0] =~ /[eE]/ or $frac[1] =~ /[eE]/))
511 {
512 0 @frac1 = map { Math::BigInt->new($_) } @frac1;
513 0 $tags[$SIZE] = 'BIG';
514 0 $skipauto = 1;
515 0 redo LOOP;
516 }
517 }
518  
519 2 return Math::FractionManip->new(@frac, @tags);
520 }
521  
522 1 ▶sub sqrt {
Subroutine sqrt: called CC=2 SCAR=7.2
Pod documented
523 1 my $self = shift;
524 1 my @frac = @{ $self->{frac} };
525 1 my @tags = @{ $self->{tags} };
526 1 my $ans;
527 1 ▶ if (ref($frac[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $frac[0]) { } 0 1 1
528 0 $frac[0] = Math::BigFloat->new(Math::BigFloat::fsqrt($frac[0], $DEF{CURRENT}{DIGITS}));
529 0 $frac[1] = Math::BigFloat->new(Math::BigFloat::fsqrt($frac[1], $DEF{CURRENT}{DIGITS}));
530 }
531 else {
532 1 @frac = (sqrt($frac[0]), sqrt($frac[1]));
533 }
534 1 return Math::FractionManip->new(@frac, @tags);
535 }
536  
537 0 ▶sub abs {
Subroutine abs: not called CC=1 SCAR=6.9
Pod documented
538 0 my $self = shift;
539 0 my @frac = @{ $self->{frac} };
540 0 my @tags = @{ $self->{tags} };
541 0 return Math::FractionManip->new(abs($frac[0]), abs($frac[1]), @tags, 'IS_REDUCED');
542 }
543  
544 0 ▶sub cmp {
Subroutine cmp: not called CC=4 SCAR=30.0
Pod documented
545 0 my @frac1 = @{ $_[0]->{frac} };
546 0 my @tags1 = @{ $_[0]->{tags} };
547 0 my (@frac2, @frac, @tags2, $x, $y);
548 0 ▶ @frac2 = @{ $_[1]->{frac} }, @tags2 = @{ $_[1]->{tags} } if ref($_[1]) eq "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "Math::FractionManip" 0 0 0
549 0 ▶ @frac2 = _from_decimal($_[1]), @tags2 = qw(CONVERTED) if ref($_[1]) ne "Math::FractionManip";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] ne "Math::FractionManip" 0 0 0
550 0 my @tags = _tags_preserve([@tags1], [@tags2]);
551 0 ▶ if (_tag($REDUCE, \@tags) == 'NO_REDUCE') {
Branch: 1 branch
BranchTrueFalseTotal
if (_tag($REDUCE, \@tags) == 'NO_REDUCE') { } 0 0 0
552 0 $x = $frac1[0] * $frac2[1];
553 0 $y = $frac2[0] * $frac1[1];
554 }
555 else {
556 0 my $gcd1 = _gcd($frac1[1], $frac2[1]);
557 0 $x = $frac1[0] * ($frac2[1] / $gcd1);
558 0 $y = $frac2[0] * ($frac1[1] / $gcd1);
559 }
560 0 return $x <=> $y;
561 }
562  
563 ################################
564 # These functions are internal
565  
566 67 ▶sub _fix_num {
Subroutine _fix_num: called CC=16 SCAR=29.6
567 67 my $tagsref = shift;
568 67 my @return = @_;
569 67 my $auto = _tag($AUTO, $tagsref) eq 'AUTO';
570 67 $tagsref->[$SIZE] = _tag($SIZE, $tagsref);
571 67 ▶ $tagsref->[$SIZE] = 'SMALL' if $auto;
Branch: 1 branch
BranchTrueFalseTotal
if $auto 67 0 67
572 67 my $num;
573 67 my $decimal = 0;
574 67 foreach $num (@return) {
575 133 ▶ if (ref($num) eq "Math::BigFloat") {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $num eq 'Math::BigFloat') { } 0 133 133
elsif (ref $num eq 'Math::BigInt') { } 4 129 133
elsif (ref $num) { } 0 129 129
elsif ($num =~ /[\.\e]/) { } 9 120 129
576 0 ▶ $tagsref->[$SIZE] = 'BIG' unless $auto;
Branch: 1 branch
BranchTrueFalseTotal
unless $auto 0 0 0
577 0 $decimal = 1;
578 }
579 elsif (ref($num) eq "Math::BigInt") {
580 4 ▶ $tagsref->[$SIZE] = 'BIG' unless $auto;
Branch: 1 branch
BranchTrueFalseTotal
unless $auto 0 4 4
581 }
582 elsif (ref($num)) {
583  
584 # do nothing
585 }
586 elsif ($num =~ /[\.\e]/) {
587 9 $decimal = 1;
588 }
589 133 ▶ if ($auto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto) 133 0 133
590 133 $num =~ /[\+\-]?\s*0*([0-9]*)\s*\.?\s*([0-9]*)0*/;
591 133 my $length = length($1) + length($2);
592 133 ▶ $tagsref->[$SIZE] = 'BIG' if $length > 15;
Branch: 1 branch
BranchTrueFalseTotal
if $length > 15 4 129 133
593 }
594 }
595 67 ▶ if ($tagsref->[$SIZE] eq 'BIG') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagsref->[$SIZE] eq "BIG") 2 65 67
596 2 ▶ @return = map { Math::BigInt->new("$_") } @return if not $decimal;
Branch: 1 branch
BranchTrueFalseTotal
unless $decimal 2 0 2
597 0 ▶ @return = map { Math::BigFloat->new("$_") } @return if $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $decimal 0 2 2
598 }
599 67 ▶ if ($tagsref->[$SIZE] eq 'SMALL' and $auto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagsref->[$SIZE] eq "SMALL" and $auto) 65 2 67
Condition66%2/3
$tagsref->[$SIZE] eq "SMALL" and $auto
!ll&&!rl&&r
2065
MC/DC50%1/2
$tagsref->[$SIZE] eq "SMALL" and $auto
$tagsref->[$SIZE] eq "SMALL" $auto
Truth table66%2/3
A and B
A= $tagsref->[$SIZE] eq "SMALL"B= $auto
ABresult
0X0
100
111
600 65 @return = map { "$_" + 0 } @return;
601 }
602 67 return ($decimal, @return);
603 }
604  
605 6 ▶sub _fix_auto {
Subroutine _fix_auto: called CC=6 SCAR=19.3
606 6 my $direction = shift;
607 6 my $tagsref = shift;
608 6 my @return = @_;
609 6 $tagsref->[$SIZE] = 'SMALL';
610 6 my $num;
611 6 foreach $num (@return) {
612 12 $num =~ /[\+\-]?\s*0*([0-9]*)\s*\.?\s*([0-9]*)0*/;
613 12 my $length = length($1) + length($2);
614 12 ▶ $tagsref->[$SIZE] = 'BIG' if $length > 15;
Branch: 1 branch
BranchTrueFalseTotal
if $length > 15 0 12 12
615 }
616 6 ▶ if ($tagsref->[$SIZE] eq 'BIG' and $direction eq 'BOTH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagsref->[$SIZE] eq 'BIG' and $direction eq 'BOTH') { } 0 6 6
elsif ($tagsref->[$SIZE] eq 'SMALL') { } 6 0 6
Condition33%1/3
$tagsref->[$SIZE] eq "BIG" and $direction eq "BOTH"
!ll&&!rl&&r
600
MC/DC0%0/2
$tagsref->[$SIZE] eq "BIG" and $direction eq "BOTH"
$tagsref->[$SIZE] eq "BIG" $direction eq "BOTH"
Truth table33%1/3
A and B
A= $tagsref->[$SIZE] eq "BIG"B= $direction eq "BOTH"
ABresult
0X0
100
111
617 0 @return = map { Math::BigInt->new("$_") } @return;
618 }
619 elsif ($tagsref->[$SIZE] eq 'SMALL') {
620 6 @return = map { "$_" + 0 } @return;
621 }
622 6 return (@return);
623 }
624  
625 302 ▶sub _is_decimal {
Subroutine _is_decimal: called CC=2 SCAR=6.9
626 302 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 48 254 302
627 254 my $return = $_[0] =~ /^\s*[\+\-0-9eE\.]+\s*$/;
628 254 return $return;
629 }
630  
631 33 ▶sub _reduce {
Subroutine _reduce: called CC=2 SCAR=6.9
632 33 my @frac = @_;
633 33 my $gcd = _gcd(@frac);
634 33 ▶ if ($gcd == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gcd == 1) { } 27 6 33
635 27 return (0, @frac);
636 }
637 else {
638 6 return (1, $frac[0] / $gcd, $frac[1] / $gcd);
639 }
640 }
641  
642 55 ▶sub _simplify_sign {
Subroutine _simplify_sign: called CC=2 SCAR=6.9
643 55 my @frac = @_;
644 55 my $sign = 1;
645 55 ▶ $sign = ($frac[0] / CORE::abs($frac[0])) * ($frac[1] / CORE::abs($frac[1])) if $frac[0];
Branch: 1 branch
BranchTrueFalseTotal
if $frac[0] 47 8 55
646 55 @frac = ($sign * CORE::abs($frac[0]), CORE::abs($frac[1]));
647 55 return @frac;
648 }
649  
650 70 ▶sub _tags {
Subroutine _tags: called CC=3 SCAR=11.0
651 70 my @return = (undef, undef);
652 70 my ($NUM, $VALUE) = (0, 1);
653  
654 70 foreach (@_) {
655 194 ▶ next if not $TAGS{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $TAGS{$_} 113 81 194
656 81 my ($num, $value) = @{ $TAGS{$_} };
657 81 $return[$num] = $value;
658 }
659  
660 70 return @return;
661 }
662  
663 416 ▶sub _tag {
Subroutine _tag: called CC=3 SCAR=11.0
664 416 my $item = shift;
665 416 my $return;
666 my $ref;
667 416 foreach $ref (@_, $DEF{CURRENT}{TAGS}) {
668 769 ▶ last if $return = ${$ref}[$item];
Branch: 1 branch
BranchTrueFalseTotal
if $return = do { $ref }->[$item] 338 431 769
669 }
670 416 return $return;
671 }
672  
673 7 ▶sub _tagnum {
Subroutine _tagnum: called CC=2 SCAR=7.6
674 7 my $item = shift;
675 7 ▶ if (exists $TAGS{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $TAGS{$item}) { } 7 0 7
676 7 return $TAGS{$item}[0];
677 }
678 else {
679 0 return -1;
680 }
681 }
682  
683 23 ▶sub _tags_preserve {
Subroutine _tags_preserve: called CC=4 SCAR=14.0
684 23 my @tags1 = @{ $_[0] };
685 23 my @tags2 = @{ $_[1] };
686 23 my @tags;
687 23 ▶ if ($tags1[$INTERNAL] eq 'CONVERTED') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tags1[$INTERNAL] eq 'CONVERTED') { } 0 23 23
elsif ($tags2[$INTERNAL] eq 'CONVERTED') { } 10 13 23
688 0 @tags = @tags2;
689 }
690 elsif ($tags2[$INTERNAL] eq 'CONVERTED') {
691 10 @tags = @tags1;
692 }
693 else {
694 13 ▶ @tags = map { $tags1[$_] eq $tags2[$_] and $tags1[$_] } (0 .. $#tags1);
Condition66%2/3
$tags1[$_] eq $tags2[$_] && $tags1[$_]
!ll&&!rl&&r
03417
MC/DC50%1/2
$tags1[$_] eq $tags2[$_] && $tags1[$_]
$tags1[$_] eq $tags2[$_] $tags1[$_]
Truth table66%2/3
A && B
A= $tags1[$_] eq $tags2[$_]B= $tags1[$_]
ABresult
0X0
100
111
695 }
696 23 return @tags;
697 }
698  
699 71 ▶sub _gcd {
Subroutine _gcd: called CC=7 SCAR=19.7
700 71 my ($x, $y) = (CORE::abs($_[0]), CORE::abs($_[1]));
701 71 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 0 71 71
702 0 $x = Math::BigInt->new($x->bgcd($y));
703 }
704 else {
705 {
706 71 ▶ $x = 1, last if $y > 1e17; # If this is so % will thinks its a zero so if
Branch: 1 branch
BranchTrueFalseTotal
if $y > "1e+17" 0 71 71
707 # $y>1e17 will simply will basicly give up and
708 # have it return 1 as the GCD.
709 71 my ($x0);
710 71 ▶ while ($y != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y != 0) 179 70 249
711 179 $x0 = $x;
712 179 ($x, $y) = ($y, $x % $y);
713  
714 # Note $x0 = $x, $x = $y, $y= $x % $y Before the Swith
715 179 ▶ $x = 1, last if ($x0 > 99999999 or $x > 999999999) and int($x0 / $x) * $x + $y != $x0;
Branch: 1 branch
BranchTrueFalseTotal
if $x0 > 99999999 || $x > 999999999 and int($x0 / $x) * $x + $y != $x0 1 178 179
Condition83%5/6
$x0 > 99999999 || $x > 999999999 and int($x0 / $x) * $x + $y != $x0
!ll&&!rl&&r
148301
$x0 > 99999999 or $x > 999999999
l!l&&r!l&&!r
310148
MC/DC66%2/3
$x0 > 99999999 || $x > 999999999 and int($x0 / $x) * $x + $y != $x0
$x0 > 99999999 $x > 999999999 int($x0 / $x) * $x + $y != $x0
Truth table60%3/5
A or B and C
A= $x0 > 99999999B= $x > 999999999C= int($x0 / $x) * $x + $y != $x0
ABCresult
00X0
1X00
0100
1X11
0111
716  
717 # This is to see if the mod operater through up on us when dealing with
718 # large numbers. If it did set the gcd = 1 and quit.
719 }
720 }
721 }
722 71 return $x;
723 }
724  
725 3 ▶sub _de_decimal {
Subroutine _de_decimal: called CC=1 SCAR=0.0
726 3 my @frac = @_;
727 3 my @return;
728 3 my $big = _tag($SIZE, $_[2]);
729 3 my (@int_part, @decimal_part);
730 3 ($int_part[0], $decimal_part[0]) = $frac[0] =~ /(\d+)\.(\d+)/;
731 3 ($int_part[1], $decimal_part[1]) = $frac[1] =~ /(\d+)\.(\d+)/;
732 3 @decimal_part = sort { $a <=> $b } (length($decimal_part[0]), length($decimal_part[1]));
733 3 my $factor = 10 ** $decimal_part[1];
734 3 @frac = ($_[0] * $factor, $_[1] * $factor);
735 3 return @frac;
736 }
737  
738 13 ▶sub _from_decimal {
Subroutine _from_decimal: called CC=20 SCAR=30.6
739 13 my $decimal = shift; # the decimal (1.312671267127)
740 13 ▶ my $big = 'BIG' if ref($decimal);
Branch: 1 branch
BranchTrueFalseTotal
if ref $decimal 0 13 13
741 13 my ($repeat); # flag to keep track if it is repeating or not
742 my ($sign);
743 13 my ($factor, $int_factor);
744 13 my ($factor2);
745 13 my ($whole_num, $whole_num_len);
746 13 my ($int_part); # integer part (1)
747 13 my ($decimal_part, $decimal_part_len); # decimal part (312671267127)
748 13 my ($decimal_part2); # decimal part - last bit \/ (312671267)
749 13 my ($pat, $pat_len); # repeating pat (1267)
750 13 my ($pat_lastb); # last bit of repeating pat (127)
751 13 my ($beg_part, $beg_part_len); # non-repeating part (3)
752 13 my ($other_part, $other_part_len); # repeating part (1267126712127)
753 13 my ($frac1, $frac2, $frac3);
754  
755 13 my $rnd_mode = $Math::BigFloat::rnd_mode; # to avoid problems with incon.
756 13 $Math::BigFloat::rnd_mode = 'trunc'; # rounding
757  
758 13 $decimal = "$decimal";
759 13 $decimal =~ s/\s//g;
760 13 ($sign, $int_part, $decimal_part) = $decimal =~ /([\+\-]?)\s*(\d*)\.(\d+)$/;
761 13 $sign .= '1';
762 13 $decimal_part_len = length($decimal_part);
763 13 ▶ $int_part = "" unless $int_part;
Branch: 1 branch
BranchTrueFalseTotal
unless $int_part 10 3 13
764 13 $factor = '1' . '0' x (length($decimal_part));
765 13 ▶ $factor = Math::BigFloat->new($factor) if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 13 13
766  
767 # Make it a BigFloat now to simplfy latter
768 13 $int_factor = '1' . '0' x (length($int_part));
769 13 $beg_part_len = 0;
770 OuterBlock:
771 13 ▶ while ($beg_part_len < $decimal_part_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beg_part_len < $decimal_part_len) 4 9 13
772 4 $beg_part = substr($decimal_part, 0, $beg_part_len);
773 4 $other_part = substr($decimal_part, $beg_part_len);
774 4 $other_part_len = length($other_part);
775 4 my $i;
776 4 ▶ for ($i = 1; $i < ($other_part_len / 2 + 1); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $other_part_len / 2 + 1) 4 0 4
777 4 $pat = substr($other_part, 0, $i);
778 4 $pat_len = $i;
779 4 local $_ = $other_part;
780 4 $repeat = undef;
781 4 while (1) {
782 21 ($_) = /^$pat(.*)/;
783 21 my $length = length($_);
784  
785 21 ▶ if ($length <= $pat_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $pat_len) 4 17 21
786 4 ▶ last unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 4 4
787 4 $pat_lastb = substr($pat, 0, $length);
788 4 ▶ $repeat = 1, last OuterBlock if $pat_lastb eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $pat_lastb eq $_ 2 2 4
789 2 ▶ if ($pat_lastb eq $_ - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_lastb eq $_ - 1) 2 0 2
790  
791 # this is needed to see if it really is the repeating fracton
792 # we intented it to be. If we don't do this 1.1212 would become
793 # 1120/999 = 1.1211211211.
794 # The first three lines converts it to a fraction and the
795 # rests tests it to the actual repeating decimal/
796 # The NO_REDUCE flag is their to save time as reducing large
797 # fraction can take a bit of time which is unnecessary as we will
798 # be converting it to a decimal.
799 2 $decimal_part2 = substr($decimal_part, 0, $decimal_part_len - length($pat_lastb));
800 2 $factor2 = '1' . '0' x (length($decimal_part2));
801 2 $frac1 = Math::FractionManip->new('0' . $beg_part, 1 . 0 x $beg_part_len, 'NO_REDUCE', $big);
802 2 $frac2 = Math::FractionManip->new('0' . $pat, 9 x $pat_len . 0 x $beg_part_len, 'NO_REDUCE', $big);
803 2 $frac3 = $frac1 + $frac2;
804 2 my $what_i_get = $frac3->decimal;
805 2 my $places = length($what_i_get);
806 2 ▶ my $decimal_p_tmp = $decimal_part2 if not $big;
Branch: 1 branch
BranchTrueFalseTotal
unless $big 2 0 2
807 2 ▶ $decimal_p_tmp = Math::BigFloat->new($decimal_part2) if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 2 2
808 2 my $what_i_should_get = (($decimal_p_tmp) / $factor2) . "$pat" x ($places);
809  
810 # The rest of this is doing nothing more but trying to compare
811 # the what_i_get and what_i_should_get but becuse the stupid
812 # BigFloat module is so pragmentic all this hopla is nessary
813 2 ▶ $what_i_should_get = Math::BigFloat->new($what_i_should_get) if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 2 2
814 2 ▶ $what_i_should_get = $what_i_should_get->fround(length($what_i_get) - 1) if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 2 2
815 2 ▶ $what_i_should_get = Math::BigFloat->new($what_i_should_get) if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 2 2
816 # ^^ Needed because the fround method does not return a BigFloat object!
817  
818 2 ▶ my $pass = "$what_i_get" eq "$what_i_should_get" if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 2 2
819 2 ▶ $pass = $what_i_get == $what_i_should_get if not $big;
Branch: 1 branch
BranchTrueFalseTotal
unless $big 2 0 2
820 2 ▶ $repeat = 1, last OuterBlock if ($pass);
Branch: 1 branch
BranchTrueFalseTotal
if $pass 2 0 2
821 }
822 }
823 }
824 }
825 0 $beg_part_len++;
826 }
827 13 ▶ if ($repeat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repeat) { } 4 9 13
828 4 $frac1 = Math::FractionManip->new('0' . $beg_part, 1 . 0 x $beg_part_len, $big);
829 4 $frac2 = Math::FractionManip->new('0' . $pat, 9 x $pat_len . 0 x $beg_part_len, $big);
830 4 ▶ $int_part = Math::FractionManip->new('0' . $int_part, 1, 'BIG') if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 4 4
831 4 $frac3 = $sign * ($int_part + $frac1 + $frac2);
832 4 return @{ $frac3->{frac} };
833 }
834 else {
835 9 return ($decimal * $factor, $factor, $big);
836 }
837 0 $Math::BigFloat::rnd_mode = $rnd_mode; # set it back to what it was.
838 }
839  
840 2 1;
841  
842 __END__
↑ Summary