silentmatt / expr-eval

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

Support for BigNum #10

Open pandronic opened 11 years ago

pandronic commented 11 years ago

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?

silentmatt commented 11 years ago

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.

pandronic commented 11 years ago

It's unfortunate that floats are implemented as JS numbers.Without changing that, I don't know what benefit would using BigNum bring ...

NexxLuo commented 3 years ago

Could you share your new parser with me ? Thanks. I really need the parser fixed the IEEE 754 issue.

silentmatt commented 3 years ago

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.

xyz-dev commented 6 months ago

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());