| 1 | package JE::Object::Number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 101 | our $VERSION = '0.066'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 101 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 101 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 101 | ▶ | use constant inf => 9**9**9; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 101 | our @ISA = 'JE::Object'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 101 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 101 | require JE::Code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 101 | require JE::Number; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 101 | require JE::Object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 101 | require JE::Object::Function; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 101 | require JE::String; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 101 | import JE::Code 'add_line_number'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | sub add_line_number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | JE::Object::Number - JavaScript Number object class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | use JE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | use JE::Object::Number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | $j = new JE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | $js_num_obj = new JE::Object::Number $j, 953.7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $perl_scalar = $js_num_obj->value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 + $js_num_obj; # 953.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | This class implements JavaScript Number objects for JE. The difference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | between this and JE::Number is that that module implements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | I<primitive> number values, while this module implements the I<objects.> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | See L<JE::Types> for descriptions of most of the methods. Only what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | is specific to JE::Object::Number is explained here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 253 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 253 | my($class, $global, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 253 | ▶ | my $self = $class->SUPER::new($global, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$global->prototype_for('Number') || $global->prop('Number')->prop('prototype')
MC/DC0%0/2
$global->prototype_for('Number') || $global->prop('Number')->prop('prototype')
$global->prototype_for('Number')
$global->prop('Number')->prop('prototype')
Truth table33%1/3
A || B
A= $global->prototype_for('Number')B= $global->prop('Number')->prop('prototype')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | prototype => $global->prototype_for('Number') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | || $global->prop('Number')->prop('prototype') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 253 | ▶ | $$$self{value} = defined blessed $val && $val->can('to_number') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined builtin::blessed($val) and $val->can("to_number")
MC/DC50%1/2
defined builtin::blessed($val) and $val->can("to_number")
defined builtin::blessed($val)
$val->can("to_number")
Truth table66%2/3
A and B
A= defined builtin::blessed($val)B= $val->can("to_number")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ? $val->to_number->[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | : JE::Number::_numify($val); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 253 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =item value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | Returns a Perl scalar containing the number that the object holds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | sub value { $${$_[0]}{value} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | =item class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | Returns the string 'Number'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 213 | ▶ | sub class { 'Number' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 101 | our @_digits = (0..9, 'a' .. 'z'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 17 | ▶ | sub _new_constructor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_constructor: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 17 | my $global = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | my $f = JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | name => 'Number', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | argnames => [qw/value/], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 27 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 27 | ▶ | defined $_[0] ? $_[0]->to_number : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | JE'Number->new($global, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | function_args => ['args'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 234 | ▶ | constructor => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 234 | unshift @_, __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 234 | goto &new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 17 | constructor_args => ['scope','args'], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # The max according to ECMA-262 ≈ 1.7976931348623157e+308. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # The max I can get in Perl with a literal is 1.797693134862314659999e+308, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # probably as a result of perl bug #41202. Using ECMA’s maximum does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # make sense in our case, anyway, as we are using perl’s (i.e., the sys- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # tem’s) floating point. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # So I am using routines borrowed from Data::Float to get what are the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # actual minimum and maximum values that we can handle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 17 | $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | name => 'MAX_VALUE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | autoload => ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | require "JE/Object/Number/maxvalue.pl"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | $JE::Object::Number::max_finite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | dontdel => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 17 | $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | name => 'MIN_VALUE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | autoload => ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | require "JE/Object/Number/maxvalue.pl"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | $JE::Object::Number::min_finite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | dontdel => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 17 | $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | name => 'NaN', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | value => JE::Number->new($global, 'nan'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | dontdel => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 17 | $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | name => 'NEGATIVE_INFINITY', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | value => JE::Number->new($global, '-inf'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | dontdel => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 17 | $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | name => 'POSITIVE_INFINITY', # positively infinite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | value => JE::Number->new($global, 'inf'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | dontdel => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 17 | my $proto = bless $f->prop({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | name => 'prototype', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | }), __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 17 | $global->prototype_for(Number=>$proto); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 17 | $$$proto{value} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | name => 'toString', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | name => 'toString', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | argnames => ['radix'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | function_args => ['this','args'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 15 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=44.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 15 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | "Number.prototype.toString is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 12 | my $radix = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 12 | ▶ | !defined $radix || $radix->id eq 'undef' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $radix or $radix->id eq "undef"
MC/DC0%0/2
not defined $radix or $radix->id eq "undef"
not defined $radix
$radix->id eq "undef"
Truth table33%1/3
not(A) or B
A= not defined $radixB= $radix->id eq "undef"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | and return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | $self->to_primitive->to_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | ($radix = $radix->to_number->value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
($radix = $radix->to_number->value) == 10 or $radix < 2 or $radix > 36 or $radix =~ /\./
($radix = $radix->to_number->value) == 10 or $radix < 2 or $radix > 36
($radix = $radix->to_number->value) == 10 or $radix < 2
MC/DC0%0/4
($radix = $radix->to_number->value) == 10 or $radix < 2 or $radix > 36 or $radix =~ /\./
($radix = $radix->to_number->value) == 10
$radix < 2
$radix > 36
$radix =~ /\./
Truth table0%0/5
A or B or C or D
A= ($radix = $radix->to_number->value) == 10B= $radix < 2C= $radix > 36D= $radix =~ /\./
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | == 10 || $radix < 2 || $radix > 36 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | $radix =~ /\./ and return $self->to_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if ($radix == 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return JE::String->new($global, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | sprintf '%b', $self->value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | elsif($radix == 8) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | return JE::String->new($global, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | sprintf '%o', $self->value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | elsif($radix == 16) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | return JE::String->new($global, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | sprintf '%x', $self->value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | my $num = $self->value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | while($num >= 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | substr($result,0,0) = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | $_digits[$num % $radix]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $num /= $radix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | return JE::String->new($global, $result); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | name => 'toLocaleString', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | name => 'toLocaleString', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | function_args => ['this'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | "Number.prototype.toLocaleString ". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | "is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # ~~~ locale stuff | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | return JE::String->_new($global, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | $self->value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | name => 'valueOf', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | name => 'valueOf', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | function_args => ['this'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 205 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 205 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 205 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | "Number.prototype.valueOf is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # We also deal with plain JE::Numbers here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | ref $self eq 'JE::Number' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | ? $self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 202 | ▶ | : JE::Number->new($global,$$$self{value}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | name => 'toFixed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | name => 'toFixed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | argnames => ['fractionDigits'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | function_args => ['this','args'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 13 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 13 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | "Number.prototype.toFixed is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 10 | my $places = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 10 | ▶ | if(defined $places) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 8 | ▶ | $places = ($places = int $places->to_number) == $places && $places; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
($places = int $places->to_number) == $places && $places
MC/DC100%2/2
($places = int $places->to_number) == $places && $places
($places = int $places->to_number) == $places
$places
Truth table100%3/3
A && B
A= ($places = int $places->to_number) == $placesB= $places
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | else { $places = 0 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 10 | ▶ | $places < 0 and throw JE::Object::Error::RangeError->new($global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | "Invalid number of decimal places: $places " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | "(negative numbers not supported)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 10 | my $num = $self->value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 10 | ▶ | $num == $num or return JE::String->_new($global, 'NaN'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 9 | ▶ | abs $num >= 1000000000000000000000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | and return JE::String->_new($global, $num); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # ~~~ if/when JE::Number::to_string is rewritten, make this use the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # Deal with numbers ending with 5. perl (in Snow Leopard at least) rounds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # 30.125 down, whereas ECMAScript says that it should round up. (15.7.4.5: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # ‘Let n be an integer for which the exact mathematical value of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # n ÷ 10^f – x is as close to zero as possible. If there are two such n, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # pick the larger n.’) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 8 | ▶ | if((my $sprintfed = sprintf "%." . ($places+1) . 'f', $num) =~ /5\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 2 | (my $upper = $sprintfed) =~ s/\.?.\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | my $lower = $upper; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 2 | ++substr $upper,-1,1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | ▶ | return JE::String->_new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | $global, $upper-$num <= $num-$lower ? $upper : $lower | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 6 | return JE::String->_new($global, sprintf "%.${places}f", $num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | name => 'toExponential', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | name => 'toExponential', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | argnames => ['fractionDigits'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | function_args => ['this','args'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 18 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 18 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | "Number.prototype. toExponential is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 15 | my $num = $self->value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 15 | ▶ | $num == $num or return JE::String->_new($global, 'NaN'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 14 | ▶ | abs $num == inf && return JE::String->_new($global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$num < 0 && '-'
MC/DC100%1/1
$num < 0 && '-'
$num < 0
Truth table100%2/2
A
A= $num < 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | ($num < 0 && '-') . 'Infinity'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 12 | my $places = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 12 | ▶ | if(defined $places) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 6 | ▶ | $places | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
0 + (($places = int $places->to_number) == $places) && $places
MC/DC100%2/2
0 + (($places = int $places->to_number) == $places) && $places
0 + (($places = int $places->to_number) == $places)
$places
Truth table100%3/3
A && B
A= 0 + (($places = int $places->to_number) == $places)B= $places
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | = 0+(($places = int $places->to_number) == $places) && $places; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 6 | else { $places = !1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 12 | ▶ | $places < 0 and throw JE::Object::Error::RangeError->new($global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | "Invalid number of decimal places: $places " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | "(negative numbers not supported)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # Deal with half-way rounding. See the note above in toFixed. It applies to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # toExponential as well (except that this is section 15.7.4.6). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 12 | ▶ | if((my $sprintfed = sprintf "%." . ($places+1) . 'e', $num) =~ /5e/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 3 | (my $upper = $sprintfed) =~ s/\.?.(e.*)\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 3 | my $lower = $upper; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 3 | ++substr $upper,-1,1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 3 | ▶ | (my $ret = ($upper-$num <= $num-$lower ? $upper : $lower) . $1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | =~ s/\.?0*e([+-])0*(?!\z)/e$1/; # convert 0.0000e+00 to 0e+0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | return JE::String->_new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | $global, $ret | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 9 | my $result = sprintf "%"."."x!!length($places)."${places}e", $num; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 9 | $result =~ s/\.?0*e([+-])0*(?!\z)/e$1/; # convert 0.0000e+00 to 0e+0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 9 | return JE::String->_new($global, $result); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | $proto->prop({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | name => 'toPrecision', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | value => JE::Object::Function->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | scope => $global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | name => 'toPrecision', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | no_proto => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | argnames => ['precision'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | function_args => ['this','args'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 18 | ▶ | function => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 18 | ▶ | die JE::Object::Error::TypeError->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | $global, add_line_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | "Argument to " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | "Number.prototype. toPrecision is not" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | . " a " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | "Number object" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | ) unless $self->class eq 'Number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 15 | my $num = $self->value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 15 | ▶ | $num == $num or return JE::String->_new($global, 'NaN'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 14 | ▶ | abs $num == inf && return JE::String->_new($global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$num < 0 && '-'
MC/DC100%1/1
$num < 0 && '-'
$num < 0
Truth table100%2/2
A
A= $num < 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | ($num < 0 && '-') . 'Infinity'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 12 | my $prec = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 12 | ▶ | if(!defined $prec || $prec->id eq 'undef') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $prec or $prec->id eq "undef"
MC/DC50%1/2
not defined $prec or $prec->id eq "undef"
not defined $prec
$prec->id eq "undef"
Truth table66%2/3
not(A) or B
A= not defined $precB= $prec->id eq "undef"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 2 | return JE::String->_new($global, $num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # ~~~ if/when JE::Number::to_string is rewritten, make this use the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 10 | ▶ | $prec = ($prec = int $prec->to_number) == $prec && $prec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
($prec = int $prec->to_number) == $prec && $prec
MC/DC0%0/2
($prec = int $prec->to_number) == $prec && $prec
($prec = int $prec->to_number) == $prec
$prec
Truth table33%1/3
A && B
A= ($prec = int $prec->to_number) == $precB= $prec
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 10 | ▶ | $prec < 1 and throw JE::Object::Error::RangeError->new($global, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | "Precision out of range: $prec " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | "(must be >= 1)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # ~~~ Probably not the most efficient alrogithm. maybe I coould optimimse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # it later. OD yI have tot proooofrfreoad my aown tiyping.? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 10 | ▶ | if ($num == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 2 | ▶ | $prec == 1 or $num = '0.' . '0' x ($prec-1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 8 | $num = sprintf "%.${prec}g", $num; # round it off | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 8 | my($e) = sprintf "%.0e", $num, =~ /e(.*)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 8 | ▶ | if($e < -6 || $e >= $prec) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$e < -6 or $e >= $prec
MC/DC100%2/2
$e < -6 or $e >= $prec
$e < -6
$e >= $prec
Truth table100%3/3
A or B
A= $e < -6B= $e >= $prec
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 3 | ($num = sprintf "%.".($prec-1)."e", $num) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | =~ s/(?<=e[+-])0+(?!\z)//; # convert 0e+00 to 0e+0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 3 | ▶ | $num =~ /\./ or $num =~ s/e/.e/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 5 | else { $num = sprintf "%." . ($prec - 1 - $e) . 'f', $num } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 10 | return JE::String->_new($global, $num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 17 | }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | dontenum => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 17 | $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 101 | return "a true value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =item JE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | =item JE::Types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | =item JE::Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | =item JE::Number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||