Big.Int Div 总是 returns 0
Big.Int Div always returns 0
在 Go 中使用 BigInt.Div() 时无法获取值。
大整数:
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
我尝试使用以下代码进行划分,总是 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()
是整数除法运算。然后用 poolInfoStruct.AllocPoint
除以 totalAllocPoint
即 2000 / 54000
。那将永远是 0
,因为除数大于被除数。
也许你想要相反的东西? totalAllocPoint / poolInfoStruct.AllocPoint
看这个例子:
totalAllocPoint := big.NewInt(54000)
allocPoint := big.NewInt(2000)
totalAllocPoint.Div(totalAllocPoint, allocPoint)
fmt.Println(totalAllocPoint)
输出27
(在Go Playground上试试)。
您表示除数和被除数是正确的。如果是这样,您不能为此使用 big.Int
,因为 big.Int
只能表示整数。
您可以使用big.Float
for this for example, and use Float.Quo()
来计算商数。
例如:
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)
输出(在 Go Playground 上尝试):
0.037037037037037035
另一种选择是使用 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)
输出(在 Go Playground 上尝试):
1/27
在 Go 中使用 BigInt.Div() 时无法获取值。
大整数:
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
我尝试使用以下代码进行划分,总是 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()
是整数除法运算。然后用 poolInfoStruct.AllocPoint
除以 totalAllocPoint
即 2000 / 54000
。那将永远是 0
,因为除数大于被除数。
也许你想要相反的东西? totalAllocPoint / poolInfoStruct.AllocPoint
看这个例子:
totalAllocPoint := big.NewInt(54000)
allocPoint := big.NewInt(2000)
totalAllocPoint.Div(totalAllocPoint, allocPoint)
fmt.Println(totalAllocPoint)
输出27
(在Go Playground上试试)。
您表示除数和被除数是正确的。如果是这样,您不能为此使用 big.Int
,因为 big.Int
只能表示整数。
您可以使用big.Float
for this for example, and use Float.Quo()
来计算商数。
例如:
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)
输出(在 Go Playground 上尝试):
0.037037037037037035
另一种选择是使用 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)
输出(在 Go Playground 上尝试):
1/27