[php-src] Issue #12376: possible round() problem
| From: | ks74 | Date: | Fri, 06 Oct 2023 20:30:00 +0000 |
| Subject: | [php-src] Issue #12376: possible round() problem | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245511@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12376
Author: ks74
### Description
The following code:
```php
<?php
$f=-11403.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=-11403.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=-1403.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=-1403.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=-1000.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=-1000.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=-999.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=-999.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=999.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=999.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=1000.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=1000.5449999999992; echo("f:$f ".round($f, 2)."<br>");
$f=10000.5449999999837; echo("f:$f ".round($f, 2)."<br>");
$f=10000.5449999999992; echo("f:$f ".round($f, 2)."<br>");
```
Resulted in this output:
```
f:-11403.545 -11403.55
f:-11403.545 -11403.55
f:-1403.545 -1403.54
f:-1403.545 -1403.55
f:-1000.545 -1000.54
f:-1000.545 -1000.55
f:-999.54499999998 -999.54
f:-999.545 -999.54
f:999.54499999998 999.54
f:999.545 999.54
f:1000.545 1000.54
f:1000.545 1000.55
f:10000.545 10000.55
f:10000.545 10000.55
```
But I expected this output instead:
```
f:-11403.545 -11403.54
f:-11403.545 -11403.54
f:-1403.545 -1403.54
f:-1403.545 -1403.54
f:-1000.545 -1000.54
f:-1000.545 -1000.54
f:-999.545 -999.54
f:-999.545 -999.54
f:999.545 999.54
f:999.545 999.54
f:1000.545 1000.54
f:1000.545 1000.54
f:10000.545 10000.54
f:10000.545 10000.54
```
I did some calculations then output the results using var_dump(), Then I got
-1403.5449999999837
-1403.5449999999992
and to my surprise they were rounded different (0.54/0.55 while should be 0.54 always).
I did some experiments and found out that the problem shows if the integer part is >=1000.
I know how to fix this for me but just in case I report this as a possible bug.
Note that the long fractional parts (.54499999998) displayed by echo in 2 cases are not a problem
for me.
### PHP Version
8.2.0
### Operating System
Windows 8.1