Skip to content

Implement printing in the specified radix for Number.prototype.toString(). #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dbatyai
Copy link
Member

@dbatyai dbatyai commented Jul 6, 2015

I had to re-open this, because I can't update the other one.
Previous discussion can be found here: #207
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com

@dbatyai dbatyai added ecma builtins Related to ECMA built-in routines development Feature implementation labels Jul 6, 2015
@dbatyai dbatyai added this to the ECMA builtins milestone Jul 6, 2015
@dbatyai
Copy link
Member Author

dbatyai commented Jul 6, 2015

I've updated this to calculate an approximate buffer size. It can be slightly larger than necessary, but that should be negligible. I've added tests for two digit numbers as well.

}

uint64_t whole = (uint64_t) this_arg_number;
double fraction = this_arg_number - (double) whole;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does usage of double instead of ecma_number_t improve precision in fp32 mode in the case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unintentional. But since you asked, I don't think this improves precision. The original value is already constrained to a float, and because of the continued multiplication the required precision to represent the result would only decrease.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see. The question was about additional precision lose, that may be can occur in intermediate operations.

I think we should use ecma_number_t in the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we should move the following defines from ecma-helpers-number.cpp to ecma-globals.h, near definition of ecma_number_t and use the definitions in the function:

#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint32_t));

/**
 * Width of sign field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_SIGN_WIDTH       (1)

/**
 * Width of biased exponent field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_BIASED_EXP_WIDTH (8)

/**
 * Width of fraction field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_FRACTION_WIDTH   (23)
#elif CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint64_t));

/**
 * Width of sign field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_SIGN_WIDTH       (1)

/**
 * Width of biased exponent field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_BIASED_EXP_WIDTH (11)

/**
 * Width of fraction field
 *
 * See also:
 *          IEEE-754 2008, 3.6, Table 3.5
 */
#define ECMA_NUMBER_FRACTION_WIDTH   (52)
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you.

@dbatyai dbatyai force-pushed the number_prototype_tostring_radix branch from 5cfbfc1 to 7cd9c7a Compare July 6, 2015 13:14
@galpeter galpeter mentioned this pull request Jul 6, 2015
16 tasks
@dbatyai dbatyai force-pushed the number_prototype_tostring_radix branch 3 times, most recently from 74166b5 to 6e0e7a9 Compare July 7, 2015 14:26
@ruben-ayrapetyan
Copy link
Contributor

Looks good to me


if (should_round)
{
/* Round off last digit. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: of

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, right :)

@LaszloLango
Copy link
Contributor

+1 lgtm

@egavrin
Copy link
Contributor

egavrin commented Jul 7, 2015

make push

@egavrin egavrin assigned dbatyai and unassigned egavrin Jul 7, 2015
@dbatyai dbatyai force-pushed the number_prototype_tostring_radix branch 3 times, most recently from c9b907f to af20005 Compare July 8, 2015 09:00
…ng().

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
@dbatyai dbatyai force-pushed the number_prototype_tostring_radix branch from af20005 to 5e0a355 Compare July 8, 2015 09:25
@dbatyai dbatyai merged commit 5e0a355 into jerryscript-project:master Jul 8, 2015
@dbatyai dbatyai deleted the number_prototype_tostring_radix branch July 31, 2015 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Feature implementation ecma builtins Related to ECMA built-in routines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants