Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decimal: impement Sum and Prod functions #54

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ jobs:
- name: Run fuzzing for fused multiply-addition
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Mul_AddMul$

- name: Run fuzzing for summation
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Add_Sum$

- name: Run fuzzing for product
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Mul_Prod$

- name: Run fuzzing for division
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Quo$

Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ linters:
- misspell
- stylecheck
- revive
- predeclared
- staticcheck
- typecheck
- unused
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# Changelog

## [0.1.33] - 2024-11-16

### Added

- Implemented `Sum`, `Prod`.

## [0.1.32] - 2024-09-28

### Added

- Implemented `Decimal.Log`.

### Changed

- `Decimal.PowInt` always correctly rounds the result.
- Deprecated `Decimal.Pow`.

## [0.1.31] - 2024-08-30

Expand Down
69 changes: 36 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,43 @@ func main() {
g, _ := decimal.NewFromInt64(7, 896, 3) // g = 7.896

// Arithmetic operations
fmt.Println(d.Add(e)) // 8 + 12.5
fmt.Println(d.Sub(e)) // 8 - 12.5
fmt.Println(d.SubAbs(e)) // abs(8 - 12.5)
fmt.Println(d.Add(e)) // 8 + 12.5
fmt.Println(d.Sub(e)) // 8 - 12.5
fmt.Println(d.SubAbs(e)) // abs(8 - 12.5)

fmt.Println(d.Mul(e)) // 8 * 12.5
fmt.Println(d.AddMul(e, f)) // 8 + 12.5 * 2.567
fmt.Println(d.SubMul(e, f)) // 8 - 12.5 * 2.567
fmt.Println(d.PowInt(2)) // 8²
fmt.Println(d.Mul(e)) // 8 * 12.5
fmt.Println(d.AddMul(e, f)) // 8 + 12.5 * 2.567
fmt.Println(d.SubMul(e, f)) // 8 - 12.5 * 2.567
fmt.Println(d.PowInt(2)) // 8²

fmt.Println(d.Quo(e)) // 8 / 12.5
fmt.Println(d.AddQuo(e, f)) // 8 + 12.5 / 2.567
fmt.Println(d.SubQuo(e, f)) // 8 - 12.5 / 2.567
fmt.Println(d.QuoRem(e)) // 8 div 12.5, 8 mod 12.5
fmt.Println(d.Inv()) // 1 / 8
fmt.Println(d.Quo(e)) // 8 / 12.5
fmt.Println(d.AddQuo(e, f)) // 8 + 12.5 / 2.567
fmt.Println(d.SubQuo(e, f)) // 8 - 12.5 / 2.567
fmt.Println(d.QuoRem(e)) // 8 div 12.5, 8 mod 12.5
fmt.Println(d.Inv()) // 1 / 8

fmt.Println(decimal.Sum(d, e, f)) // 8 + 12.5 + 2.567
fmt.Println(decimal.Prod(d, e, f)) // 8 * 12.5 * 2.567

// Transcendental functions
fmt.Println(d.Sqrt()) // √8
fmt.Println(d.Exp()) // exp(8)
fmt.Println(d.Log()) // ln(8)
fmt.Println(e.Sqrt()) // √12.5
fmt.Println(e.Exp()) // exp(12.5)
fmt.Println(e.Log()) // ln(12.5)

// Rounding to 2 decimal places
fmt.Println(g.Round(2)) // 7.90
fmt.Println(g.Ceil(2)) // 7.90
fmt.Println(g.Floor(2)) // 7.89
fmt.Println(g.Trunc(2)) // 7.89
fmt.Println(g.Round(2)) // 7.90
fmt.Println(g.Ceil(2)) // 7.90
fmt.Println(g.Floor(2)) // 7.89
fmt.Println(g.Trunc(2)) // 7.89

// Conversions
fmt.Println(f.Int64(9)) // 2 567000000
fmt.Println(f.Float64()) // 2.567
fmt.Println(f.String()) // 2.567
fmt.Println(f.Int64(9)) // 2 567000000
fmt.Println(f.Float64()) // 2.567
fmt.Println(f.String()) // 2.567

// Formatting
fmt.Printf("%.2f", f) // 2.57
fmt.Printf("%.2k", f) // 256.70%
fmt.Printf("%.2f", f) // 2.57
fmt.Printf("%.2k", f) // 256.70%
}
```

Expand All @@ -108,15 +111,15 @@ For examples related to financial calculations, see the `money` package

Comparison with other popular packages:

| Feature | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 |
| ----------------- | --------- | ------------------------ | --------------------------- |
| Speed | High | Medium | Low[^reason] |
| Mutability | Immutable | Mutable[^reason] | Immutable |
| Heap Allocations | No | Medium | High |
| Panic Free | Yes | Yes | No[^divzero] |
| Precision | 19 digits | Arbitrary | Arbitrary |
| Correctly Rounded | Yes | No | No |
| Context | Implicit | Explicit | Implicit |
| Feature | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 |
| -------------------- | --------- | ------------------------ | --------------------------- |
| Speed | High | Medium | Low[^reason] |
| Mutability | Immutable | Mutable[^reason] | Immutable |
| Heap Allocations | No | Medium | High |
| Panic Free | Yes | Yes | No[^divzero] |
| Precision | 19 digits | Arbitrary | Arbitrary |
| Correctly Rounded | Yes | No | No |
| Mathematical Context | Implicit | Explicit | Implicit |

[^reason]: decimal package was created simply because [shopspring/decimal] was
too slow and [cockroachdb/apd] was mutable.
Expand Down
Loading