Bug 146915 - LEFT and RIGHT functions inconsistent behaviour with numerals
Summary: LEFT and RIGHT functions inconsistent behaviour with numerals
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
7.2.5.2 release
Hardware: x86-64 (AMD64) Windows (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-22 09:33 UTC by Peter Smith
Modified: 2022-01-22 10:02 UTC (History)
0 users

See Also:
Crash report or crash signature:


Attachments
Simple calc sheet to demonstrate the inconsistency (12.28 KB, application/vnd.oasis.opendocument.spreadsheet)
2022-01-22 09:33 UTC, Peter Smith
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Smith 2022-01-22 09:33:30 UTC
Created attachment 177699 [details]
Simple calc sheet to demonstrate the inconsistency

Comparing the left hand (or right hand) digit of a number with a digit returns a different result depending on the formula structure, even though both structures are mathematically identical ie:
=A=B reports FALSE, whereas =A-B=0 reports TRUE
So in my spreadsheet:
=LEFT(B4)=A$2 reports FALSE (incorrectly) whereas 
=LEFT(B4)-A$2=0 reports TRUE (correctly)

I presume it's associated with the function being designed for a string (and I checked on the attached test sheet and letters work as expected) but the above still suggests an inconsistency when dealing with numbers.  I changed the format of the numbers to text but it didn't alter the results
Comment 1 Mike Kaganski 2022-01-22 10:02:30 UTC
This is not a bug.
Your A2 contains a *number* (manually formatted to left-align). LEFT(B4) returns a string. An equality operator returns FALSE when comparing strings with numbers, and that is the correct behavior: '="4"=4' must return FALSE; see ODF standard's 6.4.7 Infix Operator "=" [1] requiring that behavior explicitly.

However, when you calculate '="4"-4=0', you first perform negation, which uses implicit conversion (documented in 6.3.5 Conversion to Number [2]). Note how it's implementation-defined if strings get converted to numbers; and Calc follows all major spreadsheet software in converting the strings here. So "4" gets converted to 4, the negation is calculated, numeric result is 0, and it correctly compares as equal to 0.

Whenever you use text functions, and use their results in numeric calculations, you must convert the string to number directly to avoid confusion. Use e.g. VALUE function [3]. 

[1] https://docs.oasis-open.org/office/OpenDocument/v1.3/OpenDocument-v1.3-part4-formula.html#Infix_Operator_EQ
[2] https://docs.oasis-open.org/office/OpenDocument/v1.3/OpenDocument-v1.3-part4-formula.html#ConversionToNumber
[3] https://help.libreoffice.org/latest/en-US/text/scalc/01/04060110.html?DbPAR=CALC#bm_id3150802