diff options
author | yenlin.lai <yenlin.lai@cobinhood.com> | 2019-04-17 17:32:26 +0800 |
---|---|---|
committer | yenlinlai <38415072+yenlinlai@users.noreply.github.com> | 2019-04-19 11:10:20 +0800 |
commit | b4f0dce08b6acda30fc7dfbe7a789628d42d8b2e (patch) | |
tree | 2fae4621d4accb924d2af8ac29e4dd7482722e60 | |
parent | 05ad7315b3c581519964b7762074e00de3c4ceac (diff) | |
download | dexon-decimal-master.tar.gz dexon-decimal-master.tar.zst dexon-decimal-master.zip |
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.
-rw-r--r-- | decimal.go | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -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) } |