blib/lib/Zuzu/Module/Math.pm

filter: Statementstmt 40.9% 54 / 132 Branchbran 9.6% 6 / 62 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 16 / 40 Podpod 0.0% 0 / 1 totaltotal 31.5% 78 / 247 CC 38 SCAR 49.0
CC
38
Cov
31%
__ANON__
406.0
_parse_radix_string
56.0
_stringify_radix
30.0
CRAP 134.9
« blib/lib/Zuzu/Module/CSV.pm ↑ Summary blib/lib/Zuzu/Module/TUI.pm »
1 package Zuzu::Module::Math;
2  
3 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 our $VERSION = '0.007002';
6  
7 11 ▶use List::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 max
9 min
10 sum
11 11 );
12 11 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Math::Trig qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 acosec
15 acosech
16 acos
17 acosh
18 acotan
19 acotanh
20 asec
21 asech
22 asin
23 asinh
24 atan
25 atanh
26 cosec
27 cosech
28 cosh
29 cotan
30 cotanh
31 pi
32 sec
33 sech
34 sinh
35 tan
36 tanh
37 11 );
38  
39 11 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
40 native_class
41 native_functions
42 11 );
43  
44 4 ▶sub _num {
Subroutine _num: called CC=2 SCAR=7.1
45 4 my ( $value, $default ) = @_;
46  
47 4 ▶ return $default if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
48 4 return 0 + $value;
49 }
50  
51 0 ▶sub _parse_radix_string {
Subroutine _parse_radix_string: not called CC=7 SCAR=40.3
52 0 my ( $value, $base, $name ) = @_;
53  
54 0 ▶ my $raw = defined $value ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
55 0 my $text = $raw;
56 0 ▶ $text =~ s/^0[xX]// if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
57  
58 0 my %pattern = (
59 2 => qr/\A[01]+\z/,
60 8 => qr/\A[0-7]+\z/,
61 10 => qr/\A[0-9]+\z/,
62 16 => qr/\A[0-9A-Fa-f]+\z/,
63 );
64  
65 die "TypeException: $name expects a base-$base numeric string"
66 0 ▶ if $text !~ $pattern{$base};
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /$pattern{$base}/ 0 0 0
67  
68 0 ▶ if ( $base == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 2) 0 0 0
69 0 return Math::BigInt->from_bin( '0b' . $text );
70 }
71 0 ▶ if ( $base == 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 8) 0 0 0
72 0 return Math::BigInt->from_oct( '0' . $text );
73 }
74 0 ▶ if ( $base == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 10) 0 0 0
75 0 return Math::BigInt->new($text);
76 }
77 0 return Math::BigInt->from_hex( '0x' . $text );
78 }
79  
80 0 ▶sub _stringify_radix {
Subroutine _stringify_radix: not called CC=5 SCAR=34.0
81 0 my ( $num, $base ) = @_;
82  
83 0 ▶ my $out = substr( $num->as_bin(), 2 )
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
84 if $base == 2;
85 0 ▶ $out = substr( $num->as_oct(), 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
86 if $base == 8;
87 0 ▶ $out = $num->bstr()
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
88 if $base == 10;
89 0 ▶ $out = substr( $num->as_hex(), 2 )
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
90 if $base == 16;
91  
92 0 return pack 'A*', $out;
93 }
94  
95 60 ▶sub IMPORT {
Subroutine IMPORT: called CC=4 SCAR=22.7
Pod undocumented
96 60 my ( $class, $runtime ) = @_;
97  
98 60 my $math_class = native_class(
99 name => 'Math',
100 );
101  
102 60 my @trig_names = qw(
103 sin cos tan
104 cosec sec cotan
105 asin acos atan atan2 acosec asec acotan
106 sinh cosh tanh
107 cosech sech cotanh
108 asinh acosh atanh acosech asech acotanh
109 );
110 my %trig_impl = (
111 1 ▶ sin => sub { CORE::sin( _num( $_[0], 0 ) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 0 ▶ cos => sub { CORE::cos( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 ▶ tan => sub { tan( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 1 ▶ cosec => sub { cosec( _num( $_[0], 0 ) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 ▶ sec => sub { sec( _num( $_[0], 0 ) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 1 ▶ cotan => sub { cotan( _num( $_[0], 0 ) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 0 ▶ asin => sub { asin( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 ▶ acos => sub { acos( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ atan => sub { atan( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 ▶ atan2 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 my ( $y, $x ) = @_;
122 0 return CORE::atan2( _num( $y, 0 ), _num( $x, 1 ) );
123 },
124 0 ▶ acosec => sub { acosec( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ asec => sub { asec( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ acotan => sub { acotan( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 ▶ sinh => sub { sinh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 ▶ cosh => sub { cosh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 ▶ tanh => sub { tanh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ cosech => sub { cosech( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ sech => sub { sech( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ cotanh => sub { cotanh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ asinh => sub { asinh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ acosh => sub { acosh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ atanh => sub { atanh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 ▶ acosech => sub { acosech( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 ▶ asech => sub { asech( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 ▶ acotanh => sub { acotanh( _num( $_[0], 0 ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 60 );
140 my $trig = native_functions(
141 names => \@trig_names,
142 1500 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 1500 my ( $name ) = @_;
144 1500 my $impl = $trig_impl{$name};
145 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 4 my ( $self, @args ) = @_;
147 4 return $impl->( @args );
148 1500 };
149 },
150 60 );
151  
152 my $extras = native_functions(
153 names => [
154 qw(
155 pi rand exp log log10 pow min max sum
156 clamp hypot deg2rad rad2deg
157 )
158 ],
159 780 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=4.8
160 780 my ( $name ) = @_;
161 6 ▶ return sub {
Subroutine __ANON__: called CC=24 SCAR=60.1
162 6 my ( $self, @args ) = @_;
163
164 6 ▶ if ( @args == 1 and $name =~ /^(?:min|max|sum)$/ and Scalar::Util::blessed($args[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and $name =~ /^(?:min|max|sum)$/ and builtin::blessed($args[0])) 0 6 6
Condition33%2/6
@args == 1 and $name =~ /^(?:min|max|sum)$/ and builtin::blessed($args[0])
!ll&&!rl&&r
600
@args == 1 and $name =~ /^(?:min|max|sum)$/
!ll&&!rl&&r
600
MC/DC0%0/3
@args == 1 and $name =~ /^(?:min|max|sum)$/ and builtin::blessed($args[0])
@args == 1 $name =~ /^(?:min|max|sum)$/ builtin::blessed($args[0])
Truth table25%1/4
A and B and C
A= @args == 1B= $name =~ /^(?:min|max|sum)$/C= builtin::blessed($args[0])
ABCresult
0XX0
10X0
1100
1111
165 0 @args = @{ $args[0]->items };
166 }
167
168 6 ▶ return pi if $name eq 'pi';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "pi" 5 1 6
169 1 ▶ return @args ? rand( _num( $args[0], 1 ) ) : rand()
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "rand" 1 0 1
@args ? : 0 1 1
170 if $name eq 'rand';
171 0 ▶ return exp( _num( $args[0], 0 ) )
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "exp" 0 0 0
172 if $name eq 'exp';
173 0 ▶ if ( $name eq 'log' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "log") 0 0 0
174 0 my $value = _num( $args[0], 1 );
175 0 ▶ return log($value) if @args < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args < 2 0 0 0
176 0 my $base = _num( $args[1], 10 );
177 0 return log($value) / log($base);
178 }
179 0 ▶ return log( _num( $args[0], 1 ) ) / log(10)
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "log10" 0 0 0
180 if $name eq 'log10';
181 0 ▶ return _num( $args[0], 1 ) ** _num( $args[1], 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "pow" 0 0 0
182 if $name eq 'pow';
183 0 ▶ if ( $name eq 'min' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "min") 0 0 0
184 0 ▶ return 0 if not @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
185 0 return min( map { _num( $_, 0 ) } @args );
186 }
187 0 ▶ if ( $name eq 'max' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "max") 0 0 0
188 0 ▶ return 0 if not @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
189 0 return max( map { _num( $_, 0 ) } @args );
190 }
191 0 ▶ return sum( map { _num( $_, 0 ) } @args ) // 0
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "sum" 0 0 0
Condition0%0/2
&sum(map({_num($_, 0);} @args)) // 0
l!l
00
MC/DC0%0/1
&sum(map({_num($_, 0);} @args)) // 0
&sum(map({_num($_, 0);} @args))
Truth table0%0/2
A
A= &sum(map({_num($_, 0);} @args))
Aresult
11
00
192 if $name eq 'sum';
193 0 ▶ if ( $name eq 'clamp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "clamp") 0 0 0
194 0 my $value = _num( $args[0], 0 );
195 0 my $low = _num( $args[1], 0 );
196 0 my $high = _num( $args[2], 0 );
197 0 ▶ return $low if $value < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $low 0 0 0
198 0 ▶ return $high if $value > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $high 0 0 0
199 0 return $value;
200 }
201 0 ▶ if ( $name eq 'hypot' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "hypot") 0 0 0
202 0 my $x = _num( $args[0], 0 );
203 0 my $y = _num( $args[1], 0 );
204 0 return sqrt( $x * $x + $y * $y );
205 }
206 0 ▶ return _num( $args[0], 0 ) * pi / 180
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "deg2rad" 0 0 0
207 if $name eq 'deg2rad';
208 0 ▶ return _num( $args[0], 0 ) * 180 / pi
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "rad2deg" 0 0 0
209 if $name eq 'rad2deg';
210  
211 0 return undef;
212 780 };
213 },
214 60 );
215  
216 60 my %radix_source_base = (
217 hex => 16,
218 dec => 10,
219 oct => 8,
220 bin => 2,
221 );
222 60 my @radix_names = qw(
223 hex2dec hex2oct hex2bin
224 dec2hex dec2oct dec2bin
225 oct2hex oct2dec oct2bin
226 bin2hex bin2dec bin2oct
227 );
228 my $radix = native_functions(
229 names => \@radix_names,
230 720 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
231 720 my ( $name ) = @_;
232 720 my ( $from, $to ) = split /2/, $name, 2;
233 720 my $from_base = $radix_source_base{$from};
234 720 my $to_base = $radix_source_base{$to};
235 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 0 my ( $self, @args ) = @_;
237 0 my $num = _parse_radix_string( $args[0], $from_base, $name );
238 0 return _stringify_radix( $num, $to_base );
239 720 };
240 },
241 60 );
242  
243 $math_class->static_methods->{$_} = $trig->{$_}
244 60 for @trig_names;
245 $math_class->static_methods->{$_} = $extras->{$_}
246 60 for CORE::keys %{ $extras };
247 $math_class->static_methods->{$_} = $radix->{$_}
248 60 for @radix_names;
249  
250 return {
251 60 Math => $math_class,
252 π => pi,
253 };
254 }
255  
256 11 1;
257  
258 =pod
259  
260 =head1 NAME
261  
262 Zuzu::Module::Math - std/math bindings for ZuzuScript.
263  
264 =head1 DESCRIPTION
265  
266 Implements the C<std/math> module and exports:
267  
268 =over
269  
270 =item * C<Math> class with static mathematical helpers
271  
272 =item * C<π> constant aliasing C<pi>
273  
274 =back
275  
276 The C<Math> class provides trigonometric, hyperbolic, logarithmic, and
277 utility functions such as C<pow>, C<min>, C<max>, C<sum>, C<clamp>,
278 C<hypot>, C<deg2rad>, and C<rad2deg>. It also provides radix conversion
279 helpers C<hex2dec>, C<hex2oct>, C<hex2bin>, C<dec2hex>, C<dec2oct>,
280 C<dec2bin>, C<oct2hex>, C<oct2dec>, C<oct2bin>, C<bin2hex>, C<bin2dec>,
281 and C<bin2oct>. Hexadecimal parsing accepts an optional C<0x> or C<0X>
282 prefix.
283  
284 =head1 COPYRIGHT AND LICENCE
285  
286 B<< Zuzu::Module::Math >> is copyright Toby Inkster.
287  
288 It is free software; you may redistribute it and/or modify it under
289 the terms of either the Artistic License 1.0 or the GNU General Public
290 License version 2.
291  
292 =cut
« blib/lib/Zuzu/Module/CSV.pm ↑ Summary blib/lib/Zuzu/Module/TUI.pm »