Implement Number.prototype.toExponential()

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
This commit is contained in:
Dániel Bátyai
2015-07-01 10:49:24 +02:00
parent 505beadfeb
commit 53cbb55f25
3 changed files with 234 additions and 1 deletions
@@ -396,7 +396,174 @@ static ecma_completion_value_t
ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
/* 1. */
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
/* 7. */
if (arg_num <= -1.0 || arg_num >= 21.0)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
}
else
{
/* 3. */
if (ecma_number_is_nan (this_num))
{
ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
}
else
{
bool is_negative = false;
/* 5. */
if (ecma_number_is_negative (this_num) && !ecma_number_is_zero (this_num))
{
is_negative = true;
this_num *= -1;
}
/* 6. */
if (ecma_number_is_infinity (this_num))
{
ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
if (is_negative)
{
ecma_string_t *neg_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_MINUS_CHAR);
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
ecma_deref_ecma_string (infinity_str_p);
ecma_deref_ecma_string (neg_str_p);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (neg_inf_str_p));
}
else
{
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (infinity_str_p));
}
}
else
{
uint64_t digits = 0;
int32_t num_digits = 0;
int32_t exponent = 1;
if (!ecma_number_is_zero (this_num))
{
/* Get the parameters of the number if non zero. */
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
}
int32_t frac_digits;
if (ecma_is_value_undefined (arg))
{
frac_digits = num_digits - 1;
}
else
{
frac_digits = ecma_number_to_int32 (arg_num);
}
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - frac_digits - 1);
/* frac_digits + 2 characters for number, 5 characters for exponent, 1 for \0. */
int buffer_size = frac_digits + 2 + 5 + 1;
if (is_negative)
{
/* +1 character for sign. */
buffer_size++;
}
MEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, lit_utf8_byte_t);
int digit = 0;
uint64_t scale = 1;
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
while (scale <= digits)
{
scale *= 10;
}
lit_utf8_byte_t *actual_char_p = buff;
if (is_negative)
{
*actual_char_p++ = '-';
}
/* Add significant digits. */
for (int i = 0; i <= frac_digits; i++)
{
digit = 0;
scale /= 10;
while (digits >= scale && scale > 0)
{
digits -= scale;
digit++;
}
*actual_char_p = (lit_utf8_byte_t) (digit + '0');
actual_char_p++;
if (i == 0 && frac_digits != 0)
{
*actual_char_p++ = '.';
}
}
*actual_char_p++ = 'e';
exponent--;
if (exponent < 0)
{
exponent *= -1;
*actual_char_p++ = '-';
}
else
{
*actual_char_p++ = '+';
}
/* Get magnitude of exponent. */
int32_t scale_expt = 1;
while (scale_expt <= exponent)
{
scale_expt *= 10;
}
scale_expt /= 10;
/* Add exponent digits. */
if (exponent == 0)
{
*actual_char_p++ = '0';
}
else
{
while (scale_expt > 0)
{
digit = exponent / scale_expt;
exponent %= scale_expt;
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
scale_expt /= 10;
}
}
JERRY_ASSERT (actual_char_p - buff < buffer_size);
*actual_char_p = '\0';
ecma_string_t *str = ecma_new_ecma_string_from_utf8 (buff, (lit_utf8_size_t) (actual_char_p - buff));
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (str));
MEM_FINALIZE_LOCAL_ARRAY (buff);
}
}
}
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
return ret_value;
} /* ecma_builtin_number_prototype_object_to_exponential */
/**
+1
View File
@@ -220,6 +220,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLASH_CHAR, "/")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP, "(?:)")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR, "[")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR, "]")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MINUS_CHAR, "-")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COLON_CHAR, ":")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COMMA_CHAR, ",")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPACE_CHAR, " ")
@@ -0,0 +1,65 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//This test will not pass on FLOAT32 due to precision issues
assert((123.56).toExponential() === "1.2356e+2");
assert((123.56).toExponential(0) === "1e+2");
assert((123.56).toExponential(1) === "1.2e+2");
assert((123.56).toExponential(5) === "1.23560e+2");
assert((-1.23).toExponential(1) === "-1.2e+0");
assert((0.00023).toExponential(0) === "2e-4");
assert((0.356).toExponential(1) === "3.6e-1");
assert((0.0000356).toExponential(2) === "3.56e-5");
assert((0.000030056).toExponential(2) === "3.01e-5");
assert(Infinity.toExponential(0) === "Infinity");
assert((-Infinity).toExponential(0) === "-Infinity");
assert(NaN.toExponential(0) === "NaN");
assert((0.0).toExponential(0) === "0e+0");
assert((0.0).toExponential(1) === "0.0e+0");
assert((-0.0).toExponential(0) === "0e+0");
assert((-0.0).toExponential(1) === "0.0e+0");
assert((123456789012345678901.0).toExponential(20) === "1.23456789012345680000e+20");
assert((123456789012345678901.0).toExponential("6") === "1.234568e+20");
assert((123.45).toExponential(3.2) === "1.235e+2");
assert((123.45).toExponential(-0.1) === "1e+2");
try {
(12).toExponential(Number.MAX_VALUE);
assert(false);
} catch (e) {
assert(e instanceof RangeError)
}
try {
(12).toExponential(Infinity);
assert(false);
} catch (e) {
assert(e instanceof RangeError)
}
try {
(12).toExponential(-1);
assert(false);
} catch (e) {
assert(e instanceof RangeError)
}
try {
(12).toExponential(21);
assert(false);
} catch (e) {
assert(e instanceof RangeError)
}