Compare commits

...

6 commits

Author SHA1 Message Date
Julius F
73c9b22fc7
Merge 49883997d8 into 645a76e5b0 2024-04-07 20:12:38 +02:00
Philip Dubé
645a76e5b0
Optimize BigInt (#359) 2024-04-05 21:43:23 +02:00
Mateusz Woś
ed8f76e893
Drop support for Go versions older than 1.10 (#361)
* Drop support for Go versions older than 1.10
* Remove 1.18 from test suite
2024-04-05 21:41:05 +02:00
Mateusz Woś
0e69d5cd53
Run CI on both push to origin branches and pull requests (#360) 2024-04-04 16:35:31 +02:00
Philip Dubé
bf7794e1a8
Optimize NumDigits method (#356) 2024-04-04 16:14:38 +02:00
Julius Foitzik
49883997d8 extend readme to highlight special cases around parsing and printing when using trailing zeros 2022-03-11 11:51:11 +01:00
5 changed files with 94 additions and 17 deletions

View file

@ -1,11 +1,15 @@
name: ci
on: [push]
on:
push:
branches:
- master
pull_request:
jobs:
ci-job:
runs-on: ubuntu-latest
strategy:
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 }}
steps:
- uses: actions/checkout@v4

View file

@ -8,6 +8,8 @@ Arbitrary-precision fixed-point decimal numbers in go.
_Note:_ Decimal library can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
_Note:_ The library does not remove trailing zeros when parsing decimals from strings, e.g., `1.18` does not internally equal `1.1800` when parsing with this library, see example in usage
## Features
* The zero-value is 0, and is safe to use without initialization
@ -22,7 +24,7 @@ Run `go get github.com/shopspring/decimal`
## Requirements
Decimal library requires Go version `>=1.7`
Decimal library requires Go version `>=1.10`
## Usage
@ -59,6 +61,32 @@ func main() {
}
```
Conservation of precision with trailing zeros. This conservation is, however,
not translated backwards when printing the decimal as string.
This is important to know.
```go
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
noZeros := "1.18"
trailingZeros := "1.1800"
d1 := decimal.RequireFromString(noZeros)
d2 := decimal.RequireFromString(trailingZeros)
fmt.Println(d1.Coefficient(), d1.Exponent()) // 118 -2
fmt.Println(d2.Coefficient(), d2.Exponent()) // 118 -4
fmt.Println(d1) // 1.18
fmt.Println(d2) // 1.18
```
## Documentation
http://godoc.org/github.com/shopspring/decimal

View file

@ -1224,14 +1224,33 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
}
// NumDigits returns the number of digits of the decimal coefficient (d.Value)
// Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part
func (d Decimal) NumDigits() int {
d.ensureInitialized()
// Note(mwoss): It can be optimized, unnecessary cast of big.Int to string
if d.IsNegative() {
return len(d.value.String()) - 1
if d.value == nil {
return 1
}
return len(d.value.String())
if d.value.IsInt64() {
i64 := d.value.Int64()
// restrict fast path to integers with exact conversion to float64
if i64 <= (1<<53) && i64 >= -(1<<53) {
if i64 == 0 {
return 1
}
return int(math.Log10(math.Abs(float64(i64)))) + 1
}
}
estimatedNumDigits := int(float64(d.value.BitLen()) / math.Log2(10))
// estimatedNumDigits (lg10) may be off by 1, need to verify
digitsBigInt := big.NewInt(int64(estimatedNumDigits))
errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil)
if d.value.CmpAbs(errorCorrectionUnit) >= 0 {
return estimatedNumDigits + 1
}
return estimatedNumDigits
}
// IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.
@ -1387,9 +1406,7 @@ func (d Decimal) IntPart() int64 {
// BigInt returns integer component of the decimal as a BigInt.
func (d Decimal) BigInt() *big.Int {
scaledD := d.rescale(0)
i := &big.Int{}
i.SetString(scaledD.String(), 10)
return i
return scaledD.value
}
// BigFloat returns decimal as BigFloat.

View file

@ -121,6 +121,34 @@ func BenchmarkDecimal_RoundCash_Five(b *testing.B) {
}
}
func numDigits(b *testing.B, want int, val Decimal) {
b.Helper()
for i := 0; i < b.N; i++ {
if have := val.NumDigits(); have != want {
b.Fatalf("\nHave: %d\nWant: %d", have, want)
}
}
}
func BenchmarkDecimal_NumDigits10(b *testing.B) {
numDigits(b, 10, New(3478512345, -3))
}
func BenchmarkDecimal_NumDigits100(b *testing.B) {
s := make([]byte, 102)
for i := range s {
s[i] = byte('0' + i%10)
}
s[0] = '-'
s[100] = '.'
d, err := NewFromString(string(s))
if err != nil {
b.Log(d)
b.Error(err)
}
numDigits(b, 100, d)
}
func Benchmark_Cmp(b *testing.B) {
decimals := DecimalSlice([]Decimal{})
for i := 0; i < 1000000; i++ {
@ -132,7 +160,7 @@ func Benchmark_Cmp(b *testing.B) {
}
}
func Benchmark_decimal_Decimal_Add_different_precision(b *testing.B) {
func BenchmarkDecimal_Add_different_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
@ -143,7 +171,7 @@ func Benchmark_decimal_Decimal_Add_different_precision(b *testing.B) {
}
}
func Benchmark_decimal_Decimal_Sub_different_precision(b *testing.B) {
func BenchmarkDecimal_Sub_different_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500).Mul(NewFromFloat(0.12))
@ -154,7 +182,7 @@ func Benchmark_decimal_Decimal_Sub_different_precision(b *testing.B) {
}
}
func Benchmark_decimal_Decimal_Add_same_precision(b *testing.B) {
func BenchmarkDecimal_Add_same_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500.123)
@ -165,7 +193,7 @@ func Benchmark_decimal_Decimal_Add_same_precision(b *testing.B) {
}
}
func Benchmark_decimal_Decimal_Sub_same_precision(b *testing.B) {
func BenchmarkDecimal_Sub_same_precision(b *testing.B) {
d1 := NewFromFloat(1000.123)
d2 := NewFromFloat(500.123)

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/shopspring/decimal
go 1.7
go 1.10