-
Notifications
You must be signed in to change notification settings - Fork 44
/
exponent.go
38 lines (33 loc) · 975 Bytes
/
exponent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only
package coins
import (
"github.com/cockroachdb/apd/v3"
)
//
// Notes on the Exponent field of apd.Decimal:
// * All external apd.Decimal input values come from JSON.
// * apd.Decimal's unmarshalling code will throw an error if the exponent
// of a value exceeds apd.MaxExponent (100,000).
// * Since our software will never have an apd.Decimal value with an
// exponent that can be overflowed/underflowed, panic error handling
// is fine.
// * Overflow checking is done and centralized here so code auditors do
// do not waste time.
//
func increaseExponent(n *apd.Decimal, delta uint8) {
delta32 := int32(delta)
e := n.Exponent
n.Exponent += delta32
if n.Exponent < e {
panic("overflow")
}
}
func decreaseExponent(n *apd.Decimal, delta uint8) {
delta32 := int32(delta)
e := n.Exponent
n.Exponent -= delta32
if n.Exponent > e {
panic("underflow")
}
}