aboutsummaryrefslogtreecommitdiffstats
path: root/decimal.go
diff options
context:
space:
mode:
authoryenlin.lai <yenlin.lai@cobinhood.com>2019-04-17 17:32:26 +0800
committeryenlinlai <38415072+yenlinlai@users.noreply.github.com>2019-04-19 11:10:20 +0800
commitb4f0dce08b6acda30fc7dfbe7a789628d42d8b2e (patch)
tree2fae4621d4accb924d2af8ac29e4dd7482722e60 /decimal.go
parent05ad7315b3c581519964b7762074e00de3c4ceac (diff)
downloaddexon-decimal-master.tar.gz
dexon-decimal-master.tar.zst
dexon-decimal-master.zip
tune rescale performanceHEADmaster
Rescale is used in most operations and some of them often have the scale unchanged. e.g. In Add or Sub, one out of two rescale will always do no change. Save one big.Int.Exp in this case.
Diffstat (limited to 'decimal.go')
-rw-r--r--decimal.go7
1 files changed, 4 insertions, 3 deletions
diff --git a/decimal.go b/decimal.go
index 0f5079c..ca277cf 100644
--- a/decimal.go
+++ b/decimal.go
@@ -386,14 +386,15 @@ func (d Decimal) Rescale(exp int32) Decimal {
//
func (d Decimal) rescale(exp int32) Decimal {
d.ensureInitialized()
- // NOTE(vadim): must convert exps to float64 before - to prevent overflow
- diff := math.Abs(float64(exp) - float64(d.exp))
+ // NOTE: convert exps to int64 before - to prevent overflow
+ diff := int64(exp) - int64(d.exp)
value := new(big.Int).Set(d.value)
- expScale := new(big.Int).Exp(tenInt, big.NewInt(int64(diff)), nil)
if exp > d.exp {
+ expScale := new(big.Int).Exp(tenInt, big.NewInt(diff), nil)
value = value.Quo(value, expScale)
} else if exp < d.exp {
+ expScale := new(big.Int).Exp(tenInt, big.NewInt(-diff), nil)
value = value.Mul(value, expScale)
}