Skip to content

Commit

Permalink
decimal: remove error from Pad method
Browse files Browse the repository at this point in the history
  • Loading branch information
eapenkin committed May 19, 2024
1 parent 5dfc0e1 commit 3e17116
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [0.1.26] - 2024-05-19
## [0.1.27] - 2024-05-19

### Changed

Expand Down
4 changes: 2 additions & 2 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func newFromBint(neg bool, coef *bint, scale, minScale int) (Decimal, error) {
scale = MaxScale
case prec > scale && prec > MaxPrec: // there is an integer part
coef.rshHalfEven(coef, prec-MaxPrec)
scale = scale - (prec - MaxPrec)
scale = MaxPrec - prec + scale
}
// Handling the rare case when rshHalfEven rounded
// a 19-digit coefficient to a 20-digit coefficient.
Expand Down Expand Up @@ -1018,7 +1018,7 @@ func (d Decimal) Round(scale int) Decimal {
// The total number of digits in the result is limited by [MaxPrec].
// See also method [Decimal.Trim].
func (d Decimal) Pad(scale int) Decimal {
scale = min(scale, MaxPrec-d.Prec()+d.Scale())
scale = min(scale, MaxScale, MaxPrec-d.Prec()+d.Scale())
if scale <= d.Scale() {
return d
}
Expand Down
13 changes: 9 additions & 4 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,12 +1241,12 @@ func TestDecimal_Pad(t *testing.T) {
{"0", 1, "0.0"},
{"0", 2, "0.00"},
{"0", 19, "0.0000000000000000000"},
{"0.0", 1, "0.0"},
{"0.00", 2, "0.00"},
{"0.000000000", 19, "0.0000000000000000000"},
{"0", 20, "0.0000000000000000000"},
{"0.000000000", 0, "0.000000000"},
{"0.000000000", 1, "0.000000000"},
{"0.000000000", 2, "0.000000000"},
{"0.000000000", 19, "0.0000000000000000000"},
{"0.000000000", 20, "0.0000000000000000000"},

// Tests from GDA
{"2.17", 0, "2.17"},
Expand All @@ -1272,7 +1272,7 @@ func TestDecimal_Pad(t *testing.T) {
{"100000000000000", 5, "100000000000000.0000"},
{"10000000000000", 6, "10000000000000.00000"},
{"1000000000000", 7, "1000000000000.000000"},
{"0", 20, "0.0000000000000000000"},
{"-0.0000000000032", 63, "-0.0000000000032000000"},
}
for _, tt := range tests {
d := MustParse(tt.d)
Expand Down Expand Up @@ -3526,6 +3526,11 @@ func FuzzDecimal_Pad(f *testing.F) {
got := want.Pad(scale)
if got.Cmp(want) != 0 {
t.Errorf("%q.Pad(%v) = %q", want, scale, got)
return
}
if got.Scale() > MaxScale {
t.Errorf("%q.Pad(%v).Scale() = %v", want, scale, got.Scale())
return
}
},
)
Expand Down

0 comments on commit 3e17116

Please sign in to comment.