-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd08cfd
commit 197fd5b
Showing
3 changed files
with
70 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package numbers | ||
|
||
import "github.com/cockroachdb/apd/v3" | ||
|
||
// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. | ||
func MustParseDecimal(value string) *apd.Decimal { | ||
decimal, _, err := apd.NewFromString(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return decimal | ||
} | ||
|
||
// DecimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. | ||
// If the new exponent is less precise then the extra digits will be truncated. | ||
func DecimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { | ||
exponentDelta := newExponent - decimal.Exponent // Exponent is negative. | ||
|
||
if exponentDelta == 0 { | ||
return new(apd.Decimal).Set(decimal) | ||
} | ||
|
||
coefficient := new(apd.BigInt).Set(&decimal.Coeff) | ||
|
||
if exponentDelta < 0 { | ||
multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) | ||
coefficient.Mul(coefficient, multiplier) | ||
} else if exponentDelta > 0 { | ||
divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) | ||
coefficient.Div(coefficient, divisor) | ||
} | ||
|
||
return &apd.Decimal{ | ||
Form: decimal.Form, | ||
Negative: decimal.Negative, | ||
Exponent: newExponent, | ||
Coeff: *coefficient, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package numbers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/apd/v3" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDecimalWithNewExponent(t *testing.T) { | ||
assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 0), 0).Text('f')) | ||
assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 1), 1).Text('f')) | ||
assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 100), 0).Text('f')) | ||
assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 0), 1).Text('f')) | ||
assert.Equal(t, "0.0", DecimalWithNewExponent(apd.New(0, 0), -1).Text('f')) | ||
|
||
// Same exponent: | ||
assert.Equal(t, "12.349", DecimalWithNewExponent(MustParseDecimal("12.349"), -3).Text('f')) | ||
// More precise exponent: | ||
assert.Equal(t, "12.3490", DecimalWithNewExponent(MustParseDecimal("12.349"), -4).Text('f')) | ||
assert.Equal(t, "12.34900", DecimalWithNewExponent(MustParseDecimal("12.349"), -5).Text('f')) | ||
// Lest precise exponent: | ||
// Extra digits should be truncated rather than rounded. | ||
assert.Equal(t, "12.34", DecimalWithNewExponent(MustParseDecimal("12.349"), -2).Text('f')) | ||
assert.Equal(t, "12.3", DecimalWithNewExponent(MustParseDecimal("12.349"), -1).Text('f')) | ||
assert.Equal(t, "12", DecimalWithNewExponent(MustParseDecimal("12.349"), 0).Text('f')) | ||
assert.Equal(t, "10", DecimalWithNewExponent(MustParseDecimal("12.349"), 1).Text('f')) | ||
} |