-
-
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
Parsing unit fails with parentheses and exponents; documentation unclear #2771
Comments
Thanks for the detailed report! The only thing I am unsure of is the exact specification of the unit "language" that Unit is designed to handle. Your examples work in the mathjs expression language because mathjs is perfectly happy to do arithmetic on "pure" units without numeric values. So perhaps @josdejong or @ericman314 can comment on whether your examples are expected to work directly in the Unit class parser. |
The unit class parser is not as advanced as math.evaluate(). It can't do nested parentheses, multiple / operators, etc. In fact, I think parentheses might be ignored altogether. The parser only looks for a single / to separate the units in the numerator from the units in the denominator. And a ^ has to directly follow a unit--you can't have a ) in between. Hope that helps. |
Thanks, @gwhitney @ericman314 for the fast response. So if I understand it correctly this is not a bug but rather a limitation of the unit parser. I will close this issue then and change my usage to rely on |
@ericman314 Would it be an idea to adjust the documentation on the https://mathjs.org/docs/datatypes/units.html page? It already includes notes on the limitations of the |
Hmm. Looking at that page, the documentation on what syntax is supported doesn't agree with what I told you earlier. I might have been wrong. Without digging in deeper, I can't say. It you would like to submit an improvement or clarification for that documentation, I'm sure it would be most welcome. |
As far as I can see, the documentation is unclear on this point, so I now feel that there is definitely a problem here. Either:
Hence, reopening the issue. |
Of course, to address this, there needs to be a decision between (1) or (2) from the last comment. Again perhaps @josdejong can weigh in or we can wait until Eric has a bit more time to look into this as to which way would be better to go. |
One thing to add to the (1) or (2) discussion: |
Well, I think this is a separate issue, the clash between certain unit names and standard function names in mathjs expressions. I mean, "1 hr" parses but neither of "1 min" or "1 sec" do (because of the minimum and secant functions). And I really don't know what if anything is to be done about that; you could try to disambiguate by context, but I think that's wading into a quagmire. Right now as far as I know you just have to use other symbols for those units, like "minute" and "s". If you have a specific proposal for when the mathjs expression parser should prefer a unit to a function symbol, feel free to open a separate issue (or if your idea is not quite concrete or fully detailed, maybe open a Discussion about it instead). Thanks! |
Yes good points. I think we should improve the documentation for now, that is very little work. The parser of |
The point about |
Well currently in the mathjs expression parser, 'min' and 'sec' are unambiguously minimum and secant, so to get the units you need to write out minute and use either s or second, both of which it recognizes as seconds. But in the Unit parser, min and sec mean minutes and seconds. So there is that inconsistency. As for whether to extend the Unit parser to handle exponents in more general positions than directly on a unit, I guess @jasonhornsby and I need guidance from someone as to that or to document the limitation. Thanks. |
Yes that is a good point, well explained. Shall we should remove
|
Good, at least it is clear what to do here for this issue at this time: document the quirks in mathjs/Unit interaction as they currently stand. My vote is not to remove min and sec from Unit, just document that math overrides them in expressions, since you can still certainly do math.unit('10sec') and I don't see any reason to disable that. |
👍
Right now |
Describe the bug
When trying to parse the unit
lbf/(mile/h)^2
a SyntaxError is thrown.SyntaxError: Unexpected "^" in "lbf/(mile/h)^2" at index 12
This is unexpected since the unit should be valid and math.evaluate() can parse it without issue (see interactive prompt on https://mathjs.org/)
Narrowing down the issue it seems to be related to the
^
after the brackets.To Reproduce
These tests reproduce the issue.
The original unit
Only squared brackets:
The text was updated successfully, but these errors were encountered: