Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigNumber.pow doesn't work for negative numbers #925

Closed
rhlsthrm opened this issue Jul 3, 2020 · 4 comments
Closed

BigNumber.pow doesn't work for negative numbers #925

rhlsthrm opened this issue Jul 3, 2020 · 4 comments
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@rhlsthrm
Copy link

rhlsthrm commented Jul 3, 2020

> eth.utils.parseEther('1').mul(eth.BigNumber.from(10).pow(0)).toString()
'1000000000000000000'
> eth.utils.parseEther('1').mul(eth.BigNumber.from(10).pow(-8)).toString()
'100000000000000000000000000'
> eth.utils.parseEther('1').mul(eth.BigNumber.from(10).pow(8)).toString()
'100000000000000000000000000'

Does the function silently use absolute value?

@ricmoo
Copy link
Member

ricmoo commented Jul 3, 2020

That seems like what is happening in your above example and this is certainly a bug. I’ll address it ASAP.

Thanks!

@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. labels Jul 3, 2020
@ricmoo
Copy link
Member

ricmoo commented Jul 5, 2020

It seems negative values are not supported, in general, by BN.js, which kind makes sense in the context of a big number library.

I think the solution will be throwing in the event there is a negative exponent.

Looking more closely at what you are trying to do above, the result you would expect is 0 though. Consider:

// BigNumbers do not support fractions, so this would be 0.00000001, which is 0 to a BigNumber
const a = eth.BigNumber.from(10).pow(-8);

// This times 0 is 0
const b = eth.utils.parseEther('1').mul(a);

What you more likely want to do is use the parseUnits (or if you wish more control, the FixedNumber class directly):

const b = eth.utils.parseUnits("1", 18 - 8).toString();
// "10000000000"

@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels Jul 5, 2020
@ricmoo
Copy link
Member

ricmoo commented Jul 5, 2020

This has been addressed in 5.0.4.

I don't know it will help you the way you want it to, but the FixedNumber and and parseUnits should help you. :)

@ricmoo
Copy link
Member

ricmoo commented Jul 10, 2020

Closing this now, but if you still have problems, please re-open this issue.

Thanks! :)

@ricmoo ricmoo closed this as completed Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

2 participants