-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
number_format
no longer round properly big float
#14332
Comments
Seems the affecting commit is this one. cc @SakiTakamachi |
This is not a bug. This change in behavior occurs because the precision that can be rounded has increased by one digit. It may seem odd to round 124 to 13, but Floating point numbers are not very accurate. (edit) In Carbon this may not be useful, but if you can handle the value as a string from the start, you can use |
Thank you. I see, we can deal with that. For the record, the new method var_dump(DateTime::createFromTimestamp('1599828571.235612')); // => ...235611
var_dump(DateTime::createFromTimestamp(1599828571.235612)); // => ...235611 Or to an error when using In If At the moment we use echo number_format(1599828571.235612, 8, '.', ''); Then we get: For instance if we exchange this number via JSON JSON.stringify({created_at: 1599828571.235612}) JSON can properly represent this number with 6-digit, but we get in PHP a I'm now wondering if I should align |
Due to the mechanism of floating point numbers, once a certain precision is exceeded, it becomes impossible to maintain the precision. In PHP, the maximum precision that can be maintained is defined by the constant In this issue, we need up to 16 digits of precision, so float is unstable. Luckily, this is an unreleased feature and not from an RFC, so there may be room to add a string to the argument. |
IMO this is a bug - repro https://3v4l.org/V47rI The |
No, this is not a bug. These behaviors are clearly defined in the RFC. |
I'm not sure to fully understand neither, extending on the @mvorisek example and comparing it to I just want to be sure my understanding is complete so I adapt accordingly the code in my library. Similar question applied to I would also challenge that it's the expected behavior. |
BTW thank you for the link to the RFC, I can see this:
And:
So I'm also wondering how does it work and if it means it's definitely certain it will happen on PHP 8.4 or not. |
Regardless of the literal representations in PHP code, that double value is the value that actually exists. The commit being discussed references PR #12222:
There seems to be misinterpretation here. This is also less than ...5. |
@ranvis Please see: |
@kylekatarnls |
@SakiTakamachi |
There is a link at the beginning of my RFC, the RFC that defines the current behavior is: |
Um... That RFC describes how it works before the change. I'm now under the impression that 1e15 (0x1.c6bf52634p+49) is already around the limit for the mentioned decimal (3.32bit/digit) rounding to work reliably considering double has 52+1 bit mantissa. |
I would appreciate your opinion on this issue :) |
Hmm, what is the actual status of this ticket? It's labeled with "Status: Needs Triage" and "Status: Verified". |
I don't know what the appropriate status would be in this case... I have explained everything that can be explained. But it seems I'm not communicating that very well. |
Sounds like "needing triage" is literally correct... |
I've written to the internals mailing list in the hope to get more feedback. |
For me, this reply clarified things: compare https://3v4l.org/YbUMA and https://3v4l.org/YbUMA/rfc. This is clearly an improvement (aka. fixes very unexpected behavior), so besides clarifying UPGRADING, I think we can stick with the new behavior (and mark this ticket as WONTFIX). |
From where I stand it's hard to understand why fixing round broke the number_format case above and why we need to choose one or the other. Shouldn't it just be correct in both case? |
To clarify: what has been changed is the internal function Now inside Lines 168 to 171 in 10a94f8
The comment is apparently not correct, otherwise https://3v4l.org/YbUMA wouldn't happen. So the |
Description
The following code:
Resulted in this output:
But I expected this output instead:
This is the result with PHP < 8.4 and it sounds good to me either we consider
round()
orfloor()
as the best operation to use.Context: precision matters in my case, as I aim to support float timestamp (possibly more precise than microseconds) then to round them as close as possible and in a consistent manner.
PHP Version
8.4 (master 2024-05-26)
Operating System
Both Ubuntu and Windows
The text was updated successfully, but these errors were encountered: