expr-eval
expr-eval copied to clipboard
Support for BigNum
Have you considered adding support for a library such as
https://github.com/jtobey/javascript-bignum
in order to overcome JS's IEEE 754 limitations?
The original reason for writing the expression parser/evaluator was for generating function graphs, so that wasn't an issue. That is a good idea though, and shouldn't be too hard.
I'm pretty familiar with the javascript-bignum library because I use it in a more full-featured calculator with a different, more powerful evaluation engine. I also wrote the BigInteger class that it uses under the hood ;)
Keep in mind that it's still easy to run into IEEE 754 issues, because floating point numbers are still implemented as native JavaScript numbers. The advantage is that integers and rational numbers are exact (in addition to supporting things like complex numbers).
I've mostly abandoned this parser since I wrote the new one, but I'll look at adding bignum support when I get a chance.
It's unfortunate that floats are implemented as JS numbers.Without changing that, I don't know what benefit would using BigNum bring ...
Could you share your new parser with me ? Thanks. I really need the parser fixed the IEEE 754 issue.
The other parser is part of a larger project, and at least as it exists now, can't be separated into it's own library. It's also still using IEEE 754 floats, so it sounds like it's still not what you're looking for. There are probably expression parsers that use decimal or other types of bignums, but I'm not aware of any specifically.
const parser = new Parser(); // like this parser.binaryOps['+'] = function (a, b) { return arith.add(a, b); }; const expr = parser.parse('0.1 + 0.2'); console.log(expr.simplify({}), expr.evaluate());