Bug 154254 - Adding precision is lost when calculation is done by steps
Summary: Adding precision is lost when calculation is done by steps
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
7.5.1.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-03-18 10:43 UTC by Albert Astals Cid
Modified: 2023-03-19 20:48 UTC (History)
0 users

See Also:
Crash report or crash signature:


Attachments
The said file (9.39 KB, application/vnd.oasis.opendocument.spreadsheet)
2023-03-18 10:43 UTC, Albert Astals Cid
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Albert Astals Cid 2023-03-18 10:43:32 UTC
Created attachment 186044 [details]
The said file

See the attached file where I do

=-50+49,6-48+48,34-43+43,06

If the calculation is done all in a single step, it correctly says the result is 0

If done in several steps it says the result is 7,105427357601E-15


Version: 7.5.1.2 (X86_64) / LibreOffice Community
Build ID: 50(Build:2)
CPU threads: 12; OS: Linux 6.2; UI render: default; VCL: kf5 (cairo+xcb)
Locale: ca-ES (ca_ES.UTF-8); UI: ca-ES
7.5.1-1
Calc: threaded
Comment 1 Mike Kaganski 2023-03-18 14:54:48 UTC
This is not a bug.

Floating-point calculations in Calc are imprecise by nature [1]. Every single calculation may (and in most cases does) create a calculation error; sum of these errors would usually be non-0.

However, there are ways to account for the error *inside a series of calculations* - such as Kahan algorithm and its variations [2]. Note that it can only increase precision when during the series of calculations, not only the intermediate results are stored (which have errors every time), but also separate values of errors, which are combined independently, and are added to the final result of the main sum.

Such an algorithm had been implemented in Calc in version 7.2 (bug 137679). So now the calculations that are performed together can benefit from that. But calculations that store intermediate results separately, obviously loose the error information.

[1] https://wiki.documentfoundation.org/Faq/Calc/Accuracy
[2] https://en.wikipedia.org/wiki/Kahan_summation_algorithm
Comment 2 Albert Astals Cid 2023-03-19 19:47:46 UTC
I sorry but this is a somewhat weak answer.

Yes, floating point arithmetic is "broken", but as you said, Libreoffice knows how to do it correctly when doing the calculation in one step, so well, do the calculation in one step ;)

The intermediate results are formulas, there's nothing preventing Calc from evaluating the final formula completely instead of using the already pre-calculated intermediate results. (Speaking from the "I know nothing about this code" point of view obviously, when i say "there's nothing" i mean "in theory it should be possible", no idea how hard it'd be or not in practice)
Comment 3 Mike Kaganski 2023-03-19 20:48:25 UTC
No. It would mean that a change somewhere in the chain would result in complete recalculation, including cells before the changed cell. This is not an option. If one needs increased precision, one needs to use respective formula.