mathjs
mathjs copied to clipboard
Parser fails on "0x"
I'm attempting to port the google/mathsteps library to the current MathJS release. Along the way I've encountered an error I've seen elsewhere in my own code. It's easy to trigger, just do math.parse('0x') and you'll get
Uncaught SyntaxError: String "0x" is no valid number
It seems the parser sees this as a degenerate form of a hex literal and thus rejects it. This feels unnecessarily restrictive as 0x is a frequent thing to represent in algebra. It's come up in my current project, and also in this mathsteps port. From the behavior of the mathsteps tests, it's also clear that this was previously supported in older versions of MathJS.
Fortunately, "0x" is unambiguously not a hex literal as it's not followed by any numbers. I'd suggest special-casing "0x" to interpret it as ConstantNode 0 implicitly multiplied by SymbolNode x, or at least allowing such a behavior to be configured in the math.parser parser constructor. Or better than special-casing, allowing 0x to fall through to the same parsing that would apply to 1x or 2x, should there be no hex literal.
I'd be willing to implement but would like your thoughts on whether this would be appropriate as the default, or as an opt-in option, or not at all :-)
Thanks Josh for bringing this up. It would be nice indeed if the parser could parse 0x as an implicit multiplication, just like it does with say 0y. The same issue holds for 0b (binary) and 0o (octal).
What shall we do with other edge cases like 0xx? I guess it is best to keep the current behavior of throwing an exception, it is a bit of a gray area if you interpret variables xg and xf differently in expressions 0xg (implicit multiplication) and 0xf (hex value).
I think indeed that this will not lead to ambiguity in the parser, should be possible to make it work. I think there is no need for feature toggles.
It would be great if you could implement a solution for this!
@joshhansen Do you success to port the google/mathsteps library to the current MathJS ?
@fpiou I abandoned it for the moment. I did get it to build, but tests are failing, at least due to this issue. https://github.com/LearnSomethingTeam/mathsteps/tree/mathjs10
@joshhansen Thanks for your help. I will try your fork.
I have change some code of mathsteps not to have multiplication implicit in the code. Maybe it is a solution.