-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Floats rounds down #12075
Comments
What does |
Apparently somehow it is below 7.5 in the new version.
This is the output in php 8.2.5
|
It sounds like normal floating-point problems: before you were receiving something that was processed and resulted in 5.75, while now you're receiving something that hasn't been processed for you. The actual number instead of some stringified/unstringified value. Which means you need (needed) to be the one doing the processing - namely, rounding it to the precision that you expect the data to be first, in order to get an accurate representation of what the data was intended to be (because floating-point numbers are fun like that), and then doing the desired rounding to the precision you want for display. |
Yeah I am in the process of trying to fix these precision errors. This is an old project I inherited and the code-base it huge. I just didn't expect this sort of change in a minor version. It works if I set ATTR_EMULATE_PREPARES to false. I assume I'ts not going to be changed back, so I suppose its time to give it another shot at refactoring all calculations :) |
<?php
ini_set('precision', 14);
ini_set('serialize_precision', 17);
var_dump(ini_get('serialize_precision'));
$a = 5.749999999999966;
$b = round($a, 1);
var_dump(number_format($a, 1), $b);
ini_set('precision', 14);
ini_set('serialize_precision', 17);
echo $b;
echo "\n";
var_dump($b);
?> Expected result and not use more precise float RFC serialize_precision and precision set to -1 (for this example):
round use serialize_precision (set 17) and $precision (this example is 1). |
GH-11587 was originally keep backward compatible change before PHP 8.1. Therefore, it was fixed. |
@youkidearitai yeah I can see that, yet all tests passed in php 8.1. But I have rewritten the logic now so it works, but others might run into similar issues |
@sinnbeck I see. @SakiTakamachi Any thoughts? |
@sinnbeck For reference, this is what happens in my environment.
|
The column is a double.
And it is doing a SUM() on a bunch of rows. The value 5.75 does not come directly from here, but rather is the output of several php calculations done with the output. I am currently trying to find a way of make a simple reproducible example. |
it's ok @sinnbeck list all the steps, there is "fixed point" which means write by pen. |
@hormus thanks. That was my first thought as well but it only happens when both ATTR_EMULATE_PREPARES and ATTR_STRINGIFY_FETCHES is set to false. So I assume it's due to the fix of the trailing zeroes in pdo |
Ah I see. It may be a bit difficult, but is it possible to perform a similar test with php8.0? |
@SakiTakamachi If I manage to find a way to test it I can easily test it in both 8.1 and 8.0 using docker if needed. |
But as I said, I have already solved the issue for the app, so if this is how it should be, I completely understand and the issue can be closed 😊👍 |
ini_set('serialize_precision', 14);
var_dump(36.8 +
19.555555555556 +
10.3 +
23.0 +
7.7222222222222 +
19.555555555556 +
4.0 +
5.6666666666667 +
5.375 +
7.7222222222222 +
5.2222222222222 +
4.0 +
5.375 +
5.375 +
4.0 +
9.3333333333333 +
5.75 +
5.375 +
9.8 +
10.3 +
5.375 +
5.75 +
5.375 +
36.8 +
9.3333333333333 +
19.555555555556 +
19.555555555556 +
36.8 +
36.8 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
10.3 +
10.3 +
10.3 +
10.3 +
10.3 +
10.3 +
5.2222222222222 +
5.2222222222222 +
5.2222222222222 +
5.2222222222222 +
5.75 +
5.75 +
5.75 +
5.75 +
5.75 +
5.75 +
5.6666666666667 +
5.6666666666667 +
5.6666666666667 +
5.6666666666667 +
5.6666666666667 +
5.6666666666667 +
5.6666666666667 +
5.375 +
5.375 +
17.666666666667 +
17.666666666667 +
17.666666666667 +
17.666666666667 +
17.666666666667 +
17.666666666667 +
9.3333333333333 +
9.3333333333333 +
9.3333333333333 +
9.3333333333333 +
9.3333333333333 +
9.3333333333333 +
23.0 +
23.0 +
23.0 +
23.0 +
9.8 +
9.8 +
9.8 +
9.8 +
9.8 +
9.8 +
9.8 +
4.0 +
4.0 +
4.0 +
4.0 +
3.6666666666667 +
3.6666666666667 +
3.6666666666667 +
3.6666666666667 +
3.6666666666667 +
3.6666666666667 +
9.8 +
7.7222222222222 +
4.0 +
9.3333333333333 +
10.3 +
9.8 +
19.555555555556 +
5.6666666666667 +
5.2222222222222 +
19.555555555556 +
19.555555555556 +
19.555555555556 +
19.555555555556 +
36.8 +
7.7222222222222 +
7.7222222222222 +
7.7222222222222 +
10.3 +
5.2222222222222 +
5.2222222222222 +
5.2222222222222); Expected result is 1246, with serialize_precision -1 or 17 is 1246.0000000000052 https://wiki.php.net/rfc/precise_float_value for OLD behavior |
@hormus Nice. But as it changes based on whether https://www.php.net/ChangeLog-8.php
Anyways. I assume it's expected and that I can close the issue ? |
Regardless of the values of
So this doesn't seem like a mysql or pdo issue. |
@SakiTakamachi Thanks for trying it out. Its really stange. I am testing on the php:8.2.9-fpm from docker, and I get it every time.. Here I am connecting to the same database twice and second time I set ATTR_EMULATE_PREPARES to false It is just doing the query and
Maybe it is just some weird interoperability between my maridadb database and PDO. |
Ok I just tested on 8.1.22. Apparently my old tests on 8.1 much have been on an earlier version as the that specific version gives the exact same output as 8.2.9. So I think I will close this issue for now, and if others run into similar things, it can perhaps be reopened. |
Description
The following code:
Resulted in this output:
But I expected this output instead:
This happens after upgrading to php 8.2.9 (I have tested all versions down to 8.2.5)
I believe it is caused by this fix
PDO::ATTR_EMULATE_PREPARES is true and PDO::ATTR_STRINGIFY_FETCHES is true
PHP Version
PHP 8.2.9
Operating System
Ubuntu 20.04
The text was updated successfully, but these errors were encountered: