Skip to content
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

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

Closed
wants to merge 1 commit into from
Closed

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

wants to merge 1 commit into from

Conversation

dbatyai
Copy link
Member

@dbatyai dbatyai commented Jun 18, 2015

Depends on #100.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]

@dbatyai dbatyai added ecma builtins Related to ECMA built-in routines development Feature implementation labels Jun 18, 2015
@dbatyai dbatyai added this to the ECMA builtins milestone Jun 18, 2015
@egavrin
Copy link
Contributor

egavrin commented Jun 18, 2015

/home/jenk/.jenkins/jobs/GitHubAuto/workspace/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.cpp: In function ‘ecma_completion_value_t ecma_builtin_number_prototype_object_to_string(ecma_value_t, const ecma_value_t*, ecma_length_t)’:
/home/jenk/.jenkins/jobs/GitHubAuto/workspace/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.cpp:150:78: error: ‘ecma_number_calc_params’ was not declared in this scope
     ecma_number_calc_params (this_arg_number, &digits, &num_digits, &exponent);
                                                                              ^
compilation terminated due to -Wfatal-errors.
jerry-core/CMakeFiles/debug.jerry-core.dir/build.make:1296: recipe for target 'jerry-core/CMakeFiles/debug.jerry-core.dir/ecma/builtin-objects/ecma-builtin-number-prototype.cpp.o' failed

@dbatyai
Copy link
Member Author

dbatyai commented Jun 18, 2015

@egavrin,
Indeed, since this patch depends on #100, that will introduce this function.
When that patch lands, I'll rebase this one, and precommit will pass.

Until then, please take a look at the code.

/* Add zero terminator. */
buff[buff_index] = '\0';

ecma_string_t* str = ecma_new_ecma_string ((ecma_char_t *) buff);
Copy link
Contributor

Choose a reason for hiding this comment

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

str_p

@egavrin
Copy link
Contributor

egavrin commented Jun 21, 2015

Please rebase to master

@dbatyai
Copy link
Member Author

dbatyai commented Jun 22, 2015

I've rebased to master.

Also, I'd like to ask your opinion on the buffer. Currently it's set to a fixed 1080 size, since this is the largest we would ever need. Of course, it would be better to use a dynamic buffer, but I don't know if it would be worthwhile to implement something like that only for this function. We could also try to use a smaller buffer and limit the string size. What do you think?

…ng().

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
@ruben-ayrapetyan
Copy link
Contributor

@dbatyai, MEM_DEFINE_LOCAL_ARRAY, currently, provides a way to create dynamic buffer.

@dbatyai
Copy link
Member Author

dbatyai commented Jun 22, 2015

@ruben-ayrapetyan, could you please explain how exactly? The way I see it, MEM_DEFINE_LOCAL_ARRAY allocates a fix sized block, or am I missing something?

@ruben-ayrapetyan
Copy link
Contributor

@dbatyai, I see. You mean dynamically extensible buffer, but MEM_DEFINE_LOCAL_ARRAY allocates fixed-sized buffer with length, unknown at compile time.

Could we somehow calculate the number of digits prior to buffer allocation, maybe using log? In the case, we could use the MEM_DEFINE_LOCAL_ARRAY.

@dbatyai
Copy link
Member Author

dbatyai commented Jun 22, 2015

@ruben-ayrapetyan, Unfortunately we can't calculate the number of digits, because some finite fractions can become infinite when we convert them to a different radix.

@ruben-ayrapetyan
Copy link
Contributor

@dbatyai, yes, but in the case we anyway would truncate them to some finite number of digits. So, in the case, number of digits in output buffer is known, isn't it?

@dbatyai
Copy link
Member Author

dbatyai commented Jun 22, 2015

@ruben-ayrapetyan, you're right, I'll calculate a buffer size with log then.

@dbatyai
Copy link
Member Author

dbatyai commented Jun 22, 2015

@ruben-ayrapetyan, I've tried to get buffer size with log, but unfortunately it can only be used to calculate digit count for whole numbers, and not for fractions. Any other ideas?

@ruben-ayrapetyan
Copy link
Contributor

@ruben-ayrapetyan, I've tried to get buffer size with log, but unfortunately it can only be used to calculate digit count for whole numbers, and not for fractions. Any other ideas?

Can't digits number of fraction part be calculated from scale?

@dbatyai
Copy link
Member Author

dbatyai commented Jun 23, 2015

@ruben-ayrapetyan , scale only tells us how many zeros are there between the radix point and the first non-zero digit. This can be used to get a rough estimate, but has no real connection to the actual digit count.

assert((123400).toString(33) === "3ead");
assert((123400).toString(34) === "34pe");
assert((123400).toString(35) === "2upp");
assert((123400).toString(36) === "2n7s");
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to add some automated test, which runs through the 2 digit numbers for all 36 radix.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@dbatyai
Copy link
Member Author

dbatyai commented Jul 6, 2015

Continued here: #306

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