-
Notifications
You must be signed in to change notification settings - Fork 272
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
SUM function has calculation precision problems #1471
Comments
Thanks for your report! We've verifed the issue and are looking into it. |
Hi @KarasKsenia, Kahansum does unfortunately not remove all IEEE 754 floating point errors, what it does best is to avoid that the floating point error grows when summarizing larger datasets. When using When we are checking for equality with doubles that has been used in numeric operations ourselves we rather use a method like this: static double precision = 0.000000000000001;
bool Equals(double lnum, double rnum)
{
return (Math.Abs(lnum - rnum) < precision);
} |
Hi, thanks for your answer! Is there any chance that in future SUM function will use decimal instead of double? I suppose KahanSum works good for larger datasets, but basically SUM function should work as expected for any dataset size. |
Hi, We are not planning to replace
What we have discussed and might do in the future is to implement "Precision as displayed" for cell values. In such a feature we would round numeric values according to the format of the cell (which will have some performance issues but still might be valuable). You can also replace the SUM function provided by EPPlus with your own custom implementation that uses Decimal instead of Double if you like. See this wiki link and the source code for our implementation of SUM. |
Hi all, https://stackoverflow.com/questions/78580381/c-sharp-epplus-calculation-is-different-from-excel I just wanted to provide some information for reference. (or this is a test case) |
EPPlus usage
Commercial use (I have a commercial license)
Environment
Windows
Epplus version
7.1.3
Spreadsheet application
Excel
Description
Hi everybody!
We discussed this issue previously, all the info can be seen here #767. The main case is that SUM function had precision error and provided unexpected results, for example when adding 0.2 and 21.9, the result was less than 22.1.
The KahanSum algorithm was implemented in the new major version of EPPlus (v7) to handle this kind of issues. Unfortunately after some simple testing it looks like the issue wasn't handled and there is still no way to get stable precise sum results for very simple equations. The test, that we used, is here:
The second Assert fails because the sum of 0.2 and 21.9 is somehow not equal to 22.1, as expected.
![image](https://private-user-images.githubusercontent.com/170731948/336042015-42b7e648-3a46-4763-bff5-c34dfa4319e0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0NDA2MjEsIm5iZiI6MTcyMTQ0MDMyMSwicGF0aCI6Ii8xNzA3MzE5NDgvMzM2MDQyMDE1LTQyYjdlNjQ4LTNhNDYtNDc2My1iZmY1LWMzNGRmYTQzMTllMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcyMFQwMTUyMDFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jNWVkN2YwYTU3NmMyYWU0Y2Y1NWZhNTkzMTRlOTljODFiNjQ4NTYwNzIxOGY2NmUzODFkZGNkMTEwMzczYjMxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.7k4SGYiVHf3JP1c_Kuc-n9Jo-J1k-yX8SWdYDvDCrLY)
I checked the source code, and despite the algorithm looks perfect and basically works, the main part of the precision error handling seems not working. I'm talking of this part:
Unfortunately, these low-order digits (variable _c), which are lost after adding up two doubles, have no impact on the value of the KahanSum. The returned result is always equal to the value of the _sum variable, no matter whether _c has a value greater than 0 or not.
The text was updated successfully, but these errors were encountered: