Skip to content

Commit

Permalink
Add rationale for DefaultDecimalPrec
Browse files Browse the repository at this point in the history
Also fix SetInt test to cope with the new value.
  • Loading branch information
db47h committed May 7, 2020
1 parent 9cf3940 commit 0e73258
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"math/big"
)

// DefaultDecimalPrec is the default minimum precision used when creating a new
// Decimal from any other type. An uint64 requires up to 20 digits, which
// amounts to 2 x 19-digits Words (64 bits) or 3 x 9-digits Words (32 bits).
// Forcing the precision to 20 digits would result in 18 or 7 unused digits.
// Using 34 instead gives a higher precision at no performance or memory cost
// and gives room for 2 to 4 extra digits of extra precision for internal
// computations at no performance or memory cost either. Also 34 digits matches
// the precision of IEEE-754 decimal128.
const DefaultDecimalPrec = 34

type Decimal struct {
Expand Down
8 changes: 6 additions & 2 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ func TestDecimal_SetInt(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, _ := new(big.Int).SetString(td.s, td.b)
d := new(Decimal).SetMode(ToNearestEven).SetPrec(td.p).SetInt(b)
ep := td.pr
if td.p == 0 && ep < DefaultDecimalPrec {
ep = DefaultDecimalPrec
}
if !reflect.DeepEqual(td.d, d.mant) {
t.Fatalf("\nexpected mantissa %v\n got %v", td.d, d.mant)
}
if td.pr != d.Prec() {
t.Fatalf("\nexpected precision %v\n got %v", td.pr, d.Prec())
if ep != d.Prec() {
t.Fatalf("\nexpected precision %v\n got %v", ep, d.Prec())
}
if td.e != d.exp {
t.Fatalf("\nexpected exponent %v\n got %v", td.p, d.Prec())
Expand Down

0 comments on commit 0e73258

Please sign in to comment.