Skip to content

Commit

Permalink
Minor bigdec speedups (#7538)
Browse files Browse the repository at this point in the history
(cherry picked from commit 37e96c8)
  • Loading branch information
ValarDragon authored and mergify[bot] committed Feb 19, 2024
1 parent 0bb4f8b commit 7c25af2
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func NewBigDec(i int64) BigDec {
// create a new BigDec from integer with decimal place at prec
// CONTRACT: prec <= BigDecPrecision
func NewBigDecWithPrec(i, prec int64) BigDec {
bi := big.NewInt(i)
return BigDec{
new(big.Int).Mul(big.NewInt(i), precisionMultiplier(prec)),
bi.Mul(bi, precisionMultiplier(prec)),
}
}

Expand Down Expand Up @@ -841,11 +842,9 @@ func (d BigDec) Ceil() BigDec {
quo, rem = quo.QuoRem(tmp, precisionReuse, rem)

// no need to round with a zero remainder regardless of sign
if rem.Cmp(zeroInt) == 0 {
if rem.Sign() == 0 {
return NewBigDecFromBigInt(quo)
}

if rem.Sign() == -1 {
} else if rem.Sign() == -1 {
return NewBigDecFromBigInt(quo)
}

Expand Down Expand Up @@ -944,7 +943,7 @@ func (d *BigDec) MarshalTo(data []byte) (n int, err error) {
d.i = new(big.Int)
}

if d.i.Cmp(zeroInt) == 0 {
if d.i.Sign() == 0 {
copy(data, []byte{0x30})
return 1, nil
}
Expand Down

0 comments on commit 7c25af2

Please sign in to comment.