silentmatt / expr-eval

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

"E" gets converted to 2.718281828459045 #178

Closed dupski closed 5 years ago

dupski commented 5 years ago

Hey guys. Sorry for raising a new issue for this but just searching for "E" is not very helpful :)

We have a formula: C+D+E+F+G/H

Unfortunately expr-eval converts it to ((((C + D) + 2.718281828459045) + F) + G) / H

I'm guessing because https://en.wikipedia.org/wiki/E_(mathematical_constant) ?

How do I stop expr-eval from changing E to 2.7182.... ?

Thanks!

dupski commented 5 years ago

Looks like a workaround is to convert the variable names to Lowercase, so I'll go with that for now, but I'll leave this issue open as it would be good to know / document :)

Thanks for the awesome library :)

silentmatt commented 5 years ago

You are correct that it's replacing it because of Euler's constant, and lowercasing does work around it, because the constant name is E and is case-sensitive.

If you're not going to need it, you can remove it by either deleting E from parser.consts, or replacing the consts field altogether to remove all the built-in constants (E, PI, true, and false).

Here's an example:

var parser = new Parser();
console.log(parser.parse('C+D+E+F+G/H').toString()); // ((((C + D) + 2.718281828459045) + F) + (G / H))

delete parser.consts.E; // or use "parser.consts = {};" to remove everything

console.log(parser.parse('C+D+E+F+G/H').toString()); // ((((C + D) + E) + F) + (G / H))

You're also right that this should definitely be documented :)

dupski commented 5 years ago

https://github.com/silentmatt/expr-eval/pull/179 :)