Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bigdec speedups #7538

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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