Skip to content

Commit 53cbb55

Browse files
committed
Implement Number.prototype.toExponential()
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 505bead commit 53cbb55

File tree

3 files changed

+234
-1
lines changed

3 files changed

+234
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.cpp

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,174 @@ static ecma_completion_value_t
396396
ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**< this argument */
397397
ecma_value_t arg) /**< routine's argument */
398398
{
399-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
399+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
400+
401+
/* 1. */
402+
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
403+
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
404+
405+
/* 7. */
406+
if (arg_num <= -1.0 || arg_num >= 21.0)
407+
{
408+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
409+
}
410+
else
411+
{
412+
/* 3. */
413+
if (ecma_number_is_nan (this_num))
414+
{
415+
ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
416+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
417+
}
418+
else
419+
{
420+
bool is_negative = false;
421+
422+
/* 5. */
423+
if (ecma_number_is_negative (this_num) && !ecma_number_is_zero (this_num))
424+
{
425+
is_negative = true;
426+
this_num *= -1;
427+
}
428+
429+
/* 6. */
430+
if (ecma_number_is_infinity (this_num))
431+
{
432+
ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
433+
434+
if (is_negative)
435+
{
436+
ecma_string_t *neg_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_MINUS_CHAR);
437+
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
438+
ecma_deref_ecma_string (infinity_str_p);
439+
ecma_deref_ecma_string (neg_str_p);
440+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (neg_inf_str_p));
441+
}
442+
else
443+
{
444+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (infinity_str_p));
445+
}
446+
}
447+
else
448+
{
449+
uint64_t digits = 0;
450+
int32_t num_digits = 0;
451+
int32_t exponent = 1;
452+
453+
if (!ecma_number_is_zero (this_num))
454+
{
455+
/* Get the parameters of the number if non zero. */
456+
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
457+
}
458+
459+
int32_t frac_digits;
460+
if (ecma_is_value_undefined (arg))
461+
{
462+
frac_digits = num_digits - 1;
463+
}
464+
else
465+
{
466+
frac_digits = ecma_number_to_int32 (arg_num);
467+
}
468+
469+
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - frac_digits - 1);
470+
471+
/* frac_digits + 2 characters for number, 5 characters for exponent, 1 for \0. */
472+
int buffer_size = frac_digits + 2 + 5 + 1;
473+
474+
if (is_negative)
475+
{
476+
/* +1 character for sign. */
477+
buffer_size++;
478+
}
479+
480+
MEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, lit_utf8_byte_t);
481+
482+
int digit = 0;
483+
uint64_t scale = 1;
484+
485+
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
486+
while (scale <= digits)
487+
{
488+
scale *= 10;
489+
}
490+
491+
lit_utf8_byte_t *actual_char_p = buff;
492+
493+
if (is_negative)
494+
{
495+
*actual_char_p++ = '-';
496+
}
497+
498+
/* Add significant digits. */
499+
for (int i = 0; i <= frac_digits; i++)
500+
{
501+
digit = 0;
502+
scale /= 10;
503+
while (digits >= scale && scale > 0)
504+
{
505+
digits -= scale;
506+
digit++;
507+
}
508+
509+
*actual_char_p = (lit_utf8_byte_t) (digit + '0');
510+
actual_char_p++;
511+
512+
if (i == 0 && frac_digits != 0)
513+
{
514+
*actual_char_p++ = '.';
515+
}
516+
}
517+
518+
*actual_char_p++ = 'e';
519+
520+
exponent--;
521+
if (exponent < 0)
522+
{
523+
exponent *= -1;
524+
*actual_char_p++ = '-';
525+
}
526+
else
527+
{
528+
*actual_char_p++ = '+';
529+
}
530+
531+
/* Get magnitude of exponent. */
532+
int32_t scale_expt = 1;
533+
while (scale_expt <= exponent)
534+
{
535+
scale_expt *= 10;
536+
}
537+
scale_expt /= 10;
538+
539+
/* Add exponent digits. */
540+
if (exponent == 0)
541+
{
542+
*actual_char_p++ = '0';
543+
}
544+
else
545+
{
546+
while (scale_expt > 0)
547+
{
548+
digit = exponent / scale_expt;
549+
exponent %= scale_expt;
550+
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
551+
scale_expt /= 10;
552+
}
553+
}
554+
555+
JERRY_ASSERT (actual_char_p - buff < buffer_size);
556+
*actual_char_p = '\0';
557+
ecma_string_t *str = ecma_new_ecma_string_from_utf8 (buff, (lit_utf8_size_t) (actual_char_p - buff));
558+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (str));
559+
MEM_FINALIZE_LOCAL_ARRAY (buff);
560+
}
561+
}
562+
}
563+
564+
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
565+
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
566+
return ret_value;
400567
} /* ecma_builtin_number_prototype_object_to_exponential */
401568

402569
/**

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLASH_CHAR, "/")
220220
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP, "(?:)")
221221
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR, "[")
222222
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR, "]")
223+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MINUS_CHAR, "-")
223224
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COLON_CHAR, ":")
224225
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COMMA_CHAR, ",")
225226
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPACE_CHAR, " ")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//This test will not pass on FLOAT32 due to precision issues
17+
18+
assert((123.56).toExponential() === "1.2356e+2");
19+
assert((123.56).toExponential(0) === "1e+2");
20+
assert((123.56).toExponential(1) === "1.2e+2");
21+
assert((123.56).toExponential(5) === "1.23560e+2");
22+
assert((-1.23).toExponential(1) === "-1.2e+0");
23+
assert((0.00023).toExponential(0) === "2e-4");
24+
assert((0.356).toExponential(1) === "3.6e-1");
25+
assert((0.0000356).toExponential(2) === "3.56e-5");
26+
assert((0.000030056).toExponential(2) === "3.01e-5");
27+
assert(Infinity.toExponential(0) === "Infinity");
28+
assert((-Infinity).toExponential(0) === "-Infinity");
29+
assert(NaN.toExponential(0) === "NaN");
30+
assert((0.0).toExponential(0) === "0e+0");
31+
assert((0.0).toExponential(1) === "0.0e+0");
32+
assert((-0.0).toExponential(0) === "0e+0");
33+
assert((-0.0).toExponential(1) === "0.0e+0");
34+
assert((123456789012345678901.0).toExponential(20) === "1.23456789012345680000e+20");
35+
assert((123456789012345678901.0).toExponential("6") === "1.234568e+20");
36+
assert((123.45).toExponential(3.2) === "1.235e+2");
37+
assert((123.45).toExponential(-0.1) === "1e+2");
38+
39+
try {
40+
(12).toExponential(Number.MAX_VALUE);
41+
assert(false);
42+
} catch (e) {
43+
assert(e instanceof RangeError)
44+
}
45+
46+
try {
47+
(12).toExponential(Infinity);
48+
assert(false);
49+
} catch (e) {
50+
assert(e instanceof RangeError)
51+
}
52+
53+
try {
54+
(12).toExponential(-1);
55+
assert(false);
56+
} catch (e) {
57+
assert(e instanceof RangeError)
58+
}
59+
60+
try {
61+
(12).toExponential(21);
62+
assert(false);
63+
} catch (e) {
64+
assert(e instanceof RangeError)
65+
}

0 commit comments

Comments
 (0)