types: add Abs() method to sdk.Int (#8963)

Fixes #8962

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
This commit is contained in:
Hanjun Kim
2021-03-24 16:33:55 +00:00
committed by GitHub
co-authored by Alessio Treglia
parent 56c312a979
commit 8ee9da775e
3 changed files with 11 additions and 0 deletions
+7
View File
@@ -37,6 +37,8 @@ func mod(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Mod(i, i2) }
func neg(i *big.Int) *big.Int { return new(big.Int).Neg(i) }
func abs(i *big.Int) *big.Int { return new(big.Int).Abs(i) }
func min(i *big.Int, i2 *big.Int) *big.Int {
if i.Cmp(i2) == 1 {
return new(big.Int).Set(i2)
@@ -304,6 +306,11 @@ func (i Int) Neg() (res Int) {
return Int{neg(i.i)}
}
// Abs returns the absolute value of Int.
func (i Int) Abs() Int {
return Int{abs(i.i)}
}
// return the minimum of the ints
func MinInt(i1, i2 Int) Int {
return Int{min(i1.BigInt(), i2.BigInt())}
+3
View File
@@ -159,6 +159,8 @@ func (s *intTestSuite) TestArithInt() {
{sdk.MinInt(i1, i2), minint(n1, n2)},
{sdk.MaxInt(i1, i2), maxint(n1, n2)},
{i1.Neg(), -n1},
{i1.Abs(), n1},
{i1.Neg().Abs(), n1},
}
for tcnum, tc := range cases {
@@ -206,6 +208,7 @@ func (s *intTestSuite) TestImmutabilityAllInt() {
func(i *sdk.Int) { _ = i.MulRaw(rand.Int63()) },
func(i *sdk.Int) { _ = i.QuoRaw(rand.Int63()) },
func(i *sdk.Int) { _ = i.Neg() },
func(i *sdk.Int) { _ = i.Abs() },
func(i *sdk.Int) { _ = i.IsZero() },
func(i *sdk.Int) { _ = i.Sign() },
func(i *sdk.Int) { _ = i.Equal(randint()) },