-
Notifications
You must be signed in to change notification settings - Fork 52
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
Rethink order of operations (multiplication/division) #76
Comments
Also note #62 |
Allowing |
Number followed by the unit name should have the highest precedence, followed by *, /, and then +, - operations. |
@yurivict That would not work with |
The previous approach mostly works well, except it breaks for I don't think there's any way to resolve this elegantly within the parser, because if we replace I don't know if this means we need to special case certain identifiers like I'm glad that the unit tests check the output of the evaluation again, otherwise I wouldn't have caught this! |
Looking into this, I think there are (at least) two reasonable approaches to address this:
I think the latter would make the most sense as it addresses the ambiguous cases, but it would slightly complicate parsing with an extra step. |
Yeah I think that's a good idea and shouldn't be that hard since the environment should be available at that stage. |
I think the most common/expected way to go about this is to prioritize implicit multiplication. Then you have some specific special cases when necessary
To me those three special cases intuitevely feel like special cases too. |
Consider the three operators:
*
/
sin pi
,3 kg
,(2)(4)
or1 1/2
).Current these are all parsed mostly left-to-right, with mostly the same precedence (and a bunch of special cases).
These calculations are correct and should continue working as they do now:
1 3/8 inches
1/2 kg
3pi/2 radians to degrees
-sin (pi/2)
5 feet 2 inches
These examples don't do what the user might expect:
1 meter / 5 seconds
(parsed as(1 meter / 5) * seconds
)sin pi/2
(parsed as(sin pi)/2
)This calculation can't be parsed at all:
3sin 5
(should be equivalent to3 * (sin 5)
)It would be nice to come up with a consistent solution for all these expressions, preferably not involving whitespace.
The text was updated successfully, but these errors were encountered: