nicolewhite / algebra.js

Build, display, and solve algebraic equations.
http://algebra.js.org
MIT License
1.34k stars 111 forks source link

Arbitrary precision causes errors #93

Open staab opened 5 years ago

staab commented 5 years ago

I'm using algebra.js probably not as intended; I'm using it to actually solve some equations within my application. But it's super helpful, so I'd like to make it work. It appears that numbers with a lot of precision don't remain integers somewhere along the way. The following code:

algebra
  .parse('((23/25C + 297176318895199/312500000000000)*(0.5))-50-((23/25C + 297176318895199/312500000000000)*(0.09))')
  .eval({C: new Fraction(29114907, 5000)})

Gives me an error:

Uncaught TypeError: Invalid Argument (52412560532480240,24414062500000.004): Divisor and dividend must be of type Integer.
    at new Fraction (node_modules/algebra.js/src/fractions.js:12:15)
    at Fraction.copy (node_modules/algebra.js/src/fractions.js:17:12)
    at Fraction.reduce (node_modules/algebra.js/src/fractions.js:21:21)
    at Fraction.add (node_modules/algebra.js/src/fractions.js:77:29)
    at node_modules/algebra.js/src/expressions.js:28:57
    at Array.reduce (<anonymous>)
    at Expression.constant (node_modules/algebra.js/src/expressions.js:28:27)
    at Expression.simplify (node_modules/algebra.js/src/expressions.js:41:28)
    at Expression.add (node_modules/algebra.js/src/expressions.js:73:59)
    at node_modules/algebra.js/src/expressions.js:187:52
CrazyAlien commented 5 years ago

Same happened to me Seems the bigger the fractions it will fallback to real numbers

-142307/7392403695x^3 + 11282782/2464134565x^2 + 31471476791/7392403695x - 112183397497/492826913

algebra.js:1 Uncaught TypeError: Invalid Argument (-154542566.00000003,7392403695.000001): Divisor and dividend must be of type Integer.