BigNumber.div(divisor) doesn't return the good result #1407
-
Hi,
thanks for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
What is wrong with that values? I just used python to compute these (I often use Python for testing, since it uses arbitrary precision). These are my results (which match): >>> 52240126807063116261284835 / 32446080116944622871630
1610L
>>> 1694987324029278321362138552169957830169752283860 / 32446080116944622871630
52240126324045198977206551L which match ethers BigNumber: homestead> BigNumber.from("52240126807063116261284835").div("32446080116944622871630").toString()
'1610'
homestead> BigNumber.from("1694987324029278321362138552169957830169752283860").div("32446080116944622871630").toString()
'52240126324045198977206551' |
Beta Was this translation helpful? Give feedback.
-
I agree for Python, nevertheless you will see that even if you change 52240126807063116261284835 with 52240126807063116261284942 for example, it is still 1610, the result is truncated/rounded up and I don't know why :( |
Beta Was this translation helpful? Give feedback.
-
BigNumber only operates on integers. Values are rounded toward zero. If you are trying to perform fixed-point maths, you must use the FixedNumber object. They are much more complex to work with though, as you must consider the field in which they are operating. E.g. homestead> FixedNumber.from("52240126807063116261284835", "fixed80x18").divUnsafe(FixedNumber.from("32446080116944622871630", "fixed80x18")).toString()
'1610.059724280260943834' Is that what you were looking for? If so, check out the docs. You should also familiarize yourself with the various caveats of using fixed-point math. There is a lot of complexity when dealing with decimal components... Let me know if that helps. :) |
Beta Was this translation helpful? Give feedback.
-
(moving to discussions) |
Beta Was this translation helpful? Give feedback.
-
First of all thanks for your time, truly appreciated! |
Beta Was this translation helpful? Give feedback.
BigNumber only operates on integers. Values are rounded toward zero.
If you are trying to perform fixed-point maths, you must use the FixedNumber object. They are much more complex to work with though, as you must consider the field in which they are operating.
E.g.
Is that what you were looking for? If so, check out the docs. You should also familiarize yourself with the various caveats of using fixed-point math. There is a lot of complexity when dealing with decimal components...
Let me know if that helps. :)