-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'-9/4 x' vs '9/4 x' #2370
Comments
https://mathjs.org/docs/expressions/syntax.html
|
Thanks for your suggestion @fpiou. Your proposal makes sense to me, I'm not sure if this was a conscious decision to choose this behavior. It may be that changing this behavior would break other cases, so we need to be careful. See #792 for the in-depth discussion on this topic. math.parse('9/4x').toString()
"9 / 4 x"
math.parse('2-9/4x').toString()
"2 - 9 / 4 x"
math.parse('-9/4x').toString()
"-9 / (4 x)" -> more logical would be "-9 / 4 x" Anyone interested in trying this out and working out a PR? |
Note that the treatment of |
Now that this is the preferred resolution of #1431, there needs to be a decision as to the extent to which quotients preceding a symbol or a parenthesized expression will bind more tightly and be interpreted as implicit multiplication. Currently only
I am assuming that the parenthesized expressions in (8) should not be interpreted by this special fraction implicit multiplication rule, i.e., they should all parse to |
🤔 Those are interesting examples. I'm trying to extract a generic rule from all the examples that we're sharing here. I think we must understand the intention of people when writing something like So, I think it boils down to the following rule: when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a unary prefix, and (b) the denominator is a constant. This would leave all your examples 1-8 as is because there are not matching this rule (not dealing with a plain constant in the denominator, or not dealing with a plain constant with optional unary prefix in the numerator). Does this reasoning make sense? |
Yes, I was just looking for a clear plan here. The only case I would like to specifically re-check is (3) as that also involves just a unary operator in the numerator: you would like |
👍 Thanks. The new behavior is indeed "inconsistent" in a way, when you look at a postfix operator like Good question if we want to apply this new rule to all postfix operators ( |
I think + and - should be handled the same way. I have no preference as to whether ~ should be the same as these or not, as ~9/4x is just a bizarre expression. (I rarely have occasion to use bitwise negation in the first place). The code might be very marginally simpler if all prefix unary operators are treated the same way. |
yes
Ok let's go for that 👍 . So then the rule can be described as: when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator ( |
As per the discussion in josdejong#2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves josdejong#2370.
As per the discussion in josdejong#2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves josdejong#2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370.
Hi
I have an issue with this because of a different traitement :
math.parse('-9/4 x').toString() ----> '-9 / (4 x)'
math.parse('9/4 x').toString() ----> '9/4 x'
My preference is :
math.parse('-9/4 x').toString() ----> '-9 / 4 x'
Is it an issue or a rule that i don't understand ?
The text was updated successfully, but these errors were encountered: