Skip to content

Add 4 new rounding modes to round() function #7

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

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f29511e
Introduce two new modes to round function
jorgsowa Jul 13, 2023
220a450
Slight improvement for HALF_UP rounding
jorgsowa Jul 13, 2023
e1967e8
Refactor
jorgsowa Jul 13, 2023
526ec41
Refactor
jorgsowa Jul 13, 2023
e18af6c
optimize calculation for half-even and half-odd
jorgsowa Jul 18, 2023
3d60d48
Merge remote-tracking branch 'origin/master' into add_modes_up_and_do…
jorgsowa Jul 18, 2023
e26bb34
Update UPGRADING doc with changes to rounding modes for round function
jorgsowa Jul 18, 2023
961cb39
Generated stub for basic_functions_arginfo.h
jorgsowa Jul 18, 2023
9523c7d
Added modes PHP_ROUND_TOWARD_ZERO and PHP_ROUND_AWAY_FROM_ZERO to rou…
jorgsowa Jul 21, 2023
4c43ba9
Introduce two new modes to round function
jorgsowa Jul 13, 2023
c960b0d
Slight improvement for HALF_UP rounding
jorgsowa Jul 13, 2023
027e3e6
Refactor
jorgsowa Jul 13, 2023
2a91eb5
Refactor
jorgsowa Jul 13, 2023
3cf6a32
optimize calculation for half-even and half-odd
jorgsowa Jul 18, 2023
71dbbc3
Update UPGRADING doc with changes to rounding modes for round function
jorgsowa Jul 18, 2023
f6c908d
Generated stub for basic_functions_arginfo.h
jorgsowa Jul 18, 2023
f3ff32c
Added modes PHP_ROUND_TOWARD_ZERO and PHP_ROUND_AWAY_FROM_ZERO to rou…
jorgsowa Jul 21, 2023
49f1e27
Merge branch 'add_modes_up_and_down_to_round_function' of github.com:…
jorgsowa Aug 24, 2023
eb635af
Merge branch 'master' into add_modes_up_and_down_to_round_function
jorgsowa Aug 24, 2023
01356f5
Generate stub
jorgsowa Aug 24, 2023
383be5d
[skip ci] Mark test as XLEAK due to LSAN bug (#12018)
Girgias Aug 23, 2023
4a188b5
Switch asan build to Ubuntu 23.04 in Docker
iluuu1994 Aug 23, 2023
e01a5ac
Implement 4 new rounding modes to round()
jorgsowa Aug 24, 2023
b543ff2
Merge remote-tracking branch 'origin/add_modes_up_and_down_to_round_f…
jorgsowa Aug 24, 2023
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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ PHP NEWS
- Standard:
. Added $before_needle argument to strrchr(). (HypeMC)

- Standard:
. Added 4 new rounding modes to the round(). (Jorg Sowa)

17 Aug 2023, PHP 8.3.0beta3

- Core:
Expand Down Expand Up @@ -155,6 +158,7 @@ PHP NEWS
(Marc Bennewitz)
. Added usage of posix_spawn for proc_open when supported by OS.
(Cristian Rodriguez)
. Added 4 new rounding modes to the round(). (Jorg Sowa)

- Streams:
. Implemented GH-11242 (_php_stream_copy_to_mem: Allow specifying a maximum
Expand Down
20 changes: 20 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@
* @cvalue PHP_ROUND_HALF_ODD
*/
const PHP_ROUND_HALF_ODD = UNKNOWN;
/**
* @var int
* @cvalue PHP_ROUND_CEILING
*/
const PHP_ROUND_CEILING = UNKNOWN;
/**
* @var int
* @cvalue PHP_ROUND_FLOOR
*/
const PHP_ROUND_FLOOR = UNKNOWN;
/**
* @var int
* @cvalue PHP_ROUND_TOWARD_ZERO
*/
const PHP_ROUND_TOWARD_ZERO = UNKNOWN;
/**
* @var int
* @cvalue PHP_ROUND_AWAY_FROM_ZERO
*/
const PHP_ROUND_AWAY_FROM_ZERO = UNKNOWN;

/* crypt.c */

Expand Down
6 changes: 5 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 49 additions & 17 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,56 @@ static inline double php_intpow10(int power) {
static inline double php_round_helper(double value, int mode) {
double tmp_value;

if (value >= 0.0) {
tmp_value = floor(value + 0.5);
if ((mode == PHP_ROUND_HALF_DOWN && value == (-0.5 + tmp_value)) ||
(mode == PHP_ROUND_HALF_EVEN && value == (0.5 + 2 * floor(tmp_value/2.0))) ||
(mode == PHP_ROUND_HALF_ODD && value == (0.5 + 2 * floor(tmp_value/2.0) - 1.0)))
{
tmp_value = tmp_value - 1.0;
}
} else {
tmp_value = ceil(value - 0.5);
if ((mode == PHP_ROUND_HALF_DOWN && value == (0.5 + tmp_value)) ||
(mode == PHP_ROUND_HALF_EVEN && value == (-0.5 + 2 * ceil(tmp_value/2.0))) ||
(mode == PHP_ROUND_HALF_ODD && value == (-0.5 + 2 * ceil(tmp_value/2.0) + 1.0)))
{
tmp_value = tmp_value + 1.0;
}
switch (mode) {
case PHP_ROUND_HALF_UP:
if (value >= 0.0) {
tmp_value = floor(value + 0.5);
} else {
tmp_value= ceil(value - 0.5);
}
break;
case PHP_ROUND_HALF_DOWN:
if (value >= 0.0) {
tmp_value = ceil(value - 0.5);
} else {
tmp_value = floor(value + 0.5);
}
break;
case PHP_ROUND_CEILING:
tmp_value = ceil(value);
break;
case PHP_ROUND_FLOOR:
tmp_value = floor(value);
break;
case PHP_ROUND_TOWARD_ZERO:
if(value >= 0.0) {
tmp_value = floor(value);
} else {
tmp_value = ceil(value);
}
break;
case PHP_ROUND_AWAY_FROM_ZERO:
if(value >= 0.0) {
tmp_value = ceil(value);
} else {
tmp_value = floor(value);
}
break;
case PHP_ROUND_HALF_EVEN:
tmp_value = floor(value + 0.5);
if (tmp_value == value + 0.5 && value == (0.5 + 2 * floor(tmp_value/2.0))) {
tmp_value = tmp_value - 1.0;
}
break;
case PHP_ROUND_HALF_ODD:
tmp_value = floor(value + 0.5);
if (tmp_value == value + 0.5 && value == (0.5 + 2 * floor(tmp_value/2.0) - 1.0)) {
tmp_value = tmp_value - 1.0;
}
break;
default:
ZEND_ASSERT(0 && "Unexpected type");
}

return tmp_value;
}
/* }}} */
Expand Down
18 changes: 17 additions & 1 deletion ext/standard/php_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base);
#endif

#ifndef PHP_ROUND_HALF_DOWN
#define PHP_ROUND_HALF_DOWN 0x02 /* Down == towards zero */
#define PHP_ROUND_HALF_DOWN 0x02 /* Arithmetic rounding, down == towards zero */
#endif

#ifndef PHP_ROUND_HALF_EVEN
Expand All @@ -114,4 +114,20 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base);
#define PHP_ROUND_HALF_ODD 0x04
#endif

#ifndef PHP_ROUND_CEILING
#define PHP_ROUND_CEILING 0x05
#endif

#ifndef PHP_ROUND_FLOOR
#define PHP_ROUND_FLOOR 0x06
#endif

#ifndef PHP_ROUND_TOWARD_ZERO
#define PHP_ROUND_TOWARD_ZERO 0x07
#endif

#ifndef PHP_ROUND_AWAY_FROM_ZERO
#define PHP_ROUND_AWAY_FROM_ZERO 0x08
#endif

#endif /* PHP_MATH_H */
Loading