Skip to content

round: Bypass the precision logic when rounding to 0 places #12284

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ PHPAPI double _php_math_round(double value, int places, int mode) {
return value;
}

if (places == 0) {
return php_round_helper(value, mode);
}

places = places < INT_MIN+1 ? INT_MIN+1 : places;
precision_places = 14 - php_intlog10abs(value);

Expand Down
27 changes: 27 additions & 0 deletions ext/standard/tests/math/round_gh12143_5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-12143: Test rounding of 1.4999999999999998.
--FILE--
<?php
foreach ([
1.4999999999999998,
-1.4999999999999998,
] as $number) {
foreach ([
'PHP_ROUND_HALF_UP',
'PHP_ROUND_HALF_DOWN',
'PHP_ROUND_HALF_EVEN',
'PHP_ROUND_HALF_ODD',
] as $mode) {
printf("%-20s: %+.17g -> %+.17g\n", $mode, $number, round($number, 0, constant($mode)));
}
}
?>
--EXPECT--
PHP_ROUND_HALF_UP : +1.4999999999999998 -> +1
PHP_ROUND_HALF_DOWN : +1.4999999999999998 -> +1
PHP_ROUND_HALF_EVEN : +1.4999999999999998 -> +1
PHP_ROUND_HALF_ODD : +1.4999999999999998 -> +1
PHP_ROUND_HALF_UP : -1.4999999999999998 -> -1
PHP_ROUND_HALF_DOWN : -1.4999999999999998 -> -1
PHP_ROUND_HALF_EVEN : -1.4999999999999998 -> -1
PHP_ROUND_HALF_ODD : -1.4999999999999998 -> -1
27 changes: 27 additions & 0 deletions ext/standard/tests/math/round_gh12143_6.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-12143: Test rounding of 4503599627370495.5.
--FILE--
<?php
foreach ([
4503599627370495.5,
-4503599627370495.5,
] as $number) {
foreach ([
'PHP_ROUND_HALF_UP',
'PHP_ROUND_HALF_DOWN',
'PHP_ROUND_HALF_EVEN',
'PHP_ROUND_HALF_ODD',
] as $mode) {
printf("%-20s: %+.17g -> %+.17g\n", $mode, $number, round($number, 0, constant($mode)));
}
}
?>
--EXPECT--
PHP_ROUND_HALF_UP : +4503599627370495.5 -> +4503599627370496
PHP_ROUND_HALF_DOWN : +4503599627370495.5 -> +4503599627370495
PHP_ROUND_HALF_EVEN : +4503599627370495.5 -> +4503599627370496
PHP_ROUND_HALF_ODD : +4503599627370495.5 -> +4503599627370495
PHP_ROUND_HALF_UP : -4503599627370495.5 -> -4503599627370496
PHP_ROUND_HALF_DOWN : -4503599627370495.5 -> -4503599627370495
PHP_ROUND_HALF_EVEN : -4503599627370495.5 -> -4503599627370496
PHP_ROUND_HALF_ODD : -4503599627370495.5 -> -4503599627370495