-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Update Sum.php #4441
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
base: master
Are you sure you want to change the base?
Update Sum.php #4441
Conversation
We have a cell in the example.xlsx sheet containing a total sum as a formula: E1 = IF(SUM(A1:B1) = SUM(C1:D1), "true", "false"), where: A1 = 1.1, B1 = 2.7, C1 = 1.8, D1 = 2. We expect the calculation result to be E1 = "true", but instead, we get E1 = "false" because in PHP, the addition yields: 1.1 + 2.7 = 3.8000000000000003, 1.8 + 2 = 3.8. The idea of the fork is to accept an input parameter specifying the number of decimal places and round the final sum to that precision before comparison. Additional Notes: This issue arises due to floating-point precision errors in programming languages like PHP. A possible solution is to use rounding (e.g., round(SUM(A1:B1), N) == round(SUM(C1:D1), N) where N is the desired decimal precision).
Sorry, this will probably not be implemented. You are hitting too small a target in a game of whack-a-mole. Your fix works fine for:
It does nothing for:
And if you patch that, there are many other possibilities which will continue to fail. There are risks in using floating-point. These are well documented in the Php manual, and manuals for most other languages. If you want to compare floats, you'll have to take precautions. For example, your cell formulas could use the following:
(Where 5 and 0.00001 are, of course, totally arbitrary choices.) |
Now that I think about it, your problem really isn't with the SUM function, it is with the |
There is already a tolerance (0.1E-12) built into comparison operations, so no need for a change. I will, however, add that, because of this already existing constant, I do not duplicate your results: $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1.1);
$sheet->getCell('B1')->setValue(2.7);
$sheet->getCell('C1')->setValue(1.8);
$sheet->getCell('D1')->setValue(2);
$sheet->getCell('E1')->setValue('= IF(SUM(A1:B1) = SUM(C1:D1), "true", "false")');
var_dump($sheet->getCell('E1')->getCalculatedValue()); My result:
Do you get a different result with this code? If so, please let me know Php and PhpSpreadsheet versions. |
Have a great day! I'll check it on my side and send you an exact example a bit later. |
We have a cell in the example.xlsx sheet containing a total sum as a formula: E1 = IF(SUM(A1:B1) = SUM(C1:D1), "true", "false"), where:
A1 = 1.1,
B1 = 2.7,
C1 = 1.8,
D1 = 2.
We expect the calculation result to be E1 = "true", but instead, we get E1 = "false" because in PHP, the addition yields:
1.1 + 2.7 = 3.8000000000000003,
1.8 + 2 = 3.8.
The idea of the fork is to accept an input parameter specifying the number of decimal places and round the final sum to that precision before comparison.
Additional Notes:
This issue arises due to floating-point precision errors in programming languages like PHP.
A possible solution is to use rounding (e.g., round(SUM(A1:B1), N) == round(SUM(C1:D1), N) where N is the desired decimal precision).
This is: