-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
ext/bcmath: Supports scientific notation #18068
Draft
SakiTakamachi
wants to merge
4
commits into
php:master
Choose a base branch
from
SakiTakamachi:bcmath/support_scientific_notation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−9
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
30e1f29
WIP
SakiTakamachi dfa260b
Removed the restriction on the integer part of the notation
SakiTakamachi fbe76fc
Added handling for scientific notation when there are no fractional part
SakiTakamachi e3fa32c
Added removing fraction trailing zeros
SakiTakamachi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,96 @@ static inline const char *bc_skip_zero_reverse(const char *scanner, const char * | |
return scanner; | ||
} | ||
|
||
static bool bc_scientific_notation_str2num( | ||
bc_num *num, const char *str, const char *end, const char *integer_ptr, const char *fractional_ptr, const char *exponent_ptr, | ||
size_t digits, size_t *full_scale) | ||
{ | ||
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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
if (UNEXPECTED(*integer_ptr == 0)) { | ||
goto fail; | ||
} | ||
|
||
if (UNEXPECTED(*exponent_ptr != 'e' && *exponent_ptr != 'E')) { | ||
goto fail; | ||
} | ||
exponent_ptr++; | ||
|
||
sign exponent_sign = PLUS; | ||
if (*exponent_ptr == '+') { | ||
/* Skip Sign */ | ||
exponent_ptr++; | ||
} else if (*exponent_ptr == '-') { | ||
exponent_sign = MINUS; | ||
exponent_ptr++; | ||
} | ||
|
||
/* Skip exponent leading zeros. This is rare, so don't do bulk processing. */ | ||
while (*exponent_ptr == '0') { | ||
exponent_ptr++; | ||
} | ||
|
||
const char *exponent_end = bc_count_digits(exponent_ptr, end); | ||
if (UNEXPECTED(*exponent_end != '\0')) { | ||
/* invalid num */ | ||
goto fail; | ||
} | ||
|
||
size_t exponent = 0; | ||
while (exponent_ptr < exponent_end) { | ||
exponent = exponent * 10 + (*exponent_ptr - '0'); /* TODO: check overflow */ | ||
exponent_ptr++; | ||
} | ||
|
||
size_t str_scale = fractional_end - fractional_ptr; | ||
|
||
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; | ||
} | ||
} else { | ||
digits = 0; | ||
str_scale += exponent; | ||
|
||
*num = bc_new_num_nonzeroed(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++) { | ||
*nptr++ = 0; | ||
} | ||
|
||
*nptr++ = *integer_ptr - '0'; | ||
nptr = bc_copy_and_toggle_bcd(nptr, fractional_ptr, fractional_end); | ||
} | ||
|
||
if (full_scale) { | ||
*full_scale = str_scale; | ||
} | ||
|
||
return true; | ||
|
||
fail: | ||
*num = bc_copy_num(BCG(_zero_)); | ||
return false; | ||
} | ||
|
||
/* Assumes `num` points to NULL, i.e. does yet not hold a number. */ | ||
bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, size_t *full_scale, bool auto_scale) | ||
{ | ||
|
@@ -151,9 +241,8 @@ bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, siz | |
|
||
/* validate */ | ||
fractional_end = bc_count_digits(fractional_ptr, end); | ||
if (UNEXPECTED(*fractional_end != '\0')) { | ||
/* invalid num */ | ||
goto fail; | ||
if (*fractional_end != '\0') { | ||
return bc_scientific_notation_str2num(num, str, end, integer_ptr, fractional_ptr, fractional_end, digits, full_scale); | ||
} | ||
|
||
if (full_scale) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is false, e.g.
12e-3
should evaluate to0.012
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nielsdos
I remember that this is what the ISO says, but I may be remembering it wrong...
I'll fix it