Bug 145750 - Arithmetically 2-1 not always equal to 1?
Summary: Arithmetically 2-1 not always equal to 1?
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
7.1.6.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-18 09:25 UTC by gmarco
Modified: 2021-11-19 09:25 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
calc sheet sample test (14.72 KB, application/vnd.oasis.opendocument.spreadsheet)
2021-11-18 09:25 UTC, gmarco
Details

Note You need to log in before you can comment on or make changes to this bug.
Description gmarco 2021-11-18 09:25:39 UTC
Created attachment 176330 [details]
calc sheet sample test

I note the following: subtracting two values the result is not always accurate.
An example: 3912,24000000000000-3892,96000000000000 = 19,27999999999970
the result should be 19,28 but a comparison =if((x-y)=19,28;"OK";"notOK") gives the result "notOK".
(see attached test, noting that the decimal separator is comma (IT))
Comment 1 Mike Kaganski 2021-11-18 09:50:05 UTC
2 - 1 is 1, but a.y - b.y is not necessarily what you expect - see FAQ [1].

Closing NOTABUG.

[1] https://wiki.documentfoundation.org/Faq/Calc/Accuracy
Comment 2 Rainer Bielefeld Retired 2021-11-18 12:05:59 UTC
(In reply to Mike Kaganski from comment #1)
Yes, such inaccuracies are inevitable in <https://en.wikipedia.org/wiki/Floating-point_arithmetic> and not a LibO Problem.

Some days ago That caused a very annoying CALC Filter problem for me, but I did not find the time for deeper investigations.
Comment 3 gmarco 2021-11-18 15:07:12 UTC
Ok, it will be inevitable but, often using comparisons between values and obtaining results that are not always reliable but on which reliability is important to continue in other cascade operations,
what can be done to overcome the problem?
Comment 4 Mike Kaganski 2021-11-18 15:24:54 UTC
(In reply to gmarco from comment #3)

As mentioned in FAQ (see comment 1):

> Users need to account for that, and use rounding and *comparisons with epsilon* as necessary

=IF(ABS((x-y)-19)<1E-10;"OK";"notOK")

... or any other value suitable in this case (for different values, value of epsilon may vary).
Comment 5 gmarco 2021-11-18 16:18:01 UTC
> 
> =IF(ABS((x-y)-19)<1E-10;"OK";"notOK")
> 
> ... or any other value suitable in this case (for different values, value of
> epsilon may vary).

Well, I'll try changing the formulas, I generally use the comparison "= 0" or "> 0" or "<> 0"
and I hope that fitting them with "<1E-10" is OK.
Comment 6 gmarco 2021-11-19 09:25:39 UTC
Thanks, the formula works and can be simplified like this:

     x                       y                     x-y = z		
3912,24000000000000     3892,96000000000000     19,27999999999970
that is
   x	       y       x-y = z		
3912,24     3892,96     19,28     =IF(x-y=ABS(z);"OK";"notOK")