Skip to content

Commit 891b37d

Browse files
committed
Merge remote-tracking branch 'true/master'
2 parents 4c76af7 + a1bdfc3 commit 891b37d

File tree

9 files changed

+1222
-280
lines changed

9 files changed

+1222
-280
lines changed

.github/workflows/ci.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
name: decimal-ci
2-
on: [push]
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
37
jobs:
48
ci-job:
59
runs-on: ubuntu-latest
610
strategy:
711
matrix:
8-
go: [ '1.7.x', '1.15', '1.16', '1.17', '1.x' ]
12+
go: [ '1.10.x', '1.19', '1.20', '1.21', '1.22', '1.x' ]
913
name: Go ${{ matrix.go }}
1014
steps:
11-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v4
1216
- name: Use go
13-
uses: actions/setup-go@v2
17+
uses: actions/setup-go@v5
1418
with:
1519
go-version: ${{ matrix.go }}
1620
- run: go build .

CHANGELOG.md

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
## Decimal v1.4.0
2+
#### BREAKING
3+
- Drop support for Go version older than 1.10 [#361](https://github.com/shopspring/decimal/pull/361)
4+
5+
#### FEATURES
6+
- Add implementation of natural logarithm [#339](https://github.com/shopspring/decimal/pull/339) [#357](https://github.com/shopspring/decimal/pull/357)
7+
- Add improved implementation of power operation [#358](https://github.com/shopspring/decimal/pull/358)
8+
- Add Compare method which forwards calls to Cmp [#346](https://github.com/shopspring/decimal/pull/346)
9+
- Add NewFromBigRat constructor [#288](https://github.com/shopspring/decimal/pull/288)
10+
- Add NewFromUint64 constructor [#352](https://github.com/shopspring/decimal/pull/352)
11+
12+
#### ENHANCEMENTS
13+
- Migrate to Github Actions [#245](https://github.com/shopspring/decimal/pull/245) [#340](https://github.com/shopspring/decimal/pull/340)
14+
- Fix examples for RoundDown, RoundFloor, RoundUp, and RoundCeil [#285](https://github.com/shopspring/decimal/pull/285) [#328](https://github.com/shopspring/decimal/pull/328) [#341](https://github.com/shopspring/decimal/pull/341)
15+
- Use Godoc standard to mark deprecated Equals and StringScaled methods [#342](https://github.com/shopspring/decimal/pull/342)
16+
- Removed unnecessary min function for RescalePair method [#265](https://github.com/shopspring/decimal/pull/265)
17+
- Avoid reallocation of initial slice in MarshalBinary (GobEncode) [#355](https://github.com/shopspring/decimal/pull/355)
18+
- Optimize NumDigits method [#301](https://github.com/shopspring/decimal/pull/301) [#356](https://github.com/shopspring/decimal/pull/356)
19+
- Optimize BigInt method [#359](https://github.com/shopspring/decimal/pull/359)
20+
- Support scanning uint64 [#131](https://github.com/shopspring/decimal/pull/131) [#364](https://github.com/shopspring/decimal/pull/364)
21+
- Add docs section with alternative libraries [#363](https://github.com/shopspring/decimal/pull/363)
22+
23+
#### BUGFIXES
24+
- Fix incorrect calculation of decimal modulo [#258](https://github.com/shopspring/decimal/pull/258) [#317](https://github.com/shopspring/decimal/pull/317)
25+
- Allocate new(big.Int) in Copy method to deeply clone it [#278](https://github.com/shopspring/decimal/pull/278)
26+
- Fix overflow edge case in QuoRem method [#322](https://github.com/shopspring/decimal/pull/322)
27+
128
## Decimal v1.3.1
229

330
#### ENHANCEMENTS

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# decimal
22

3-
[![Github Actions](https://github.com/shopspring/decimal/actions/workflows/ci.yml/badge.svg)](https://github.com/shopspring/decimal/actions/workflows/ci.yml)
3+
[![ci](https://github.com/shopspring/decimal/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/shopspring/decimal/actions/workflows/ci.yml)
44
[![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal)
66

@@ -22,7 +22,12 @@ Run `go get github.com/shopspring/decimal`
2222

2323
## Requirements
2424

25-
Decimal library requires Go version `>=1.7`
25+
Decimal library requires Go version `>=1.10`
26+
27+
## Documentation
28+
29+
http://godoc.org/github.com/shopspring/decimal
30+
2631

2732
## Usage
2833

@@ -59,14 +64,16 @@ func main() {
5964
}
6065
```
6166

62-
## Documentation
63-
64-
http://godoc.org/github.com/shopspring/decimal
67+
## Alternative libraries
6568

66-
## Production Usage
69+
When working with decimal numbers, you might face problems this library is not perfectly suited for.
70+
Fortunately, thanks to the wonderful community we have a dozen other libraries that you can choose from.
71+
Explore other alternatives to find the one that best fits your needs :)
6772

68-
* [Spring](https://shopspring.com/), since August 14, 2014.
69-
* If you are using this in production, please let us know!
73+
* [cockroachdb/apd](https://github.com/cockroachdb/apd) - arbitrary precision, mutable and rich API similar to `big.Int`, more performant than this library
74+
* [alpacahq/alpacadecimal](https://github.com/alpacahq/alpacadecimal) - high performance, low precision (12 digits), fully compatible API with this library
75+
* [govalues/decimal](https://github.com/govalues/decimal) - high performance, zero-allocation, low precision (19 digits)
76+
* [greatcloak/decimal](https://github.com/greatcloak/decimal) - fork focusing on billing and e-commerce web application related use cases, includes out-of-the-box BSON marshaling support
7077

7178
## FAQ
7279

const.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package decimal
2+
3+
import (
4+
"strings"
5+
)
6+
7+
const (
8+
strLn10 = "2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298341967784042286248633409525465082806756666287369098781689482907208325554680843799894826233198528393505308965377732628846163366222287698219886746543667474404243274365155048934314939391479619404400222105101714174800368808401264708068556774321622835522011480466371565912137345074785694768346361679210180644507064800027750268491674655058685693567342067058113642922455440575892572420824131469568901675894025677631135691929203337658714166023010570308963457207544037084746994016826928280848118428931484852494864487192780967627127577539702766860595249671667418348570442250719796500471495105049221477656763693866297697952211071826454973477266242570942932258279850258550978526538320760672631716430950599508780752371033310119785754733154142180842754386359177811705430982748238504564801909561029929182431823752535770975053956518769751037497088869218020518933950723853920514463419726528728696511086257149219884997874887377134568620916705849807828059751193854445009978131146915934666241071846692310107598438319191292230792503747298650929009880391941702654416816335727555703151596113564846546190897042819763365836983716328982174407366009162177850541779276367731145041782137660111010731042397832521894898817597921798666394319523936855916447118246753245630912528778330963604262982153040874560927760726641354787576616262926568298704957954913954918049209069438580790032763017941503117866862092408537949861264933479354871737451675809537088281067452440105892444976479686075120275724181874989395971643105518848195288330746699317814634930000321200327765654130472621883970596794457943468343218395304414844803701305753674262153675579814770458031413637793236291560128185336498466942261465206459942072917119370602444929358037007718981097362533224548366988505528285966192805098447175198503666680874970496982273220244823343097169111136813588418696549323714996941979687803008850408979618598756579894836445212043698216415292987811742973332588607915912510967187510929248475023930572665446276200923068791518135803477701295593646298412366497023355174586195564772461857717369368404676577047874319780573853271810933883496338813069945569399346101090745616033312247949360455361849123333063704751724871276379140924398331810164737823379692265637682071706935846394531616949411701841938119405416449466111274712819705817783293841742231409930022911502362192186723337268385688273533371925103412930705632544426611429765388301822384091026198582888433587455960453004548370789052578473166283701953392231047527564998119228742789713715713228319641003422124210082180679525276689858180956119208391760721080919923461516952599099473782780648128058792731993893453415320185969711021407542282796298237068941764740642225757212455392526179373652434440560595336591539160312524480149313234572453879524389036839236450507881731359711238145323701508413491122324390927681724749607955799151363982881058285740538000653371655553014196332241918087621018204919492651483892"
9+
)
10+
11+
var (
12+
ln10 = newConstApproximation(strLn10)
13+
)
14+
15+
type constApproximation struct {
16+
exact Decimal
17+
approximations []Decimal
18+
}
19+
20+
func newConstApproximation(value string) constApproximation {
21+
parts := strings.Split(value, ".")
22+
coeff, fractional := parts[0], parts[1]
23+
24+
coeffLen := len(coeff)
25+
maxPrecision := len(fractional)
26+
27+
var approximations []Decimal
28+
for p := 1; p < maxPrecision; p *= 2 {
29+
r := RequireFromString(value[:coeffLen+p])
30+
approximations = append(approximations, r)
31+
}
32+
33+
return constApproximation{
34+
RequireFromString(value),
35+
approximations,
36+
}
37+
}
38+
39+
// Returns the smallest approximation available that's at least as precise
40+
// as the passed precision (places after decimal point), i.e. Floor[ log2(precision) ] + 1
41+
func (c constApproximation) withPrecision(precision int32) Decimal {
42+
i := 0
43+
44+
if precision >= 1 {
45+
i++
46+
}
47+
48+
for precision >= 16 {
49+
precision /= 16
50+
i += 4
51+
}
52+
53+
for precision >= 2 {
54+
precision /= 2
55+
i++
56+
}
57+
58+
if i >= len(c.approximations) {
59+
return c.exact
60+
}
61+
62+
return c.approximations[i]
63+
}

const_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package decimal
2+
3+
import "testing"
4+
5+
func TestConstApproximation(t *testing.T) {
6+
for _, testCase := range []struct {
7+
Const string
8+
Precision int32
9+
ExpectedApproximation string
10+
}{
11+
{"2.3025850929940456840179914546", 0, "2"},
12+
{"2.3025850929940456840179914546", 1, "2.3"},
13+
{"2.3025850929940456840179914546", 3, "2.302"},
14+
{"2.3025850929940456840179914546", 5, "2.302585"},
15+
{"2.3025850929940456840179914546", 10, "2.302585092994045"},
16+
{"2.3025850929940456840179914546", 100, "2.3025850929940456840179914546"},
17+
{"2.3025850929940456840179914546", -1, "2"},
18+
{"2.3025850929940456840179914546", -5, "2"},
19+
{"3.14159265359", 0, "3"},
20+
{"3.14159265359", 1, "3.1"},
21+
{"3.14159265359", 2, "3.141"},
22+
{"3.14159265359", 4, "3.1415926"},
23+
{"3.14159265359", 13, "3.14159265359"},
24+
} {
25+
ca := newConstApproximation(testCase.Const)
26+
expected, _ := NewFromString(testCase.ExpectedApproximation)
27+
28+
approximation := ca.withPrecision(testCase.Precision)
29+
30+
if approximation.Cmp(expected) != 0 {
31+
t.Errorf("expected approximation %s, got %s - for const with %s precision %d", testCase.ExpectedApproximation, approximation.String(), testCase.Const, testCase.Precision)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)