question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use operator precedence to determine argument of /, etc

See original GitHub issue

Description

Arno and I have discussed this in the past, but I don’t think I ever put into an issue. Now I have…

@saivan brought up the problem again in a comment in https://github.com/arnog/mathlive/issues/185#issuecomment-489025046:

I’m not sure if I’m in the right place for this. But it seems like typing something like “sinx” followed by “/” should give you \frac{\sin{x}}{ _ }but currently, it gives you \sin \frac{x}{_}. I’d say that when you type “/”, the expectation is that the previous term to the immediate left of your cursor will end up on the top of the fraction.

Similarly for parenthesis, an opening parenthesis somewhere, should put the closing parenthesis (by default), at the complete end of the same scope.

I think the proper thing to do is to use an operator’s precedence in determining the argument to division and scripts and other 2D operators. The MathML operator dictionary is a good place to find relative precedence along with whether an operator is prefix, infix, or postfix, or potentially more than one of those (with different relative precedence values). In the case of sin x / and similarly sin(x) /, the operator dictionary puts function application at 850 and / at 660. This means that sin binds more tightly to it’s argument than does / so that sin x should be the numerator, not x. [I’m not sure why U+2044 (fraction slash) and U+2215 (division slash) are given 265; seems like a mistake]

In order to do the right thing, you really need to parse the expression because you might have something more complicated such as sin^-1 sin x / which should have sin^-1 sin x as the numerator. I suspect that conversion to maston has to deal with this type of issue, so the functionality is at least partially in mathlive already. Years ago (actually now decades, sigh) I did that with the Mathematica frontend’s editor and that seems to have been well accepted. On the other hand, most editors don’t bother with this and I don’t see many complaints, so maybe people just are accepting of what they have to use 😌

FYI: for scripted expressions, the precedence should come from the base of the script (recursively). Typically this might happen with sums and integrals. The above example with sin ^ -1 doesn’t apply here because the operator is function application, not the sin.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saivancommented, May 6, 2019

Yep of course

3 + 4(sin(x) > 3 + 4[sin(x)]/[ __ ]

So for this one when you type the ( to the left of sin, it would implicitly add a bracket to the end of the expression, but when you type /, we destroy the original bracket and make sin(x) the numerator.

A similar thing would happen here:

3 + 4(sin(x) + 5x > 3 + 4(sin(x) + [5x]/[__]

Since we press the forward slash after the 5x term, we would most likely expect the 5x to become the numerator.

I can largely say that this is how you would read these expressions in english. For example, you’d say three plus 4, open bracket, sin x plus five x over .... As a general rule, I’d expect to be able to type the expression in the way I’d read it, because that will avoid the need for me to back track when I realise that I forgot to type something.

0reactions
saivancommented, May 9, 2019

Oh wow, that was fast :p

Read more comments on GitHub >

github_iconTop Results From Across the Web

Operator Precedence - Introduction to Programming in Java
In Java, the operands of an operator are always evaluated left-to-right. Similarly, argument lists are always evaluated left-to-right.
Read more >
C++ Operator Precedence - cppreference.com
Operators that have the same precedence are bound to their arguments in the direction of their associativity. For example, the expression a ...
Read more >
MySQL 8.0 Reference Manual :: 12.4.1 Operator Precedence
The precedence of operators determines the order of evaluation of terms in an expression. To override this order and group terms explicitly, use...
Read more >
Operator precedence - Rosetta Code
Task Provide a list of precedence and associativity of all the operators and constructs that the language utilizes in descending order of precedence...
Read more >
Ask Hackaday: Is There A Legit Use For Operator Precedence?
So it makes total sense that there is Operator Precedence. This is also called Order of Operations, and it dictates which computations will ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found