我无法在 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我试图使用以下代码进行划分,始终返回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)
1 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
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)
哪个输出(在Go游乐场上尝试)。27
您指出除数和股息是正确的。如果是这样,则不能用于此目的,因为只能表示整数。big.Int
big.Int
你可以使用大。例如,浮点
数,并使用浮点数.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游乐场上尝试):
0.037037037037037035
tot := big.NewRat(0, 1).SetInt(totalAllocPoint) ap := big.NewRat(0, 1).SetInt(allocPoint) tot.Quo(ap, tot) fmt.Println(tot)
输出(在Go游乐场上尝试):
1/27
- 1 回答
- 0 关注
- 68 浏览
添加回答
举报
0/150
提交
取消