feat(math): add mutative api for Int.BigInt() (#17803)

This commit is contained in:
Hieu Vu
2023-09-25 12:12:23 +00:00
committed by GitHub
parent 2b22254923
commit 150013ee50
3 changed files with 32 additions and 0 deletions
+4
View File
@@ -36,6 +36,10 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j
## [Unreleased]
### Features
* [#17803](https://github.com/cosmos/cosmos-sdk/pull/17803) Add mutative api for Int.BigInt()
### Bug Fixes
* [#17725](https://github.com/cosmos/cosmos-sdk/pull/17725) Fix state break in ApproxRoot. This has been present since math/v1.0.1. It changed the rounding behavior at precision end in an intermediary division from banker's to truncation. The truncation occurs from binary right shift in the case of square roots. The change is now reverted back to banker's rounding universally for any root.
+8
View File
@@ -86,6 +86,14 @@ func (i Int) BigInt() *big.Int {
return new(big.Int).Set(i.i)
}
// BigInt converts Int to big.Int, mutative the input
func (i Int) BigIntMut() *big.Int {
if i.IsNil() {
return nil
}
return i.i
}
// IsNil returns true if Int is uninitialized
func (i Int) IsNil() bool {
return i.i == nil
+20
View File
@@ -56,6 +56,26 @@ func (s *intTestSuite) TestNewIntFromBigInt() {
s.Require().NotEqual(r, i.BigInt())
}
func (s *intTestSuite) TestConvertToBigIntMutative() {
r := big.NewInt(42)
i := math.NewIntFromBigInt(r)
// Compare value of BigInt & BigIntMut
s.Require().Equal(i.BigInt(), i.BigIntMut())
// Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change
p := i.BigIntMut()
p.SetInt64(50)
s.Require().Equal(big.NewInt(50), i.BigIntMut())
s.Require().Equal(big.NewInt(50), i.BigInt())
// Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change
p = i.BigInt()
p.SetInt64(60)
s.Require().NotEqual(big.NewInt(60), i.BigIntMut())
s.Require().NotEqual(big.NewInt(60), i.BigInt())
}
func (s *intTestSuite) TestIntPanic() {
// Max Int = 2^256-1 = 1.1579209e+77
// Min Int = -(2^256-1) = -1.1579209e+77