Replies: 3 comments 5 replies
-
@ricmoo would be very happy, if you could take a quick look at my question 👍 :) |
Beta Was this translation helpful? Give feedback.
-
You may need to look at your unit analysis to understand what you are trying to do. When you multiple you also multiply the units. Usually you would want something "price ( But given that, lets look at the example above:
Then when you use
When you multiple or divide in any unit, you must carry the rest of the operations out in that new unit. Usually we use a unit of 1, so things work out fine, but you have expanded the unit by (10 ** 18). My guess it you want to use FixedPoint Math more than operate on unit-sensitive values, so you can use: > num1 = FixedNumber.from("0.03306254672760674")
FixedNumber { format: "fixed128x18", value: "0.03306254672760674" }
> num2 = FixedNumber.from("6436.466091886065")
FixedNumber { format: "fixed128x18", value: "6436.466091886065" }
> num1.mulUnsafe(num2)
FixedNumber { format: "fixed128x18", value: "212.805960923639361059" } The FixedNumber class performs all the unit correction internally for you. Also note to use strings for your values, not cast numbers to strings, otherwise this will happed: String(1.000000000000000333)
"1.0000000000000002" Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
In my service, number 1 is a factor and number 2 is an actual price. I am trying to use the BigNumbers, What would be best practice to continue doing calculations with these values? And these numbers both come from a contract function, so they are BigNumbers , thats why I thought I can just use the mul function and multiple both of them. This is the thing that I don't really understand. Would I use formatEther to convert them to the unit Ether, and then convert them to a FixedNumber and then do the mathematical operations? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I stumbled upon a weird behavior using the utils bignumber mul function. https://docs.ethers.io/v5/api/utils/bignumber/
From what I saw you can use the mul function to multiply two big numbers. This is exactly what I am trying to do.
I tried to replicate this problem in a simple 4 line of code.
This simple multiplication operation logs the following result 212805960923639361059.2502060781 instead of what I expect: 212.80596....
I saw same behaviors using the div function. Am I using the big number math operations wrong ?
Beta Was this translation helpful? Give feedback.
All reactions