blib/lib/Math/SlideRule.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 96.0% 48 / 50 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 98.5% 203 / 206 SCAR 35.3
CC
34
Cov
98%
divide
9.0
multiply
9.0
standard_form
8.0
CRAP 34.0
« blib/lib/Math/SlideRule/ConfigData.pm ↑ Summary Build »
1 # -*- Perl -*-
2 #
3 # slide rule virtualization for Perl
4  
5 package Math::SlideRule;
6  
7 2 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '1.11';
14  
15 ########################################################################
16 #
17 # ATTRIBUTES
18  
19 # these are taken from common scale names on a slide rule; see code for
20 # how they are populated
21 2 has A => ( is => 'lazy', );
22 2 has C => ( is => 'lazy', );
23  
24 1 ▶sub _build_A { $_[0]->_range_exp_weighted( 1, 100 ) }
Subroutine _build_A: called CC=1 SCAR=0.0
25 1 ▶sub _build_C { $_[0]->_range_exp_weighted( 1, 10 ) }
Subroutine _build_C: called CC=1 SCAR=0.0
26  
27 # increased precision comes at the cost of additional memory use
28 #
29 # NOTE changing the precision after A, C and so forth have been
30 # generated will do nothing to those values. instead, construct a new
31 # object with a different precision set, if necessary
32 2 ▶has precision => ( is => 'rw', default => sub { 10_000 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 ########################################################################
35 #
36 # METHODS
37  
38 # builds two arrays, one of values (1, 2, 3...), another of distances
39 # based on the log of those values. these arrays returned in a hash
40 # reference. slide rule lookups obtain the index of a value, then use
41 # that to find the distance of that value, then uses other distances
42 # to figure out some new location, that a new value can be worked back
43 # out from
44 #
45 # NOTE that these scales are not calibrated directly to one another
46 # as they would be on a slide rule
47 sub _range_exp_weighted {
48 2 ▶ my ( $self, $min, $max ) = @_;
Subroutine _range_exp_weighted: called CC=2 SCAR=6.9
49  
50 2 my @range = map log, $min, $max;
51 2 my ( @values, @distances );
52  
53 2 my $slope = ( $range[1] - $range[0] ) / $self->precision;
54  
55 2 for my $d ( 0 .. $self->precision ) {
56 # via slope equation; y = mx + b and m = (y2-y1)/(x2-x1) with
57 # assumption that precision 0..$mp and @range[min,max]
58 20002 push @distances, $slope * $d + $range[0];
59 20002 push @values, exp $distances[-1];
60 }
61  
62 2 return { value => \@values, dist => \@distances };
63 }
64  
65 # binary search an array of values for a given value, returning index of
66 # the closest match. used to lookup values and their corresponding
67 # distances from the various A, C, etc. attribute tables. NOTE this
68 # routine assumes that the given value has been normalized e.g. via
69 # standard_form to lie somewhere on or between the minimum and maximum
70 # values in the given array reference
71 sub _rank {
72 95 ▶ my ( $self, $value, $ref ) = @_;
Subroutine _rank: called CC=6 SCAR=18.0
73  
74 95 my $lo = 0;
75 95 my $hi = $#$ref;
76  
77 95 ▶ while ( $lo <= $hi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo <= $hi) 999 48 1047
78 999 my $mid = int( $lo + ( $hi - $lo ) / 2 );
79 999 ▶ if ( $ref->[$mid] > $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->[$mid] > $value) { } 477 522 999
elsif ($ref->[$mid] < $value) { } 475 47 522
80 477 $hi = $mid - 1;
81 } elsif ( $ref->[$mid] < $value ) {
82 475 $lo = $mid + 1;
83 } else {
84 47 return $mid;
85 }
86 }
87  
88 # no exact match; return index of value closest to the numeral supplied
89 48 ▶ if ( $lo > $#$ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo > $ref->$#*) { } 0 48 48
90 0 return $hi;
91 } else {
92 48 ▶ if ( abs( $ref->[$lo] - $value ) >= abs( $ref->[$hi] - $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $ref->[$lo] - $value >= abs $ref->[$hi] - $value) { } 26 22 48
93 26 return $hi;
94 } else {
95 22 return $lo;
96 }
97 }
98 }
99  
100 # division is just multiplication done backwards on a slide rule, as the
101 # same physical distances are involved. there are also "CF" and "CI" (C
102 # scale, folded, or inverse) and so forth scales to assist with such
103 # operations, though these mostly just help avoid excess motions on the
104 # slide rule
105 #
106 # NOTE cannot just pass m*(1/n) to multiply() because that looses
107 # precision: .82 for 75/92 while can get .815 on pocket slide rule
108 sub divide {
109 10 ▶ my $self = shift;
Subroutine divide: called CC=9 SCAR=22.0
Pod documented
110 10 my $n = shift;
111 10 my $i = 0;
112  
113 10 ▶ die "need at least two numbers\n" if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 1 9 10
114 9 ▶ die "argument index $i not a number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and &looks_like_number($n) 2 7 9
Condition100%3/3
defined $n and &looks_like_number($n)
!ll&&!rl&&r
117
MC/DC100%2/2
defined $n and &looks_like_number($n)
defined $n &looks_like_number($n)
Truth table100%3/3
A and B
A= defined $nB= &looks_like_number($n)
ABresult
0X0
100
111
115 unless defined $n and looks_like_number($n);
116  
117 7 my ( $n_coe, $n_exp, $neg_count ) = $self->standard_form($n);
118  
119 7 my $n_idx = $self->_rank( $n_coe, $self->C->{value} );
120 7 my $distance = $self->C->{dist}[$n_idx];
121 7 my $exponent = $n_exp;
122  
123 7 for my $m (@_) {
124 10 $i++;
125 10 ▶ die "argument index $i not a number\n" if !looks_like_number($m);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($m) 2 8 10
126  
127 8 ▶ $neg_count++ if $m < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m < 0 3 5 8
128  
129 8 my ( $m_coe, $m_exp, undef ) = $self->standard_form($m);
130 8 my $m_idx = $self->_rank( $m_coe, $self->C->{value} );
131  
132 8 $distance -= $self->C->{dist}[$m_idx];
133 8 $exponent -= $m_exp;
134  
135 8 ▶ if ( $distance < $self->C->{dist}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance < $self->C->{"dist"}[0]) 8 0 8
136 8 $distance = $self->C->{dist}[-1] + $distance;
137 8 $exponent--;
138 }
139 }
140  
141 5 my $d_idx = $self->_rank( $distance, $self->C->{dist} );
142 5 my $product = $self->C->{value}[$d_idx];
143  
144 5 $product *= 10**$exponent;
145 5 ▶ $product *= -1 if $neg_count % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $neg_count % 2 == 1 1 4 5
146  
147 5 return $product;
148 }
149  
150 sub multiply {
151 21 ▶ my $self = shift;
Subroutine multiply: called CC=9 SCAR=22.0
Pod documented
152 21 my $n = shift;
153 21 my $i = 0;
154  
155 21 ▶ die "need at least two numbers\n" if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 1 20 21
156 20 ▶ die "argument index $i not a number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and &looks_like_number($n) 2 18 20
Condition100%3/3
defined $n and &looks_like_number($n)
!ll&&!rl&&r
1118
MC/DC100%2/2
defined $n and &looks_like_number($n)
defined $n &looks_like_number($n)
Truth table100%3/3
A and B
A= defined $nB= &looks_like_number($n)
ABresult
0X0
100
111
157 unless defined $n and looks_like_number($n);
158  
159 18 my ( $n_coe, $n_exp, $neg_count ) = $self->standard_form($n);
160  
161 # chain method has first lookup on D and then subsequent done by
162 # moving C on slider and keeping tabs with the hairline, then reading
163 # back on D for the final result. (plus incrementing the exponent
164 # count when a reverse slide is necessary, for example for 3.4*4.1, as
165 # that jumps to the next magnitude)
166 #
167 # one can also do the multiplication on the A and B scales, which is
168 # handy if you then need to pull the square root off of D. but this
169 # implementation ignores such alternatives
170 18 my $n_idx = $self->_rank( $n_coe, $self->C->{value} );
171 18 my $distance = $self->C->{dist}[$n_idx];
172 18 my $exponent = $n_exp;
173  
174 18 for my $m (@_) {
175 26 $i++;
176 26 ▶ die "argument index $i not a number\n" if !looks_like_number($m);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($m) 2 24 26
177  
178 24 ▶ $neg_count++ if $m < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m < 0 10 14 24
179  
180 24 my ( $m_coe, $m_exp, undef ) = $self->standard_form($m);
181 24 my $m_idx = $self->_rank( $m_coe, $self->C->{value} );
182  
183 24 $distance += $self->C->{dist}[$m_idx];
184 24 $exponent += $m_exp;
185  
186 # order of magnitude change, adjust back to bounds (these are
187 # notable on a slide rule by having to index from the opposite
188 # direction than usual for the C and D scales (though one could
189 # also obtain the value with the A and B or the CI and DI
190 # scales, but those would then need some rule to track the
191 # exponent change))
192 24 ▶ if ( $distance > $self->C->{dist}[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance > $self->C->{"dist"}[-1]) 8 16 24
193 8 $distance -= $self->C->{dist}[-1];
194 8 $exponent++;
195 }
196 }
197  
198 16 my $d_idx = $self->_rank( $distance, $self->C->{dist} );
199 16 my $product = $self->C->{value}[$d_idx];
200  
201 16 $product *= 10**$exponent;
202 16 ▶ $product *= -1 if $neg_count % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $neg_count % 2 == 1 4 12 16
203  
204 16 return $product;
205 }
206  
207 # relies on conversion from A to C scales (and that the distances in
208 # said scales are linked to one another)
209 sub sqrt {
210 9 ▶ my ( $self, $n ) = @_;
Subroutine sqrt: called CC=5 SCAR=16.1
Pod documented
211 9 ▶ die "argument not a number\n" unless defined $n and looks_like_number($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and &looks_like_number($n) 2 7 9
Condition100%3/3
defined $n and &looks_like_number($n)
!ll&&!rl&&r
117
MC/DC100%2/2
defined $n and &looks_like_number($n)
defined $n &looks_like_number($n)
Truth table100%3/3
A and B
A= defined $nB= &looks_like_number($n)
ABresult
0X0
100
111
212 7 ▶ die "Can't take sqrt of $n\n" if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 1 6 7
213  
214 6 my ( $n_coe, $n_exp, undef ) = $self->standard_form($n);
215  
216 6 ▶ if ( $n_exp % 2 == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_exp % 2 == 1) 3 3 6
217 3 $n_coe *= 10;
218 3 $n_exp--;
219 }
220  
221 6 my $n_idx = $self->_rank( $n_coe, $self->A->{value} );
222  
223 # NOTE division is due to A and C scale distances not being calibrated
224 # directly with one another
225 6 my $distance = $self->A->{dist}[$n_idx] / 2;
226  
227 6 my $d_idx = $self->_rank( $distance, $self->C->{dist} );
228 6 my $sqrt = $self->C->{value}[$d_idx];
229  
230 6 $sqrt *= 10**( $n_exp / 2 );
231  
232 6 return $sqrt;
233 }
234  
235 # converts numbers to standard form (scientific notation) or otherwise
236 # between a particular range of numbers (to support A/B "double
237 # decade" scales)
238 sub standard_form {
239 76 ▶ my ( $self, $val, $min, $max ) = @_;
Subroutine standard_form: called CC=8 SCAR=20.8
Pod documented
240  
241 76 ▶ $min //= 1;
Condition100%2/2
$min //= 1
l!l
175
MC/DC100%1/1
$min //= 1
$min
Truth table100%2/2
A
A= $min
Aresult
11
00
242 76 ▶ $max //= 10;
Condition100%2/2
$max //= 10
l!l
175
MC/DC100%1/1
$max //= 10
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
243  
244 76 ▶ my $is_neg = $val < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val < 0 ? : 17 59 76
245  
246 76 $val = abs $val;
247 76 my $exp = 0;
248  
249 76 ▶ if ( $val < $min ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val < $min) { } 9 67 76
elsif ($val >= $max) { } 45 22 67
250 9 ▶ while ( $val < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $min) 17 9 26
251 17 $val *= 10;
252 17 $exp--;
253 }
254 } elsif ( $val >= $max ) {
255 45 ▶ while ( $val >= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= $max) 59 45 104
256 59 $val /= 10;
257 59 $exp++;
258 }
259 }
260  
261 76 return $val, $exp, $is_neg;
262 }
263  
264 2 1;
265 __END__
« blib/lib/Math/SlideRule/ConfigData.pm ↑ Summary Build »