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

PHP 8.4.2 wrong round() #17253

Closed
akademic opened this issue Dec 24, 2024 · 8 comments
Closed

PHP 8.4.2 wrong round() #17253

akademic opened this issue Dec 24, 2024 · 8 comments

Comments

@akademic
Copy link

Description

The following code:

<?php
$numerator = 7380;
$denominator = 28800;
$precision = 2;
$result = round(($numerator / $denominator) * 100, $precision);

echo $result;

Resulted in this output:

25.62

But I expected this output instead:

25.63

Without round we get 25.625, so after round it must be 25.63

PHP Version

PHP 8.4.2

Operating System

No response

@devnexen
Copy link
Member

If that helps, see this one #17136

@remicollet
Copy link
Member

remicollet commented Dec 24, 2024

I don't see anything wrong

$ php -r 'var_dump($a = 7380 / 28800 * 100, round($a, 2));'
float(25.624999999999996)
float(25.62)

25.624999999999996 is properly rounded to 25.62

PHP 8.4 is right here
Indeed 8.3 give a different result

Behavior change is documented
https://www.php.net/manual/en/migration84.other-changes.php

@cmb69 cmb69 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2024
@remicollet
Copy link
Member

Notice teh behavior have changed in 5.2.7 (yes, quite old)
as fixing https://bugs.php.net/bug.php?id=42294
sadly, cannot find the commit... lost during github migration....

@remicollet
Copy link
Member

Floating arithmetic fun:

$ php84 -r 'var_dump($a=round(7380/28800*100,2));'
float(25.62)
$ php84 -r 'var_dump($a=round(7380*100/28800,2));'
float(25.63)

@cmb69
Copy link
Member

cmb69 commented Dec 24, 2024

My favorite is still 0.1 + 0.2 != 0.3.

@urmas-lipso
Copy link

also found this fun situation:
echo (10.075 / 0.05) . ' - ' . round(10.075 / 0.05) . ' - ' . round(201.5);
giving results
with 8.4.x 201.5 - 201 - 202
with 8.2.x and 8.3.x 201.5 - 202 - 202
note the different number in the middle

@TimWolla
Copy link
Member

10.075 actually is 10.0749999999999992895 and 0.05 actually is 0.0500000000000000027756. The division between those is 201.499999999999971578, which is different than 201.5 (which is the next-higher float).

@urmas-lipso
Copy link

presumed something like this, but the problem is result being different for 8.3 and 8.4, that will cause problems while upgrading

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

6 participants