-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Add bcfloor, bcceil and bcround to BCMath #13096
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
44f5e64
RFC: Add bcfloor, bcceil and bcround to BCMath
SakiTakamachi 72b73a6
add value error case to tests
SakiTakamachi 2f3f2e5
Address review comments
SakiTakamachi 7549f57
Address comments 1
SakiTakamachi ad5247f
splitted the test
SakiTakamachi c5cbc18
Changed expected value to STR_PAD_LEFT
SakiTakamachi c5a87bf
Fixed the comment
SakiTakamachi caae7c7
Changed to PHP license
SakiTakamachi 71fdefe
Fixed forgetting to correct the test
SakiTakamachi 34415a1
Fixed test expectations
SakiTakamachi 94c3830
Separated round mode into separate header file
SakiTakamachi 58b2657
add pad
SakiTakamachi 2e2dd10
address comment
SakiTakamachi 963a6b4
fixed config.w32
SakiTakamachi 7c900f0
address comment
SakiTakamachi d227a94
Fix the test case and the test fails
SakiTakamachi a3d08e5
Fixed round.c
SakiTakamachi 9cf63f8
When rounding to a precision finer than num, add 0 at the end
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Saki Takamachi <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#include "bcmath.h" | ||
#include "private.h" | ||
#include <stddef.h> | ||
|
||
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) | ||
{ | ||
/* clear result */ | ||
bc_free_num(result); | ||
|
||
/* Initialize result */ | ||
*result = bc_new_num(num->n_len, 0); | ||
(*result)->n_sign = num->n_sign; | ||
|
||
/* copy integer part */ | ||
memcpy((*result)->n_value, num->n_value, num->n_len); | ||
|
||
/* If the number is positive and we are flooring, then nothing else needs to be done. | ||
* Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */ | ||
if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) { | ||
return; | ||
} | ||
SakiTakamachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* check fractional part. */ | ||
size_t count = num->n_scale; | ||
const char *nptr = num->n_value + num->n_len; | ||
while ((count > 0) && (*nptr == 0)) { | ||
count--; | ||
nptr++; | ||
} | ||
|
||
/* If all digits past the decimal point are 0 */ | ||
if (count == 0) { | ||
return; | ||
} | ||
SakiTakamachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* Increment the absolute value of the result by 1 and add sign information */ | ||
bc_num tmp = _bc_do_add(*result, BCG(_one_), 0); | ||
tmp->n_sign = (*result)->n_sign; | ||
bc_free_num(result); | ||
*result = tmp; | ||
} |
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.
I miss tests for the error cases (this one and a few more below)
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.
Thanks for checking!
I added value error case to tests.
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.
Please add a case with a nul byte in the string. The current API doesn't support them as it basically ignores anything past the nul byte.