forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFC] Add bcfloor, bcceil and bcround to BCMath (php#13096)
Implementation for the "Adding bcround, bcfloor and bcceil to BCMath" RFC: https://wiki.php.net/rfc/adding_bcround_bcfloor_bcceil_to_bcmath * Separated round mode into separate header file Co-authored-by: Gina Peter Banyard <[email protected]>
- Loading branch information
1 parent
78ec2bd
commit 5359392
Showing
27 changed files
with
1,500 additions
and
38 deletions.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -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; | ||
} | ||
|
||
/* 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; | ||
} | ||
|
||
/* 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.