Units/Convert/Base.pm

filter: Statementstmt 96.0% 96 / 100 Branchbran 87.0% 47 / 54 Conditioncond 78.5% 11 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 18 totaltotal 82.7% 178 / 215 CC 34 SCAR 16.0
CC
34
Cov
92%
simple_convert
8.1
spread
8.0
range_score
7.0
CRAP 4.9
« Units/Rank.pm ↑ Summary Units/Convert/Multi.pm »
1 package Math::Calc::Units::Convert::Base;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶sub major_pref {
Subroutine major_pref: called CC=1 SCAR=0.0
Pod undocumented
5 5 return 0;
6 }
7  
8 # major_variants :void -> ( unit name )
9 #
10 # Return the set of prefix-free variants of the class that you might
11 # want to use as a final result. So for time, this would return
12 # "second" and "year" but not "millisecond" or "gigayear".
13 #
14 # Only this base class will ever use the unit passed in, and that's
15 # because this base class is used for unknown units. Subclasses should
16 # return a list of variants regardless of what is passed as $unit.
17 #
18 2 ▶sub major_variants {
Subroutine major_variants: called CC=1 SCAR=0.0
Pod undocumented
19 2 my ($self, $unit) = @_;
20 2 return $unit;
21 }
22  
23 # singular : unitName -> unitName
24 #
25 # Convert a possibly pluralized unit name to the uninflected singular
26 # form of the same name.
27 #
28 # Example: inches -> inch
29 # Example: inch -> inch
30 #
31 # I suppose I ought to optionally allow Lingu::EN::Inflect or whatever
32 # it's called.
33 #
34 251 ▶sub singular {
Subroutine singular: called CC=5 SCAR=16.1
Pod undocumented
35 251 my $self = shift;
36 251 local $_ = shift;
37  
38 251 ▶ return $_ unless /s$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /s$/ 200 51 251
39 51 ▶ return $1 if /^(.*[^e])s$/; # doesn't end in es => just chop off the s
Branch: 1 branch
BranchTrueFalseTotal
if /^(.*[^e])s$/ 27 24 51
40 24 ▶ return $1 if /^(.*(ch|sh))es$/; # eg inches -> inch
Branch: 1 branch
BranchTrueFalseTotal
if /^(.*(ch|sh))es$/ 5 19 24
41 19 ▶ return $1 if /^(.*[aeiou][^aeiou]e)s$/; # scales -> scale
Branch: 1 branch
BranchTrueFalseTotal
if /^(.*[aeiou][^aeiou]e)s$/ 2 17 19
42 17 chop; return $_; # Chop off the s
43 }
44  
45 38 ▶sub unit_map {
Subroutine unit_map: called CC=1 SCAR=0.0
Pod undocumented
46 38 return {};
47 }
48  
49 17 ▶sub variants {
Subroutine variants: called CC=1 SCAR=0.0
Pod undocumented
50 17 my ($self, $base) = @_;
51 17 my $map = $self->unit_map();
52 17 return ($base, keys %$map);
53 }
54  
55 # same : unit x unit -> boolean
56 #
57 # Returns whether the two units match, where a unit is a string (eg
58 # "week") and a power. So "days" and "toothpicks" are not the same,
59 # nor are feet and square feet.
60 26 ▶sub same {
Subroutine same: called CC=5 SCAR=16.2
Pod undocumented
61 26 my ($self, $u, $v) = @_;
62 26 ▶ return 0 if keys %$u != keys %$v;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$u != keys %$v 0 26 26
63 26 ▶ while (my ($name, $power) = each %$u) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$u) 27 14 41
64 27 ▶ return 0 if ! exists $v->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $v->{$name} 12 15 27
65 15 ▶ return 0 if $v->{$name} != $power;
Branch: 1 branch
BranchTrueFalseTotal
if $v->{$name} != $power 0 15 15
66 }
67 14 return 1;
68 }
69  
70 # simple_convert : unitName x unitName -> multiplier
71 #
72 # Second unit name must be canonical.
73 #
74 665 ▶sub simple_convert {
Subroutine simple_convert: called CC=8 SCAR=20.9
Pod undocumented
75 665 my ($self, $from, $to) = @_;
76 665 ▶ return 1 if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 214 451 665
77  
78 451 my $map = $self->unit_map();
79 451 ▶ my $w = $map->{$from} || $map->{lc($from)};
Condition66%2/3
$map->{$from} || $$map{lc $from}
l!l&&r!l&&!r
2620189
MC/DC50%1/2
$map->{$from} || $$map{lc $from}
$map->{$from} $$map{lc $from}
Truth table66%2/3
A || B
A= $map->{$from}B= $$map{lc $from}
ABresult
1X1
011
000
80 451 ▶ if (! $w) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($w) 189 262 451
81 189 $from = $self->singular($from);
82 189 ▶ $w = $map->{$from} || $map->{lc($from)};
Condition66%2/3
$map->{$from} || $$map{lc $from}
l!l&&r!l&&!r
40185
MC/DC50%1/2
$map->{$from} || $$map{lc $from}
$map->{$from} $$map{lc $from}
Truth table66%2/3
A || B
A= $map->{$from}B= $$map{lc $from}
ABresult
1X1
011
000
83 }
84 451 ▶ return if ! $w; # Failed
Branch: 1 branch
BranchTrueFalseTotal
unless $w 185 266 451
85  
86 # We might have only gotten one step closer (hour -> minute -> sec)
87 266 ▶ if ($w->[1] ne $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->[1] ne $to) { } 99 167 266
88 99 my $submult = $self->simple_convert($w->[1], $to);
89 99 ▶ return if ! defined $submult;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $submult 0 99 99
90 99 return $w->[0] * $submult;
91 } else {
92 167 return $w->[0];
93 }
94 }
95  
96 # to_canonical : unitName -> amount x unitName
97 #
98 # Convert the given unit to the canonical unit for this class, along
99 # with a conversion factor.
100 #
101 223 ▶sub to_canonical {
Subroutine to_canonical: called CC=3 SCAR=11.0
Pod undocumented
102 223 my ($self, $unitName) = @_;
103 223 my $canon = $self->canonical_unit();
104 223 ▶ if ($canon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($canon) { } 210 13 223
105 210 my $mult = $self->simple_convert($unitName, $canon);
106 210 ▶ return if ! defined $mult;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mult 127 83 210
107 83 return ($mult, $canon);
108 } else {
109 13 return (1, $self->singular($unitName));
110 }
111 }
112  
113 # canonical_unit : void -> unit name
114 #
115 # Return the canonical unit for this class.
116 #
117 13 ▶sub canonical_unit {
Subroutine canonical_unit: called CC=1 SCAR=0.0
Pod undocumented
118 13 return;
119 }
120  
121 0 ▶sub abbreviated_canonical_unit {
Subroutine abbreviated_canonical_unit: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my ($self) = @_;
123 0 return $self->canonical_unit;
124 }
125  
126 #################### RANKING, SCORING, DISPLAYING ##################
127  
128 # spread : magnitude x base unit x units to spread over
129 # -> ( <mag,unit> )
130 #
131 # @$units MUST BE SORTED, LARGER UNITS FIRST!
132 #
133 1 my $THRESHOLD = 0.01;
134 13 ▶sub spread {
Subroutine spread: called CC=8 SCAR=20.8
Pod undocumented
135 13 my ($self, $mag, $base, $start, $units) = @_;
136 13 ▶ die if $mag < 0; # Must be given a positive value!
Branch: 1 branch
BranchTrueFalseTotal
if $mag < 0 0 13 13
137 13 ▶ return [ 0, $base ] if $mag == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mag == 0 0 13 13
138  
139 13 my $orig = $mag;
140  
141 13 my @desc;
142 13 my $started = 0;
143 13 foreach my $unit (@$units) {
144 86 ▶ $started = 1 if $unit eq $start;
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq $start 12 74 86
145 86 ▶ next unless $started;
Branch: 1 branch
BranchTrueFalseTotal
unless $started 55 31 86
146  
147 31 ▶ last if ($mag / $orig) < $THRESHOLD;
Branch: 1 branch
BranchTrueFalseTotal
if $mag / $orig < $THRESHOLD 12 19 31
148 19 my $mult = $self->simple_convert($unit, $base);
149 19 my $n = int($mag / $mult);
150 19 ▶ next if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 2 17 19
151 17 $mag -= $n * $mult;
152 17 push @desc, [ $n, $unit ];
153 }
154  
155 13 return @desc;
156 }
157  
158 # range_score : amount x unitName -> score
159 #
160 # Returns 1 if the value is in range for the unit, 0.1 if the value is
161 # infinitely close to being in range, and decaying to 0.001 as the
162 # value approaches infinitely far away from the range.
163 #
164 # For the outside of range values, I convert to log space (so 1/400 is
165 # just as far away from 1 as 400 is). I then treat the allowed range
166 # as a one standard deviation wide segment of a normal distribution,
167 # and use appropriate modifiers to make the result range from 0.001 to
168 # 0.1.
169 #
170 # The above formula was carefully chosen from thousands of
171 # possibilities, by picking things at random and scribbling them down
172 # on a piece of paper, then pouring sparkling apple cider all over and
173 # using the one that was still readable.
174 #
175 # Ok, not really. Just pretend that I went to that much trouble.
176 #
177 282 ▶sub range_score {
Subroutine range_score: called CC=7 SCAR=19.5
Pod undocumented
178 282 my ($self, $val, $unitName) = @_;
179 282 my $ranges = $self->get_ranges();
180 282 ▶ my $range = $ranges->{$unitName} || $ranges->{default};
Condition66%2/3
$ranges->{$unitName} || $ranges->{'default'}
l!l&&r!l&&!r
212610
MC/DC0%0/2
$ranges->{$unitName} || $ranges->{'default'}
$ranges->{$unitName} $ranges->{'default'}
Truth table66%2/3
A || B
A= $ranges->{$unitName}B= $ranges->{'default'}
ABresult
1X1
011
000
181  
182 # Return 1 if it's in range
183 282 ▶ if ($val >= $range->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= $range->[0]) 142 140 282
184 142 ▶ if (! defined $range->[1] || ($val <= $range->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $range->[1] or $val <= $range->[1]) 39 103 142
Condition100%3/3
not defined $range->[1] or $val <= $range->[1]
l!l&&r!l&&!r
237103
MC/DC100%2/2
not defined $range->[1] or $val <= $range->[1]
not defined $range->[1] $val <= $range->[1]
Truth table100%3/3
not(A) or B
A= not defined $range->[1]B= $val <= $range->[1]
ABresult
1X1
011
000
185 39 return 1;
186 }
187 }
188  
189 243 $val = _sillylog($val);
190  
191 243 my $r0 = _sillylog($range->[0]);
192 243 my $r1;
193 243 ▶ if (defined $range->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range->[1]) { } 240 3 243
194 240 $r1 = _sillylog($range->[1]);
195 } else {
196 3 $r1 = 4;
197 }
198  
199 243 my $width = $r1 - $r0;
200 243 my $mean = ($r0 + $r1) / 2;
201 243 my $stddev = $width / 2;
202  
203 243 my $n = ($val - $mean) / $stddev; # Normalized value
204  
205 243 our $mulconst;
206 243 ▶ $mulconst ||= 0.999 * exp(1/8);
Condition100%2/2
$Math::Calc::Units::Convert::Base::mulconst ||= "1.13201530461376"
l!l
2421
MC/DC100%1/1
$Math::Calc::Units::Convert::Base::mulconst ||= "1.13201530461376"
$Math::Calc::Units::Convert::Base::mulconst
Truth table100%2/2
A
A= $Math::Calc::Units::Convert::Base::mulconst
Aresult
11
00
207  
208 243 return 0.001 + $mulconst * exp(-$n**2/2);
209 }
210  
211 # Infinity-free logarithm
212 726 ▶sub _sillylog {
Subroutine _sillylog: called CC=2 SCAR=8.1
213 726 my $x = shift;
214 726 ▶ return log($x) if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 726 0 726
215 0 return log(1e-50);
216 }
217  
218 # pref_score : unitName -> score
219 #
220 # Maps a unit name (eg week) to a score. Higher scores are more likely
221 # to be chosen.
222 296 ▶sub pref_score {
Subroutine pref_score: called CC=2 SCAR=6.9
Pod undocumented
223 296 my ($self, $unitName) = @_;
224 296 my $prefs = $self->get_prefs();
225 296 my $specific = $prefs->{$unitName};
226 296 ▶ return defined($specific) ? $specific : $prefs->{default};
Branch: 1 branch
BranchTrueFalseTotal
defined $specific ? : 162 134 296
227 }
228  
229 # get_prefs : void -> { unit name => score }
230 #
231 # Return a map of unit names to their score, higher scores meaning
232 # they're more likely to be chosen.
233 4 ▶sub get_prefs {
Subroutine get_prefs: called CC=1 SCAR=0.0
Pod undocumented
234 4 return { default => 0.1 };
235 }
236  
237 2 ▶sub get_ranges {
Subroutine get_ranges: called CC=1 SCAR=0.0
Pod undocumented
238 2 return { default => [ 1, undef ] };
239 }
240  
241 # render_unit : unit name x power -> descriptive string
242 #
243 # Return a rendering of the given unit name and a power to raise the
244 # unit to.
245 #
246 # Example: render_unit("weeks", 2) produces "weeks**2".
247 #
248 66 ▶sub render_unit {
Subroutine render_unit: called CC=2 SCAR=7.6
Pod undocumented
249 66 my ($self, $name, $power, $options) = @_;
250 66 ▶ if ($power == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power == 1) { } 66 0 66
251 66 return $name;
252 } else {
253 0 return "$name**$power";
254 }
255 }
256  
257 # render : value x name x power -> descriptive string
258 #
259 # Return a rendering of the given value with the given units.
260 #
261 # Example: render(4.8, "weeks", -1) produces "4.8 weeks**-1".
262 #
263 49 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
264 49 my ($self, $val, $name, $power, $options) = @_;
265 49 return sprintf("%.5g ",$val).$self->render_unit($name, $power, $options);
266 }
267  
268 14 ▶sub construct {
Subroutine construct: called CC=1 SCAR=0.0
Pod undocumented
269 14 return;
270 }
271  
272 1 1;
« Units/Rank.pm ↑ Summary Units/Convert/Multi.pm »