Skip to content

No matter what "precision" or "epsilon" I set, the result I get from "sqrt" is not rounded up #2612

Answered by josdejong
PaulRBerg asked this question in Q&A
Discussion options

You must be logged in to vote

Executing your expression, I get:

340282366920938463463.37460743176821145599999999999999999999999999999999999999853

Which is indeed the correct answer but contains a rounding error.

The BigNumber data type (powered by Decimal.js) has a higher precision but is still a floating point number type, and hence can have round-off errors.

You can use math.format or math.round to round the result if you want to get rid of the round-off errors:

math.config({precision: 60+18+2})
const x = math.bignumber("115792089237316195423570985008687907853269984665640564039457.584007913129639935")
const y = math.bignumber("0.000000000000000001")
const result = math.sqrt(x.mul(y))
// "340282366920938463463.37460…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@PaulRBerg
Comment options

Answer selected by PaulRBerg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants