I am unable to get a value when using BigInt.Div() in Go.
Big Ints:
totalAllocPoint, _ := instance_polypup.TotalAllocPoint(&bind.CallOpts{}) //*big.int
fmt.Println("totalAllocPoint: ", totalAllocPoint)// 54000
poolInfoStruct, _ := instance_polypup.PoolInfo(&bind.CallOpts{}, &pid) //*big.int
fmt.Println("allocPoint: ", poolInfoStruct.AllocPoint)// 2000
I have tried to divide using the following code, always returns 0:
poolInfoStruct.AllocPoint.Div(poolInfoStruct.AllocPoint, totalAllocPoint)
fmt.Println("poolAlloc: ", poolInfoStruct.AllocPoint)
poolAlloc := big.NewInt(0).Div(poolInfoStruct.AllocPoint, totalAllocPoint)
fmt.Println("poolAlloc: ", poolAlloc)
poolAlloc := big.NewInt(0)
poolAlloc.Div(poolInfoStruct.AllocPoint, totalAllocPoint)
fmt.Println("poolAlloc: ", poolAlloc)
Int.Div()
is an integer division operation. And you divide poolInfoStruct.AllocPoint
by totalAllocPoint
which is 2000 / 54000
. That will always be 0
, because the divisor is greater than the dividend.
Maybe you want the opposite? totalAllocPoint / poolInfoStruct.AllocPoint
See this example:
totalAllocPoint := big.NewInt(54000)
allocPoint := big.NewInt(2000)
totalAllocPoint.Div(totalAllocPoint, allocPoint)
fmt.Println(totalAllocPoint)
Which output 27
(try it on the Go Playground).
You indicated that divisor and dividend are correct. If so, you cannot use big.Int
for this, as big.Int
can only represent integers.
You may use big.Float
for this for example, and use Float.Quo()
to calculate the quotient.
For example:
totalAllocPoint := big.NewInt(54000)
allocPoint := big.NewInt(2000)
tot := big.NewFloat(0).SetInt(totalAllocPoint)
ap := big.NewFloat(0).SetInt(allocPoint)
tot.Quo(ap, tot)
fmt.Println(tot)
Output (try it on the Go Playground):
0.037037037037037035
Another option is to use big.Rat
and Rat.Quo()
:
tot := big.NewRat(0, 1).SetInt(totalAllocPoint)
ap := big.NewRat(0, 1).SetInt(allocPoint)
tot.Quo(ap, tot)
fmt.Println(tot)
Output (try it on the Go Playground):
1/27