[php-src] Issue #12143: Incorrect `round(0.49999999999999994, 0, PHP_ROUND_HALF_UP)` result
| From: | TimWolla | Date: | Wed, 06 Sep 2023 12:58:58 +0000 |
| Subject: | [php-src] Issue #12143: Incorrect `round(0.49999999999999994, 0, PHP_ROUND_HALF_UP)` result | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245346@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12143
Author: TimWolla
### Description
The following code:
```php
<?php
var_dump(round(0.49999999999999994, 0, PHP_ROUND_HALF_UP), 0.49999999999999994);
```
Resulted in this output:
```
float(1)
float(0.49999999999999994)
```
But I expected this output instead:
```
float(0)
float(0.49999999999999994)
```
-----------------------
Extracted from https://github.com/php/php-src/pull/12056#discussion_r1316488935
into a dedicated issue. The solution consists of using
modf() and then explicitly
checking against 0.5, instead of adding or subtracting 0.5, since the
result of the addition / subtraction might not be exactly representable.
/cc @jorgsowa Are you interested in developing the fix?
### PHP Version
8.2.10
### Operating System
_No response_