Skip to content

Commit

Permalink
constant for maxDecBitLen
Browse files Browse the repository at this point in the history
  • Loading branch information
mankenavenkatesh committed Jun 16, 2021
1 parent c3492be commit 161c111
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
// Ceiling[Log2[999 999 999 999 999 999]]
DecimalPrecisionBits = 60

maxDecBitLen = maxBitLen + DecimalPrecisionBits

// max number of iterations in ApproxRoot function
maxApproxRootIterations = 100
)
Expand Down Expand Up @@ -222,7 +224,7 @@ func (d Dec) BigInt() *big.Int {
func (d Dec) Add(d2 Dec) Dec {
res := new(big.Int).Add(d.i, d2.i)

if res.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if res.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{res}
Expand All @@ -232,7 +234,7 @@ func (d Dec) Add(d2 Dec) Dec {
func (d Dec) Sub(d2 Dec) Dec {
res := new(big.Int).Sub(d.i, d2.i)

if res.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if res.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{res}
Expand All @@ -243,7 +245,7 @@ func (d Dec) Mul(d2 Dec) Dec {
mul := new(big.Int).Mul(d.i, d2.i)
chopped := chopPrecisionAndRound(mul)

if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{chopped}
Expand All @@ -254,7 +256,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec {
mul := new(big.Int).Mul(d.i, d2.i)
chopped := chopPrecisionAndTruncate(mul)

if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{chopped}
Expand All @@ -264,7 +266,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec {
func (d Dec) MulInt(i Int) Dec {
mul := new(big.Int).Mul(d.i, i.i)

if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if mul.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{mul}
Expand All @@ -274,7 +276,7 @@ func (d Dec) MulInt(i Int) Dec {
func (d Dec) MulInt64(i int64) Dec {
mul := new(big.Int).Mul(d.i, big.NewInt(i))

if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if mul.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{mul}
Expand All @@ -289,7 +291,7 @@ func (d Dec) Quo(d2 Dec) Dec {
quo := new(big.Int).Quo(mul, d2.i)
chopped := chopPrecisionAndRound(quo)

if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{chopped}
Expand All @@ -304,7 +306,7 @@ func (d Dec) QuoTruncate(d2 Dec) Dec {
quo := mul.Quo(mul, d2.i)
chopped := chopPrecisionAndTruncate(quo)

if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{chopped}
Expand All @@ -319,7 +321,7 @@ func (d Dec) QuoRoundUp(d2 Dec) Dec {
quo := new(big.Int).Quo(mul, d2.i)
chopped := chopPrecisionAndRoundUp(quo)

if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) {
if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return Dec{chopped}
Expand Down

0 comments on commit 161c111

Please sign in to comment.