Big.Int Div 始终返回 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


我试图使用以下代码进行划分,始终返回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)


有只小跳蛙
浏览 69回答 1
1回答

守候你守候我

Int.Div() 是一个整数除法运算。你除以哪个是.这将永远是,因为除数大于股息。poolInfoStruct.AllocPointtotalAllocPoint2000 / 540000也许你想要相反的?totalAllocPoint / poolInfoStruct.AllocPoint请参阅此示例:totalAllocPoint := big.NewInt(54000) allocPoint := big.NewInt(2000) totalAllocPoint.Div(totalAllocPoint, allocPoint) fmt.Println(totalAllocPoint)哪个输出(在Go游乐场上尝试)。27您指出除数和股息是正确的。如果是这样,则不能用于此目的,因为只能表示整数。big.Intbig.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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go