Skip to content

Commit 377e9df

Browse files
authored
decimal: implement Expm1, Log1p, AppendText, AppendBinary methods
2 parents 990024f + 4d4b0d0 commit 377e9df

File tree

7 files changed

+1129
-335
lines changed

7 files changed

+1129
-335
lines changed

.github/workflows/go.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,22 @@ jobs:
117117
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Sqrt_PowInt$
118118

119119
- name: Run fuzzing for square root and power
120-
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Sqrt_Pow$
120+
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Pow_Sqrt$
121121

122122
- name: Run fuzzing for power and integer power
123123
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Pow_PowInt$
124124

125125
- name: Run fuzzing for exponential and power
126-
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Exp_Pow$
126+
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Pow_Exp$
127127

128128
- name: Run fuzzing for exponential and logarithm
129-
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Exp_Log$
129+
run: go test -fuzztime 20s -fuzz ^FuzzDecimalp_Log_Exp$
130+
131+
- name: Run fuzzing for shifted exponential and exponential
132+
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Expm1_Exp$
133+
134+
- name: Run fuzzing for shifted logarithm and logarithm
135+
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Log1p_Log$
130136

131137
- name: Run fuzzing for comparison
132138
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Cmp$

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [0.1.36] - 2025-01-19
4+
5+
### Added
6+
7+
- Implemented `Decimal.AppendText`, `Decimal.AppendBinary`, `Decimal.Expm1`, `Decimal.Log1p`.
8+
39
## [0.1.35] - 2025-01-12
410

511
### Added

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88
[![versionb]][version]
99
[![awesomeb]][awesome]
1010

11-
Package decimal implements immutable decimal floating-point numbers for Go.
11+
Package decimal implements correctly rounded decimal floating-point numbers for Go.
1212
This package is designed specifically for use in transactional financial systems.
1313

1414
## Key Features
1515

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
2221
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").
2423
- **No Panics** - All methods are panic-free, returning errors instead of crashing
2524
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.
2827
- **Simple String Representation** - Decimals are represented in a straightforward
2928
format avoiding the complexities of scientific or engineering notations.
3029
- **Rigorous Testing** - All methods are cross-validated against
@@ -83,7 +82,9 @@ func main() {
8382
// Transcendental functions
8483
fmt.Println(e.Sqrt()) // √12.5
8584
fmt.Println(e.Exp()) // exp(12.5)
85+
fmt.Println(e.Expm1()) // exp(12.5) - 1
8686
fmt.Println(e.Log()) // ln(12.5)
87+
fmt.Println(e.Log1p()) // ln(12.5 + 1)
8788
fmt.Println(e.Log2()) // log₂(12.5)
8889
fmt.Println(e.Log10()) // log₁₀(12.5)
8990
fmt.Println(e.Pow(d)) // 12.5⁸
@@ -118,12 +119,12 @@ Comparison with other popular packages:
118119

119120
| Feature | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 |
120121
| -------------------- | --------- | ------------------------ | --------------------------- |
122+
| Correctly Rounded | Yes | No | No |
121123
| Speed | High | Medium | Low[^reason] |
122-
| Mutability | Immutable | Mutable[^reason] | Immutable |
123124
| Heap Allocations | No | Medium | High |
124-
| Panic Free | Yes | Yes | No[^divzero] |
125125
| Precision | 19 digits | Arbitrary | Arbitrary |
126-
| Correctly Rounded | Yes | No | No |
126+
| Panic Free | Yes | Yes | No[^divzero] |
127+
| Mutability | Immutable | Mutable[^reason] | Immutable |
127128
| Mathematical Context | Implicit | Explicit | Implicit |
128129

129130
[^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
178179
[awesomeb]: https://awesome.re/mentioned-badge.svg
179180
[cockroachdb/apd]: https://pkg.go.dev/github.com/cockroachdb/apd
180181
[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
181186
[specification]: https://speleotrove.com/decimal/telcoSpec.html
182187
[fuzz testing]: https://github.com/govalues/decimal-tests
183188
[half-to-even]: https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even

0 commit comments

Comments
 (0)