silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.18k stars 239 forks source link

Exponential operator #229

Open iserranoe opened 4 years ago

iserranoe commented 4 years ago

The exponential operator used with Parser is ^; however, javascript exponential operator is . Is there a way for Parser to accept as exponential operator?

//Standard JavaScript exponential
> 2**3
8
//eval function exponential
> eval('2**3')
8
//Not working with Parser
> Parser = require('expr-eval').Parser
[Function: Parser] { parse: [Function], evaluate: [Function] }
> Parser.evaluate('2**3')
Thrown:
Error: unexpected TOP: *
     .....
//Working with other operator
> Parser.evaluate('2^3')
8
silentmatt commented 4 years ago

That's a good idea. I'm not a big fan of having multiple syntaxes for the same thing, but especially with JavaScript using **, that would align with people's expectations. We could make it an option, but the extra complexity doesn't seem worth it, and it's easy enough to support both. There also shouldn't be any backward-compatibility issues, since ** is current a syntax error.