mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 04:20:50 +01:00
Compare commits
4 commits
870c906735
...
fab37b20c6
Author | SHA1 | Date | |
---|---|---|---|
|
fab37b20c6 | ||
|
d00399e161 | ||
|
aa070c4ec8 | ||
|
a68916bc93 |
2 changed files with 156 additions and 0 deletions
77
decimal.go
77
decimal.go
|
@ -19,6 +19,7 @@ package decimal
|
|||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
|
@ -124,6 +125,26 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
|||
}
|
||||
}
|
||||
|
||||
// NewFromBigRat returns a new Decimal from a big.Rat. The numerator and
|
||||
// denominator are divided and rounded to the given precision.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// d1 := NewFromBigRat(big.NewRat(0, 1), 0) // output: "0"
|
||||
// d2 := NewFromBigRat(big.NewRat(4, 5), 1) // output: "0.8"
|
||||
// d3 := NewFromBigRat(big.NewRat(1000, 3), 3) // output: "333.333"
|
||||
// d4 := NewFromBigRat(big.NewRat(2, 7), 4) // output: "0.2857"
|
||||
//
|
||||
func NewFromBigRat(value *big.Rat, precision int32) Decimal {
|
||||
return Decimal{
|
||||
value: new(big.Int).Set(value.Num()),
|
||||
exp: 0,
|
||||
}.DivRound(Decimal{
|
||||
value: new(big.Int).Set(value.Denom()),
|
||||
exp: 0,
|
||||
}, precision)
|
||||
}
|
||||
|
||||
// NewFromString returns a new Decimal from a string representation.
|
||||
// Trailing zeroes are not trimmed.
|
||||
//
|
||||
|
@ -2006,3 +2027,59 @@ func (d Decimal) Tan() Decimal {
|
|||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// More math
|
||||
|
||||
// Sqrt returns the square root of d, accurate to DivisionPrecision decimal places.
|
||||
// Sqrt is only valid for non-negative numbers; it will return an error otherwise.
|
||||
func (d Decimal) Sqrt() (Decimal, error) {
|
||||
s, _, err := d.SqrtRound(int32(DivisionPrecision))
|
||||
return s, err
|
||||
}
|
||||
|
||||
// ErrImaginaryResult indicates an operation that would produce an imaginary result.
|
||||
var ErrImaginaryResult = errors.New("The result of this operation is imaginary.")
|
||||
|
||||
// SqrtMaxIter sets a limit for number of iterations for the Sqrt function
|
||||
const SqrtMaxIter = 1000000
|
||||
|
||||
// SqrtRound returns the square root of d, accurate to precision decimal places.
|
||||
// The bool precise returns whether the precision was achieved.
|
||||
// SqrtRound is only valid for non-negative numbers; it will return an error otherwise.
|
||||
func (d Decimal) SqrtRound(precision int32) (Decimal, bool, error) {
|
||||
var (
|
||||
maxError = New(1, -precision)
|
||||
one = NewFromFloat(1)
|
||||
lo, hi Decimal
|
||||
)
|
||||
|
||||
// Handle cases where d < 0, d = 0, 0 < d < 1, and d > 1
|
||||
if d.GreaterThanOrEqual(one) {
|
||||
lo = Zero
|
||||
hi = d
|
||||
} else if d.Equal(one) {
|
||||
return one, true, nil
|
||||
} else if d.LessThan(Zero) {
|
||||
return Zero, false, ErrImaginaryResult
|
||||
} else if d.Equal(Zero) {
|
||||
return Zero, true, nil
|
||||
} else {
|
||||
// d is between 0 and 1. Therefore, 0 < d < Sqrt(d) < 1.
|
||||
lo = d
|
||||
hi = one
|
||||
}
|
||||
|
||||
var mid Decimal
|
||||
for i := 0; i < SqrtMaxIter; i++ {
|
||||
mid = lo.Add(hi).Div(New(2, 0)) //mid = (lo+hi)/2;
|
||||
if mid.Mul(mid).Sub(d).Abs().LessThan(maxError) {
|
||||
return mid, true, nil
|
||||
}
|
||||
if mid.Mul(mid).GreaterThan(d) {
|
||||
hi = mid
|
||||
} else {
|
||||
lo = mid
|
||||
}
|
||||
}
|
||||
return mid, false, nil
|
||||
}
|
||||
|
|
|
@ -556,6 +556,51 @@ func TestNewFromBigIntWithExponent(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNewFromBigRat(t *testing.T) {
|
||||
mustParseRat := func(val string) *big.Rat {
|
||||
num, _ := new(big.Rat).SetString(val)
|
||||
return num
|
||||
}
|
||||
|
||||
type Inp struct {
|
||||
val *big.Rat
|
||||
prec int32
|
||||
}
|
||||
|
||||
tests := map[Inp]string{
|
||||
Inp{big.NewRat(0, 1), 16}: "0",
|
||||
Inp{big.NewRat(4, 5), 16}: "0.8",
|
||||
Inp{big.NewRat(10, 2), 16}: "5",
|
||||
Inp{big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828", // rounded
|
||||
Inp{big.NewRat(1, 434324545566634), 16}: "0.0000000000000023",
|
||||
Inp{big.NewRat(1, 3), 16}: "0.3333333333333333",
|
||||
Inp{big.NewRat(2, 3), 2}: "0.67", // rounded
|
||||
Inp{big.NewRat(2, 3), 16}: "0.6666666666666667", // rounded
|
||||
Inp{big.NewRat(10000, 3), 16}: "3333.3333333333333333",
|
||||
Inp{mustParseRat("30702832066636633479"), 16}: "30702832066636633479",
|
||||
Inp{mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753",
|
||||
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500", // rounded
|
||||
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753", // rounded
|
||||
Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832",
|
||||
}
|
||||
|
||||
// add negatives
|
||||
for p, s := range tests {
|
||||
if p.val.Cmp(new(big.Rat)) > 0 {
|
||||
tests[Inp{p.val.Neg(p.val), p.prec}] = "-" + s
|
||||
}
|
||||
}
|
||||
|
||||
for input, s := range tests {
|
||||
d := NewFromBigRat(input.val, input.prec)
|
||||
if d.String() != s {
|
||||
t.Errorf("expected %s, got %s (%s, %d)",
|
||||
s, d.String(),
|
||||
d.value.String(), d.exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
origin := New(1, 0)
|
||||
cpy := origin.Copy()
|
||||
|
@ -3194,6 +3239,40 @@ func TestAvg(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSqrtRound(t *testing.T) {
|
||||
i := NewFromFloat(-1)
|
||||
if _, err := i.Sqrt(); err != ErrImaginaryResult {
|
||||
t.Errorf("Square root of -1 should produce error")
|
||||
}
|
||||
|
||||
var vals = map[string]string{
|
||||
// value : Sqrt(value)
|
||||
"0.0": "0.0",
|
||||
"0.002342": "0.0483942145302514",
|
||||
"1.0": "1.0",
|
||||
"3.0": "1.7320508075688773",
|
||||
"4.0": "2.0",
|
||||
"4.5": "2.1213203435596426",
|
||||
"3289854.0": "1813.7954680724064485",
|
||||
}
|
||||
|
||||
for val, expected := range vals {
|
||||
v, err := NewFromString(val)
|
||||
if err != nil {
|
||||
t.Errorf("error parsing test value into Decimal")
|
||||
}
|
||||
|
||||
e, err := NewFromString(expected)
|
||||
if err != nil {
|
||||
t.Errorf("error parsing test expected value into Decimal")
|
||||
}
|
||||
|
||||
if sqrt, err := v.Sqrt(); err != nil || !sqrt.Equal(e) {
|
||||
t.Errorf("Square root of %s should be %s, not %s (error: %s)", v, e, sqrt, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundBankAnomaly(t *testing.T) {
|
||||
a := New(25, -1)
|
||||
b := New(250, -2)
|
||||
|
|
Loading…
Reference in a new issue