Skip to content

ext/bcmath: Supports scientific notation #18068

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed the restriction on the integer part of the notation
  • Loading branch information
SakiTakamachi committed Mar 16, 2025
commit dfa260b2a11cdd640f588b18a93244372033236e
56 changes: 27 additions & 29 deletions ext/bcmath/libbcmath/src/str2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ static bool bc_scientific_notation_str2num(
{
const char *fractional_end = exponent_ptr;

/* In scientific notation, the mantissa always has one integer digit. */
if (UNEXPECTED(digits != 1)) {
goto fail;
}

/* Must be 1 <= mantissa < 10 */
if (UNEXPECTED(*integer_ptr == 0)) {
goto fail;
}

if (UNEXPECTED(*exponent_ptr != 'e' && *exponent_ptr != 'E')) {
goto fail;
}
Expand Down Expand Up @@ -152,36 +142,44 @@ static bool bc_scientific_notation_str2num(
exponent_ptr++;
}

const char *integer_end = integer_ptr + digits;

size_t str_scale = fractional_end - fractional_ptr;
size_t str_full_len = digits + str_scale;
size_t leading_zero_paddings = 0;

if (exponent_sign == PLUS) {
digits += exponent;
str_scale = str_scale > exponent ? str_scale - exponent : 0;

*num = bc_new_num_nonzeroed(digits, str_scale);
(*num)->n_sign = *str == '-' ? MINUS : PLUS;
char *nptr = (*num)->n_value;
char *nend = nptr + digits + str_scale;

*nptr++ = *integer_ptr - '0';
nptr = bc_copy_and_toggle_bcd(nptr, fractional_ptr, fractional_end);
while (nptr < nend) {
*nptr++ = 0;
if (digits == 0) {
leading_zero_paddings = 1;
}
str_scale = str_scale > exponent ? str_scale - exponent : 0;
} else {
digits = 0;
str_scale += exponent;
if (digits > exponent) {
digits -= exponent;
} else {
leading_zero_paddings = exponent - digits + 1; /* 1 is for interger part */
digits = 0;
}
}

*num = bc_new_num_nonzeroed(1, str_scale); // 1 is for 0
(*num)->n_sign = *str == '-' ? MINUS : PLUS;
char *nptr = (*num)->n_value;
*num = bc_new_num_nonzeroed(digits > 0 ? digits : 1, str_scale); /* 1 is for 0 */
(*num)->n_sign = *str == '-' ? MINUS : PLUS;
char *nptr = (*num)->n_value;

for (size_t i = 0; i < exponent; i++) {
for (size_t i = 0; i < leading_zero_paddings; i++) {
*nptr++ = 0;
}

nptr = bc_copy_and_toggle_bcd(nptr, integer_ptr, integer_end);
nptr = bc_copy_and_toggle_bcd(nptr, fractional_ptr, fractional_end);

if (digits > str_full_len) {
/* Fill the rest integer part with zeros */
for (size_t i = 0; i < digits - str_full_len; i++) {
*nptr++ = 0;
}

*nptr++ = *integer_ptr - '0';
nptr = bc_copy_and_toggle_bcd(nptr, fractional_ptr, fractional_end);
}

if (full_scale) {
Expand Down