英文:
Big.Int Div always returns 0
问题
在使用Go语言的BigInt.Div()方法时,我无法获得结果值。
BigInts:
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)
英文:
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)
答案1
得分: 4
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
,并使用 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
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论