From 9f30a90980799c5f363a5f50cffa3bcea81b6421 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Mon, 25 Jun 2018 20:03:55 -0700 Subject: [PATCH] Merge PR #1374: types/rat: Fix overflowing in printing This now uses the underlying golang big.rat's string function, instead of casting to num and den which are int64s. Closes #1258 --- CHANGELOG.md | 1 + types/rational.go | 2 +- types/rational_test.go | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f46d00949..da8bfb7f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ FIXES * Fixed bug where chain ID wasn't passed properly in x/bank REST handler * Fixed bug where `democli account` didn't decode the account data correctly * \#1343 - fixed unnecessary parallelism in CI +* \#1258 - printing big.rat's can no longer overflow int64 ## 0.19.0 diff --git a/types/rational.go b/types/rational.go index 6e542e110..dc6569be3 100644 --- a/types/rational.go +++ b/types/rational.go @@ -115,7 +115,7 @@ func (r Rat) Mul(r2 Rat) Rat { return Rat{*new(big.Rat).Mul(&(r.Rat), &(r2.Ra func (r Rat) Quo(r2 Rat) Rat { return Rat{*new(big.Rat).Quo(&(r.Rat), &(r2.Rat))} } // Quo - quotient func (r Rat) Add(r2 Rat) Rat { return Rat{*new(big.Rat).Add(&(r.Rat), &(r2.Rat))} } // Add - addition func (r Rat) Sub(r2 Rat) Rat { return Rat{*new(big.Rat).Sub(&(r.Rat), &(r2.Rat))} } // Sub - subtraction -func (r Rat) String() string { return fmt.Sprintf("%v/%v", r.Num(), r.Denom()) } +func (r Rat) String() string { return r.Rat.String() } var ( zero = big.NewInt(0) diff --git a/types/rational_test.go b/types/rational_test.go index 30abb1a51..2483704ed 100644 --- a/types/rational_test.go +++ b/types/rational_test.go @@ -100,7 +100,7 @@ func TestEqualities(t *testing.T) { } -func TestArithmatic(t *testing.T) { +func TestArithmetic(t *testing.T) { tests := []struct { r1, r2 Rat resMul, resDiv, resAdd, resSub Rat @@ -297,3 +297,14 @@ func TestRatsEqual(t *testing.T) { } } + +func TestStringOverflow(t *testing.T) { + // two random 64 bit primes + rat1 := NewRat(5164315003622678713, 4389711697696177267) + rat2 := NewRat(-3179849666053572961, 8459429845579852627) + rat3 := rat1.Add(rat2) + assert.Equal(t, + "29728537197630860939575850336935951464/37134458148982045574552091851127630409", + rat3.String(), + ) +}