MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.63k stars 743 forks source link

[QUESTION] Trying to multiply with more significant digits returns 0 #330

Closed munanadi closed 2 years ago

munanadi commented 2 years ago

This is the calc I'm trying to replicate using the library

4.035226259175442e+25 * 10**-24 = 40.3522..
new Big(4.035226259175442e+25).mul(new Big(10).pow(-24)) // This returns 0
new Big(4.035226259175442e+25).mul(new Big(10).pow(-21)) // THIS WORKS! Returns 4035.. something

Am I doing something wrong? Is there a limit on the max numbers that I can use? Any pointers will help. Thank you!

shuckster commented 2 years ago

You need to work with strings, not numbers.

MikeMcl commented 2 years ago

For starters, you're in the wrong repo. I assume you're using big.js not bignumber.js.

Anyway, here's a pointer to the pow documentation. You should be able to work it out from there.

Hint: you need to change Big.DP.

And, yes, you should be passing numbers with more than a few digits as strings.

munanadi commented 2 years ago

Apologies. I should have written the explicit code. I did change the necessary methods calls to suit this library. Thanks for your help :+1: