|
8 | 8 | [![versionb]][version]
|
9 | 9 | [![awesomeb]][awesome]
|
10 | 10 |
|
11 |
| -Package decimal implements immutable decimal floating-point numbers for Go. |
| 11 | +Package decimal implements correctly rounded decimal floating-point numbers for Go. |
12 | 12 | This package is designed specifically for use in transactional financial systems.
|
13 | 13 |
|
14 | 14 | ## Key Features
|
15 | 15 |
|
16 |
| -- **Immutability** - Once set, a decimal remains constant, |
17 |
| - ensuring safe concurrent access across goroutines. |
18 |
| -- **Banker's Rounding** - Uses [half-to-even] rounding, also known as |
19 |
| - "banker's rounding", to minimize cumulative rounding errors commonly seen |
20 |
| - in financial calculations. |
21 |
| -- **Accurate Arithmetic** - For all methods, the result is the one that would |
| 16 | +- **BSON, JSON, XML, SQL** - Implements the necessary interfaces for direct compatibility |
| 17 | + with the [mongo-driver/bson], [encoding/json], [encoding/xml], and [database/sql] packages. |
| 18 | +- **No Heap Allocations** - Optimized to avoid heap allocations, |
| 19 | + preventing garbage collector impact during arithmetic operations. |
| 20 | +- **Correct Rounding** - For all methods, the result is the one that would |
22 | 21 | be obtained if the true mathematical value were rounded to 19 digits of
|
23 |
| - precision using the half-to-even rounding. |
| 22 | + precision using the [half-to-even] rounding (a.k.a. "banker's rounding"). |
24 | 23 | - **No Panics** - All methods are panic-free, returning errors instead of crashing
|
25 | 24 | your application in cases such as overflow or division by zero.
|
26 |
| -- **No Heap Allocations** - Optimized to avoid heap allocations, |
27 |
| - reducing garbage collector impact during arithmetic operations. |
| 25 | +- **Immutability** - Once set, a decimal remains constant, |
| 26 | + ensuring safe concurrent access across goroutines. |
28 | 27 | - **Simple String Representation** - Decimals are represented in a straightforward
|
29 | 28 | format avoiding the complexities of scientific or engineering notations.
|
30 | 29 | - **Rigorous Testing** - All methods are cross-validated against
|
@@ -83,7 +82,9 @@ func main() {
|
83 | 82 | // Transcendental functions
|
84 | 83 | fmt.Println(e.Sqrt()) // √12.5
|
85 | 84 | fmt.Println(e.Exp()) // exp(12.5)
|
| 85 | + fmt.Println(e.Expm1()) // exp(12.5) - 1 |
86 | 86 | fmt.Println(e.Log()) // ln(12.5)
|
| 87 | + fmt.Println(e.Log1p()) // ln(12.5 + 1) |
87 | 88 | fmt.Println(e.Log2()) // log₂(12.5)
|
88 | 89 | fmt.Println(e.Log10()) // log₁₀(12.5)
|
89 | 90 | fmt.Println(e.Pow(d)) // 12.5⁸
|
@@ -118,12 +119,12 @@ Comparison with other popular packages:
|
118 | 119 |
|
119 | 120 | | Feature | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 |
|
120 | 121 | | -------------------- | --------- | ------------------------ | --------------------------- |
|
| 122 | +| Correctly Rounded | Yes | No | No | |
121 | 123 | | Speed | High | Medium | Low[^reason] |
|
122 |
| -| Mutability | Immutable | Mutable[^reason] | Immutable | |
123 | 124 | | Heap Allocations | No | Medium | High |
|
124 |
| -| Panic Free | Yes | Yes | No[^divzero] | |
125 | 125 | | Precision | 19 digits | Arbitrary | Arbitrary |
|
126 |
| -| Correctly Rounded | Yes | No | No | |
| 126 | +| Panic Free | Yes | Yes | No[^divzero] | |
| 127 | +| Mutability | Immutable | Mutable[^reason] | Immutable | |
127 | 128 | | Mathematical Context | Implicit | Explicit | Implicit |
|
128 | 129 |
|
129 | 130 | [^reason]: decimal package was created simply because [shopspring/decimal] was
|
@@ -178,6 +179,10 @@ The benchmark results shown in the table are provided for informational purposes
|
178 | 179 | [awesomeb]: https://awesome.re/mentioned-badge.svg
|
179 | 180 | [cockroachdb/apd]: https://pkg.go.dev/github.com/cockroachdb/apd
|
180 | 181 | [shopspring/decimal]: https://pkg.go.dev/github.com/shopspring/decimal
|
| 182 | +[mongo-driver/bson]: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueUnmarshaler |
| 183 | +[encoding/json]: https://pkg.go.dev/encoding/json#Unmarshaler |
| 184 | +[encoding/xml]: https://pkg.go.dev/encoding#TextUnmarshaler |
| 185 | +[database/sql]: https://pkg.go.dev/database/sql#Scanner |
181 | 186 | [specification]: https://speleotrove.com/decimal/telcoSpec.html
|
182 | 187 | [fuzz testing]: https://github.com/govalues/decimal-tests
|
183 | 188 | [half-to-even]: https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even
|
0 commit comments