Skip to content
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

possible round() problem #12376

Closed
ks74 opened this issue Oct 6, 2023 · 1 comment
Closed

possible round() problem #12376

ks74 opened this issue Oct 6, 2023 · 1 comment

Comments

@ks74
Copy link

ks74 commented Oct 6, 2023

Description

The following code:

<?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

@damianwadley
Copy link
Member

See #12143

@damianwadley damianwadley closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants