Mathjs: TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber

Created on 3 Oct 2018  路  4Comments  路  Source: josdejong/mathjs

Here's my code:

math.config({number: 'BigNumber', precision: 64})
math.eval('80 / day * 5 days')

I'm expecting the result '400'.

It works fine without configuring with BigNumber, but I do need to use BigNumber on my math object in general, as high precision might be important for the calculation and I don't know the user's expression ahead of time. I've tried BigNumbering parts of the expression, but haven't had any luck.

bug

All 4 comments

That's an interesting issue, thanks for reporting. Looks like somewhere in the evaluation a number is introduced (most likely the unit day is represented as a Unit with a value being a number instead of a BigNumber.

This is because of the hard-coded _normalize(1) in divideScalar.js. The 1 needs to be promoted to a BigNumber before being passed to _normalize in order to avoid the error.

I submitted PR #1270 which fixes the bug in this particular case, but I don't know if it's a permanent fix. It seems a little sloppy. I would have rather applied the fix to _normalize itself, but that function doesn't seem to be the source of the bug. There are TODOs in divideScalar.js to move the division into Unit.js, perhaps it's time to do just that?

This issue has been fixed now in v5.2.0. Thanks again @ericman314 !

This issue has been fixed now in v5.2.0. Thanks again @ericman314 !

Confirmed fixed on my end too. Thanks @josdejong & @ericman314!

Was this page helpful?
0 / 5 - 0 ratings