mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Compare commits
7 commits
066a525084
...
bb599667c0
Author | SHA1 | Date | |
---|---|---|---|
|
bb599667c0 | ||
|
80ec940054 | ||
|
2fa107d0fe | ||
|
645a76e5b0 | ||
|
ed8f76e893 | ||
|
aa070c4ec8 | ||
|
a68916bc93 |
5 changed files with 136 additions and 19 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
@ -9,7 +9,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go: [ '1.7.x', '1.18', '1.19', '1.20', '1.21', '1.x' ]
|
go: [ '1.10.x', '1.19', '1.20', '1.21', '1.22', '1.x' ]
|
||||||
name: Go ${{ matrix.go }}
|
name: Go ${{ matrix.go }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
|
@ -22,7 +22,7 @@ Run `go get github.com/shopspring/decimal`
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
Decimal library requires Go version `>=1.7`
|
Decimal library requires Go version `>=1.10`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -63,11 +63,6 @@ func main() {
|
||||||
|
|
||||||
http://godoc.org/github.com/shopspring/decimal
|
http://godoc.org/github.com/shopspring/decimal
|
||||||
|
|
||||||
## Production Usage
|
|
||||||
|
|
||||||
* [Spring](https://shopspring.com/), since August 14, 2014.
|
|
||||||
* If you are using this in production, please let us know!
|
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
#### Why don't you just use float64?
|
#### Why don't you just use float64?
|
||||||
|
|
73
decimal.go
73
decimal.go
|
@ -19,6 +19,7 @@ package decimal
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
@ -130,6 +131,18 @@ func NewFromInt32(value int32) Decimal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromUint64 converts an uint64 to Decimal.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// NewFromUint64(123).String() // output: "123"
|
||||||
|
func NewFromUint64(value uint64) Decimal {
|
||||||
|
return Decimal{
|
||||||
|
value: new(big.Int).SetUint64(value),
|
||||||
|
exp: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
||||||
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
||||||
return Decimal{
|
return Decimal{
|
||||||
|
@ -1406,9 +1419,7 @@ func (d Decimal) IntPart() int64 {
|
||||||
// BigInt returns integer component of the decimal as a BigInt.
|
// BigInt returns integer component of the decimal as a BigInt.
|
||||||
func (d Decimal) BigInt() *big.Int {
|
func (d Decimal) BigInt() *big.Int {
|
||||||
scaledD := d.rescale(0)
|
scaledD := d.rescale(0)
|
||||||
i := &big.Int{}
|
return scaledD.value
|
||||||
i.SetString(scaledD.String(), 10)
|
|
||||||
return i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BigFloat returns decimal as BigFloat.
|
// BigFloat returns decimal as BigFloat.
|
||||||
|
@ -2322,3 +2333,59 @@ func (d Decimal) Tan() Decimal {
|
||||||
}
|
}
|
||||||
return y
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -480,6 +480,7 @@ func TestNewFromInt(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
9223372036854775807: "9223372036854775807",
|
9223372036854775807: "9223372036854775807",
|
||||||
|
-9223372036854775808: "-9223372036854775808",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -505,6 +506,7 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
2147483647: "2147483647",
|
2147483647: "2147483647",
|
||||||
|
-2147483648: "-2147483648",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -524,6 +526,25 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewFromUint64(t *testing.T) {
|
||||||
|
tests := map[uint64]string{
|
||||||
|
0: "0",
|
||||||
|
1: "1",
|
||||||
|
323412345: "323412345",
|
||||||
|
9223372036854775807: "9223372036854775807",
|
||||||
|
18446744073709551615: "18446744073709551615",
|
||||||
|
}
|
||||||
|
|
||||||
|
for input, s := range tests {
|
||||||
|
d := NewFromUint64(input)
|
||||||
|
if d.String() != s {
|
||||||
|
t.Errorf("expected %s, got %s (%s, %d)",
|
||||||
|
s, d.String(),
|
||||||
|
d.value.String(), d.exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewFromBigIntWithExponent(t *testing.T) {
|
func TestNewFromBigIntWithExponent(t *testing.T) {
|
||||||
type Inp struct {
|
type Inp struct {
|
||||||
val *big.Int
|
val *big.Int
|
||||||
|
@ -3460,6 +3481,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) {
|
func TestRoundBankAnomaly(t *testing.T) {
|
||||||
a := New(25, -1)
|
a := New(25, -1)
|
||||||
b := New(250, -2)
|
b := New(250, -2)
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
||||||
module github.com/shopspring/decimal
|
module github.com/shopspring/decimal
|
||||||
|
|
||||||
go 1.7
|
go 1.10
|
||||||
|
|
Loading…
Reference in a new issue