expr-eval icon indicating copy to clipboard operation
expr-eval copied to clipboard

Support for BigNum

Open pandronic opened this issue 12 years ago • 5 comments

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?

pandronic avatar Oct 22 '12 08:10 pandronic

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.

silentmatt avatar Oct 22 '12 20:10 silentmatt

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

pandronic avatar Oct 23 '12 05:10 pandronic

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

NexxLuo avatar Mar 02 '21 08:03 NexxLuo

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.

silentmatt avatar Mar 02 '21 16:03 silentmatt

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

xyz-dev avatar Mar 08 '24 03:03 xyz-dev