JE/Object/Math.pm

filter: Statementstmt 46.8% 52 / 111 Branchbran 1.3% 1 / 72 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 32.0% 8 / 25 Podpod 100% 2 / 2 totaltotal 24.3% 63 / 259 CC 49 SCAR 41.9
CC
49
Cov
25%
__ANON__
240.0
__ANON__
56.0
__ANON__
42.0
CRAP 65.9
« JE/Code.pm ↑ Summary JE/Parser.pm »
1 package JE::Object::Math;
2  
3 2 our $VERSION = '0.066';
4  
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use constant inf => 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use constant nan => sin 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use POSIX qw'floor ceil';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our @ISA = 'JE::Object';
15  
16 2 require JE::Number;
17 2 require JE::Object;
18 2 require JE::Object::Function;
19  
20  
21  
22 =head1 NAME
23  
24 JE::Object::Math - JavaScript Math object
25  
26 =head1 SYNOPSIS
27  
28 use JE;
29 use JE::Object::Math;
30  
31 $j = new JE;
32  
33 $math_obj = new JE::Object::Math $j;
34  
35 =head1 DESCRIPTION
36  
37 This class implements the JavaScript Math object.
38  
39 =head1 METHODS
40  
41 See L<JE::Types> for descriptions of most of the methods. Only what
42 is specific to JE::Object::Math is explained here.
43  
44 =over
45  
46 =item JE::Object::Math->new($global_obj)
47  
48 Creates a new Math object.
49  
50 =cut
51  
52 2 ▶sub new {
Subroutine new: called CC=1 SCAR=4.6
Pod documented
53 2 my($class, $global) = @_;
54 2 my $self = $class->SUPER::new($global);
55  
56 2 $self->prop({
57 name => 'E',
58 value => JE::Number->new($global, exp 1),
59 dontenum => 1,
60 dontdel => 1,
61 readonly => 1,
62 });
63 2 $self->prop({
64 name => 'LN10',
65 value => JE::Number->new($global, log 10),
66 dontenum => 1,
67 dontdel => 1,
68 readonly => 1,
69 });
70 2 $self->prop({
71 name => 'LN2',
72 value => JE::Number->new($global, log 2),
73 dontenum => 1,
74 dontdel => 1,
75 readonly => 1,
76 });
77 2 $self->prop({
78 name => 'LOG2E',
79 value => JE::Number->new($global, 1/log 2),
80 dontenum => 1,
81 dontdel => 1,
82 readonly => 1,
83 });
84 2 $self->prop({
85 name => 'LOG10E',
86 value => JE::Number->new($global, 1/log 10),
87 dontenum => 1,
88 dontdel => 1,
89 readonly => 1,
90 });
91 2 $self->prop({
92 name => 'PI',
93 value => JE::Number->new($global, 4 * atan2 1,1),
94 dontenum => 1,
95 dontdel => 1,
96 readonly => 1,
97 });
98 2 $self->prop({
99 name => 'SQRT1_2',
100 value => JE::Number->new($global, .5**.5),
101 dontenum => 1,
102 dontdel => 1,
103 readonly => 1,
104 });
105 2 $self->prop({
106 name => 'SQRT2',
107 value => JE::Number->new($global, 2**.5),
108 dontenum => 1,
109 dontdel => 1,
110 readonly => 1,
111 });
112  
113 $self->prop({
114 name => 'abs',
115 value => JE::Object::Function->new({
116 scope => $global,
117 name => 'abs',
118 argnames => ['x'],
119 no_proto => 1,
120 function_args => ['args'],
121 2 ▶ function => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
122 2 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
123 defined $_[0]
124 ? abs $_[0]->to_number->value
125 : 'nan');
126 },
127 2 }),
128 dontenum => 1,
129 });
130 $self->prop({
131 name => 'acos',
132 value => JE::Object::Function->new({
133 scope => $global,
134 name => 'acos',
135 argnames => ['x'],
136 no_proto => 1,
137 function_args => ['args'],
138 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
139 0 my $num;
140 0 ▶ if(defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
141 0 $num = $_[0]->to_number->value;
142 0 $num = atan2+
143 (1 - $num * $num)**.5,
144 $num;
145 }
146 else {
147 0 $num = 'nan';
148 }
149 0 JE::Number->new($global, $num);
150 },
151 2 }),
152 dontenum => 1,
153 });
154 $self->prop({
155 name => 'asin',
156 value => JE::Object::Function->new({
157 scope => $global,
158 name => 'asin',
159 argnames => ['x'],
160 no_proto => 1,
161 function_args => ['args'],
162 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
163 0 my $num;
164 0 ▶ if(defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
165 0 $num = $_[0]->to_number->value;
166 0 $num = atan2+
167 $num,
168 (1 - $num * $num)**.5;
169 }
170 else {
171 0 $num = 'nan';
172 }
173 0 JE::Number->new($global, $num);
174 },
175 2 }),
176 dontenum => 1,
177 });
178 $self->prop({
179 name => 'atan',
180 value => JE::Object::Function->new({
181 scope => $global,
182 name => 'atan',
183 argnames => ['x'],
184 no_proto => 1,
185 function_args => ['args'],
186 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
187 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
188 defined $_[0]
189 ? atan2($_[0]->to_number->value, 1)
190 : 'nan');
191 },
192 2 }),
193 dontenum => 1,
194 });
195 $self->prop({
196 name => 'atan2',
197 value => JE::Object::Function->new({
198 scope => $global,
199 name => 'atan2',
200 argnames => [qw/y x/],
201 no_proto => 1,
202 function_args => ['args'],
203 0 ▶ function => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
204 JE::Number->new($global,
205 defined $_[0] && defined $_[1]
206 0 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && defined $_[1] ? : 0 0 0
Condition0%0/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table0%0/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
207 0 my $a = $_[0]->to_number->value;
208 0 my $b = $_[1]->to_number->value;
209 # Windoze has trouble
210 # with two infs.
211 0 ▶ $a + 1 == $a && $b+1 == $b
Branch: 3 branches
BranchTrueFalseTotal
$a + 1 == $a && $b + 1 == $b ? : 0 0 0
$b > 0 ? : 0 0 0
$a > 0 ? : 0 0 0
Condition0%0/3
$a + 1 == $a and $b + 1 == $b
!ll&&!rl&&r
000
MC/DC0%0/2
$a + 1 == $a and $b + 1 == $b
$a + 1 == $a $b + 1 == $b
Truth table0%0/3
A and B
A= $a + 1 == $aB= $b + 1 == $b
ABresult
0X0
100
111
212 ? ($b>0 ? 1 : 3) * atan2(1,1)
213 * ($a > 0 ? 1 : -1)
214 : atan2($a, $b)
215 }
216 : 'nan');
217 },
218 2 }),
219 dontenum => 1,
220 });
221 $self->prop({
222 name => 'ceil',
223 value => JE::Object::Function->new({
224 scope => $global,
225 name => 'ceil',
226 argnames => [qw/x/],
227 no_proto => 1,
228 function_args => ['args'],
229 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
230 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
231 defined $_[0]
232 ? ceil($_[0]->to_number->value)
233 : 'nan');
234 },
235 2 }),
236 dontenum => 1,
237 });
238 $self->prop({
239 name => 'cos',
240 value => JE::Object::Function->new({
241 scope => $global,
242 name => 'cos',
243 argnames => [qw/x/],
244 no_proto => 1,
245 function_args => ['args'],
246 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
247 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
248 defined $_[0]
249 ? cos($_[0]->to_number->value)
250 : 'nan');
251 },
252 2 }),
253 dontenum => 1,
254 });
255 $self->prop({
256 name => 'exp',
257 value => JE::Object::Function->new({
258 scope => $global,
259 name => 'exp',
260 argnames => [qw/x/],
261 no_proto => 1,
262 function_args => ['args'],
263 0 ▶ function => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
264 0 ▶ JE::Number->new($global,
Branch: 3 branches
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
$_[0] + 1 == $_[0] ? : 0 0 0
$_[0] < 0 ? : 0 0 0
265 defined $_[0]
266 ? $_[0] + 1 == $_[0] # inf
267 ? $_[0] < 0 ? 0 : 'inf'
268 : exp($_[0]->to_number->value)
269 : 'nan');
270 },
271 2 }),
272 dontenum => 1,
273 });
274 $self->prop({
275 name => 'floor',
276 value => JE::Object::Function->new({
277 scope => $global,
278 name => 'floor',
279 argnames => [qw/x/],
280 no_proto => 1,
281 function_args => ['args'],
282 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
283 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
284 defined $_[0]
285 ? floor($_[0]->to_number->value)
286 : 'nan');
287 },
288 2 }),
289 dontenum => 1,
290 });
291 $self->prop({
292 name => 'log',
293 value => JE::Object::Function->new({
294 scope => $global,
295 name => 'log',
296 argnames => [qw/x/],
297 no_proto => 1,
298 function_args => ['args'],
299 0 ▶ function => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
300 0 my $num;
301 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
302 0 $num = $_[0]->to_number->value;
303 0 ▶ $num = $num < 0 ? 'nan' :
Branch: 2 branches
BranchTrueFalseTotal
$num < 0 ? : 0 0 0
$num == 0 ? : 0 0 0
304 $num == 0 ? '-Infinity' :
305 log $num;
306 }
307 0 else { $num = 'nan' }
308 0 JE::Number->new($global, $num);
309 },
310 2 }),
311 dontenum => 1,
312 });
313 $self->prop({
314 name => 'max',
315 value => JE::Object::Function->new({
316 scope => $global,
317 name => 'max',
318 length => 2,
319 no_proto => 1,
320 function_args => ['args'],
321 0 ▶ function => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
322 0 ▶@_ or return JE::Number->new($global, '-inf');
Branch: 1 branch
BranchTrueFalseTotal
@_ or return "JE::Number"->new($global, "-inf") 0 0 0
323 0 my $result; my $num;
324 0 for (@_) {
325 0 ▶ ($num = $_->to_number->value) == $num or
Branch: 1 branch
BranchTrueFalseTotal
($num = $_->to_number->value) == $num or return "JE::Number"->new($global, "nan") 0 0 0
326 return JE::Number->new($global, 'nan');;
327 0 ▶ $result = $num if !defined $result or $result < $num;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $result or $result < $num 0 0 0
Condition0%0/3
not defined $result or $result < $num
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $result or $result < $num
not defined $result $result < $num
Truth table0%0/3
not(A) or B
A= not defined $resultB= $result < $num
ABresult
1X1
011
000
328 }
329 0 JE::Number->new($global, $result);
330  
331 },
332 2 }),
333 dontenum => 1,
334 });
335 $self->prop({
336 name => 'min',
337 value => JE::Object::Function->new({
338 scope => $global,
339 name => 'min',
340 length => 2,
341 no_proto => 1,
342 function_args => ['args'],
343 0 ▶ function => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
344 0 ▶@_ or return JE::Number->new($global, 'inf');
Branch: 1 branch
BranchTrueFalseTotal
@_ or return "JE::Number"->new($global, "inf") 0 0 0
345 0 my $result; my $num;
346 0 for (@_) {
347 0 ▶ ($num = $_->to_number->value) == $num or
Branch: 1 branch
BranchTrueFalseTotal
($num = $_->to_number->value) == $num or return "JE::Number"->new($global, "nan") 0 0 0
348 return JE::Number->new($global, 'nan');;
349 0 ▶ $result = $num if !defined $result or $result > $num;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $result or $result > $num 0 0 0
Condition0%0/3
not defined $result or $result > $num
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $result or $result > $num
not defined $result $result > $num
Truth table0%0/3
not(A) or B
A= not defined $resultB= $result > $num
ABresult
1X1
011
000
350 }
351 0 JE::Number->new($global, $result);
352  
353 },
354 2 }),
355 dontenum => 1,
356 });
357  
358 $self->prop({
359 name => 'pow',
360 value => JE::Object::Function->new({
361 scope => $global,
362 name => 'pow',
363 argnames => [qw/x y/],
364 no_proto => 1,
365 function_args => ['args'],
366 0 ▶ function => sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
367 0 ▶my $x = defined $_[0] ? $_[0]->to_number->value : nan;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
368 0 ▶my $y = defined $_[1] ? $_[1]->to_number->value : nan;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
369  
370 0 ▶abs $x == 1 && abs $y == inf &&
Branch: 1 branch
BranchTrueFalseTotal
abs $x == 1 and abs $y == "Inf" and return "JE::Object::Number"->new($global, "nan") 0 0 0
Condition0%0/3
abs $x == 1 and abs $y == "Inf"
!ll&&!rl&&r
000
MC/DC0%0/2
abs $x == 1 and abs $y == "Inf"
abs $x == 1 abs $y == "Inf"
Truth table0%0/3
A and B
A= abs $x == 1B= abs $y == "Inf"
ABresult
0X0
100
111
371 return JE::Object::Number->new($global, 'nan');
372  
373 0 ▶$y == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
$y == 0 and return "JE::Number"->new($global, 1) 0 0 0
374 return JE::Number->new($global, 1);
375  
376 0 ▶$x == 0 && $y < 0 && return JE'Number->new($global, inf);
Branch: 1 branch
BranchTrueFalseTotal
$x == 0 and $y < 0 and return "JE::Number"->new($global, "Inf") 0 0 0
Condition0%0/3
$x == 0 and $y < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x == 0 and $y < 0
$x == 0 $y < 0
Truth table0%0/3
A and B
A= $x == 0B= $y < 0
ABresult
0X0
100
111
377  
378 0 ▶$x == -+inf && $y < 0 &&
Branch: 2 branches
BranchTrueFalseTotal
$x == "-Inf" and $y < 0 and return "JE::Number"->new($global, int $y != $y || !($y % 2) ? 0 : 0) 0 0 0
int $y != $y || !($y % 2) ? : 0 0 0
Condition0%0/6
$x == "-Inf" and $y < 0
!ll&&!rl&&r
000
int $y != $y or not $y % 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$x == "-Inf" and $y < 0
$x == "-Inf" $y < 0
MC/DC0%0/2
int $y != $y or not $y % 2
int $y != $y not $y % 2
Truth table0%0/3
A and B
A= $x == "-Inf"B= $y < 0
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= int $y != $yB= not $y % 2
ABresult
1X1
011
000
379 return JE'Number->new($global,
380 int $y != $y || !($y % 2) ? 0 : -0.0);
381  
382 0 ▶$x == -+inf && $y > 0 && int $y != $y &&
Branch: 1 branch
BranchTrueFalseTotal
$x == "-Inf" and $y > 0 and int $y != $y and return "JE::Number"->new($global, "Inf") 0 0 0
Condition0%0/6
$x == "-Inf" and $y > 0 and int $y != $y
!ll&&!rl&&r
000
$x == "-Inf" and $y > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$x == "-Inf" and $y > 0 and int $y != $y
$x == "-Inf" $y > 0 int $y != $y
Truth table0%0/4
A and B and C
A= $x == "-Inf"B= $y > 0C= int $y != $y
ABCresult
0XX0
10X0
1100
1111
383 return JE'Number->new($global, inf);
384  
385 0 return JE::Number->new($global, $x ** $y);
386  
387 },
388 2 }),
389 dontenum => 1,
390 });
391 $self->prop({
392 name => 'random',
393 value => JE::Object::Function->new({
394 scope => $global,
395 name => 'random',
396 no_proto => 1,
397 function_args => [],
398 0 ▶ function => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
399 0 JE::Number->new($global, rand);
400 },
401 2 }),
402 dontenum => 1,
403 });
404 $self->prop({
405 name => 'round',
406 value => JE::Object::Function->new({
407 scope => $global,
408 name => 'round',
409 argnames => ['x'],
410 no_proto => 1,
411 function_args => ['args'],
412 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
413 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
414 defined $_[0]
415 ? floor($_[0]->to_number->value+.5)
416 : 'nan');
417 },
418 2 }),
419 dontenum => 1,
420 });
421 $self->prop({
422 name => 'sin',
423 value => JE::Object::Function->new({
424 scope => $global,
425 name => 'sin',
426 argnames => [qw/x/],
427 no_proto => 1,
428 function_args => ['args'],
429 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
430 0 ▶ JE::Number->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
431 defined $_[0]
432 ? sin($_[0]->to_number->value)
433 : 'nan');
434 },
435 2 }),
436 dontenum => 1,
437 });
438 $self->prop({
439 name => 'sqrt',
440 value => JE::Object::Function->new({
441 scope => $global,
442 name => 'sqrt',
443 argnames => [qw/x/],
444 no_proto => 1,
445 function_args => ['args'],
446 0 ▶ function => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
447 JE::Number->new($global,
448 defined $_[0]
449 0 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
450 0 my $num
451 = $_[0]->to_number->value;
452 0 ▶ $num == -+inf
Branch: 1 branch
BranchTrueFalseTotal
$num == -9**9**9 ? : 0 0 0
453 ? 'nan'
454 : $num ** .5
455 }
456 : 'nan');
457 },
458 2 }),
459 dontenum => 1,
460 });
461 $self->prop({
462 name => 'tan',
463 value => JE::Object::Function->new({
464 scope => $global,
465 name => 'tan',
466 argnames => [qw/x/],
467 no_proto => 1,
468 function_args => ['args'],
469 0 ▶ function => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
470 0 my $num = shift;
471 0 ▶ if(defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num) { } 0 0 0
472 0 $num = $num->to_number->value;
473 0 $num = sin($num) / cos $num;
474 }
475 0 else { $num = nan }
476 0 JE::Number->new($global, $num);
477 },
478 2 }),
479 dontenum => 1,
480 });
481  
482 2 $self;
483 }
484  
485  
486  
487 =item value
488  
489 Not particularly useful. Returns a hash ref that is completely empty,
490 unless you've added
491 your own properties to the object. This may change in a future release.
492  
493 =item class
494  
495 Returns the string 'Math'
496  
497 =cut
498  
499 3 ▶sub class { 'Math' }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
500  
501  
502  
503  
504 2 return "a true value";
505  
506 =back
507  
508 =head1 SEE ALSO
509  
510 =over 4
511  
512 =item JE
513  
514 =item JE::Types
515  
516 =item JE::Object
517  
518 =item JE::Number
519  
520 =back
521  
522 =cut
« JE/Code.pm ↑ Summary JE/Parser.pm »