Bug 143881 - FORMULA: abs{x}^2 is formatted with unexpected operator precedence
Summary: FORMULA: abs{x}^2 is formatted with unexpected operator precedence
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Formula Editor (show other bugs)
Version:
(earliest affected)
7.1.5.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-08-14 22:01 UTC by hardy
Modified: 2023-12-14 03:14 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description hardy 2021-08-14 22:01:06 UTC
Description:
When generating a formula with the content abs{x}^2 it is formated like |x²| and not as expected like |x|².

Steps to Reproduce:
1. open LO impress (or LO writer)
2. generate a new formula object, e.g. via the menu "Insert" - "Object" - "Formula..."
3. type the following code: abs{x}^2 
3b. alternativly: click onto the |o| icon in the "functions" category of the GUI sidebar. Afterwards complete the suggested code fragment "abs{<?>}" to the formula wanted: "abs{x}^2".
4. leave the formular editor to see the layouted result of this formula

Actual Results:
This formula is displayed as: |x²|

Expected Results:
I expected the curly brackets to "stick" the x to the abs function. Therefore I expected the result to look like this: |x|²


Reproducible: Always


User Profile Reset: No



Additional Info:
I suggest to change the operator precedence in this case, such that the curly brackets after the "abs" keyword stick the content of those brackets inside the argument of the abs function. Then this would behave in the same way as "log(x)^2".

Alternativly I suggest to change the output of the formula GUI sidebar to "abs <?>" instead of "abs{<?>}". Removing the curly brackets form the suggested text string would show me as user that I should not expect abs to behave like other functions with argument (e.g. like the above log), but that I should care myself to properly put bracket around, to achieve what I want.
Comment 1 Roman Kuznetsov 2021-08-15 07:59:09 UTC
use {abs{x}}^2 , because you want to have ^2 for abs function and not only for x

Status => NOTABUG

ps: when you use log(x)^2 you'll get logarithm for x^2 again and it's right from mathematics POV
Comment 2 hardy 2021-08-15 19:00:47 UTC
I can accept that you do not want to change the syntax of the formula editor. I was fully aware of this workaround, and just wanted to help improving LibreOffice.

@Roman: But facts cannot be changed by just stating them differently. The input "log(x)^2" is currently (and correctly in my perception) displayed as "log(x)²" by LibreOffice, and not as "log(x²)" like you wrongly claimed in your last comment. (And any math program like matlab or excel, or even any pocket calculator will give you the result for {log(x)}^2 as well for this input.) So the behavior of LO really is as inconsistent as I described it in my original post. Please stick to the truth, even though you have the power to decide according to your own and different preferences.
Comment 3 Roman Kuznetsov 2021-08-15 19:20:20 UTC
(In reply to hardy from comment #2)
> I can accept that you do not want to change the syntax of the formula
> editor. I was fully aware of this workaround, and just wanted to help
> improving LibreOffice.
 

So, possibly we can change this one to an enhancement. 

Dante, what do you think about hardy's ideas?
Comment 4 dante19031999 2021-08-21 01:10:30 UTC
(In reply to Roman Kuznetsov from comment #3)
> (In reply to hardy from comment #2)
> > I can accept that you do not want to change the syntax of the formula
> > editor. I was fully aware of this workaround, and just wanted to help
> > improving LibreOffice.
>  
> 
> So, possibly we can change this one to an enhancement. 
> 
> Dante, what do you think about hardy's ideas?

Problems:
Changing the behavior would break old documents.
Changing the UI would break the visual editor because those brackets keep the sentence together. The abs priority is the same as multiplication, so sums inside of it would exit the abs the moment you type them.

The most I can do is change the code inserted via the UI to { abs { <?> } }.
I leave that decision up to you. The UI change is one line patch.

I also find that feature a bit troubling, however blackguards compatibility is problematic.

And also, log(x)^2 is equivalent to { { log } { { ( x ) } ^ 2 } } for the parser, but the problem is invisible. Math isn't right from mathematics POV, it just looks like it.
Comment 5 Roman Kuznetsov 2021-08-22 09:23:43 UTC
Thanks Dante. 

hardy, I still don't think we should change here anything and I don't think it's a bug also.
Comment 6 hardy 2021-08-22 14:11:34 UTC
That's up to you, the LO team. I've explained my observations and suggestions as a (power) user of LO, not more, not less. If you now decide that there is no feasibly enough way to improve the LO formula module, then that's it.

(There are several other akward behaviors of the formula module, and I will learn to live with this one as well. I think the LO user experience for formulas is much worse than in LaTex, but it is still better then in MS Word.)
Comment 7 Mike Kaganski 2021-08-22 17:27:29 UTC
This is definitely an inconsistent behavior, and a bug. However, fixing this is naturally a compatibility problem.

There are two options:
1. Do what Dante suggests in comment 4 (the braces around the function). Somewhat ugly, but the least intrusive solution keeping backward and forward compatibility.
2. Introduce a compatibility flag, that would signal that abs priority is changed. That would allow to use new precedence in new documents, and allow to keep the formula layout in existing documents without the flag, but will break the formula in older versions of software (or AOO). Also users would face different behavior of formulas in different cases (maybe even inside the same ODT, if two formulas internally have two different flag values).
Comment 8 dante19031999 2021-08-23 01:18:06 UTC
> 2. Introduce a compatibility flag, that would signal that abs priority is
> changed.

That's no so easy. The token list is hardcoded into the code.
It's set up on program initialization, even before the SMMOD even exists.

It could be accessed from a static function, using a bad hack ( const cast + search for TABS, not 100% sure it won't break ). In that case we would add that flag to SmModel::_getPropertyValues and SmModel::_setPropertyValues . It would be initialized as "new mode". And add a trigger on the importer that would set it up to "old mode".

It would need a second flag to check if the first one has the default value or actually was set by the document.

However, there are some things that I'm not confident about.
How will this react while using multiple thread (multiple formulas at same time).
If it has to be thread safe, then I'm not confident it can be done.
Comment 9 Mike Kaganski 2021-08-23 05:53:29 UTC
(In reply to dante19031999 from comment #8)

There's no thread-safety problem, if each *formula* (with its own ODF settings) is managed by a single thread. The flag should be per-document (i.e., per *formula* document) setting.

But your proposal about changing braces around auto-inserted text looks safer and easier.
Comment 10 dante19031999 2021-08-23 13:42:09 UTC
(In reply to Mike Kaganski from comment #9)
> (In reply to dante19031999 from comment #8)
> 
> There's no thread-safety problem, if each *formula* (with its own ODF
> settings) is managed by a single thread. The flag should be per-document
> (i.e., per *formula* document) setting.

It's not the flag itself what could not be thread safe, but the token parser list.
It's defined here: https://opengrok.libreoffice.org/xref/core/starmath/source/parse5.cxx?r=b3b1936e#38
It's common to all parsers.
And it could also have an impact on the element docking window performance.

> But your proposal about changing braces around auto-inserted text looks
> safer and easier.

That change would have this shape:
https://gerrit.libreoffice.org/c/core/+/120867
Comment 11 QA Administrators 2023-12-14 03:14:15 UTC
Dear hardy,

To make sure we're focusing on the bugs that affect our users today, LibreOffice QA is asking bug reporters and confirmers to retest open, confirmed bugs which have not been touched for over a year.

There have been thousands of bug fixes and commits since anyone checked on this bug report. During that time, it's possible that the bug has been fixed, or the details of the problem have changed. We'd really appreciate your help in getting confirmation that the bug is still present.

If you have time, please do the following:

Test to see if the bug is still present with the latest version of LibreOffice from https://www.libreoffice.org/download/

If the bug is present, please leave a comment that includes the information from Help - About LibreOffice.
 
If the bug is NOT present, please set the bug's Status field to RESOLVED-WORKSFORME and leave a comment that includes the information from Help - About LibreOffice.

Please DO NOT

Update the version field
Reply via email (please reply directly on the bug tracker)
Set the bug's Status field to RESOLVED - FIXED (this status has a particular meaning that is not 
appropriate in this case)


If you want to do more to help you can test to see if your issue is a REGRESSION. To do so:
1. Download and install oldest version of LibreOffice (usually 3.3 unless your bug pertains to a feature added after 3.3) from https://downloadarchive.documentfoundation.org/libreoffice/old/

2. Test your bug
3. Leave a comment with your results.
4a. If the bug was present with 3.3 - set version to 'inherited from OOo';
4b. If the bug was not present in 3.3 - add 'regression' to keyword


Feel free to come ask questions or to say hello in our QA chat: https://web.libera.chat/?settings=#libreoffice-qa

Thank you for helping us make LibreOffice even better for everyone!

Warm Regards,
QA Team

MassPing-UntouchedBug