Skip to content

Commit

Permalink
decimal: implement Equal, Less, SubMul, SubQuo methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eapenkin authored Aug 30, 2024
1 parent 2ac296c commit d5ed3ca
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 250 deletions.
29 changes: 19 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ jobs:
run: go test -fuzztime 20s -fuzz ^FuzzBCD$

- name: Run fuzzing for string conversion
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_String$
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_String_Parse$

- name: Run fuzzing for binary conversion
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_BCD$
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_BCD_ParseBCD$

- name: Run fuzzing for float64 conversion
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Float64$
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Float64_NewFromFloat64$

- name: Run fuzzing for int64 conversion
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Int64$
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Int64_NewFromInt64$

- name: Run fuzzing for addition
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Add$
Expand All @@ -80,23 +80,32 @@ jobs:
- name: Run fuzzing for fused multiply-addition
run: go test -fuzztime 60s -fuzz ^FuzzDecimal_AddMul$

- name: Run fuzzing for fused quotient-addition
run: go test -fuzztime 60s -fuzz ^FuzzDecimal_AddQuo$

- name: Run fuzzing for square root
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Sqrt$
- name: Run fuzzing for fused multiply-addition
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Add_Mul_AddMul$

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

- name: Run fuzzing for fused quotient-addition
run: go test -fuzztime 60s -fuzz ^FuzzDecimal_AddQuo$

- name: Run fuzzing for fused quotient-addition
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Add_Quo_AddQuo$

- name: Run fuzzing for integer division and remainder
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_QuoRem$

- name: Run fuzzing for square root
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Sqrt_Pow$

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

- name: Run fuzzing for comparison and subtraction
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_CmpSub$
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Cmp_Sub$

- name: Run fuzzing for constructor
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_New$

- name: Run fuzzing for padding
run: go test -fuzztime 20s -fuzz ^FuzzDecimal_Pad$
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.1.31] - 2024-08-30

### Added

- Implemented `Decimal.SubMul`, `Decimal.SubQuo`, `Decimal.Equal`, `Decimal.Less`.

## [0.1.30] - 2024-08-29

### Added
Expand Down
49 changes: 26 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ func main() {
// 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.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.Quo(e)) // 8 ÷ 12.5
fmt.Println(d.AddQuo(e, f)) // 8 + 12.5 ÷ 2.567
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.Inv()) // 1 / 8

fmt.Println(d.Pow(2)) //
fmt.Println(d.Sqrt()) // √8
fmt.Println(d.Exp()) // e⁸
fmt.Println(d.Exp()) // exp(8)
fmt.Println(d.Pow(2)) //

// Rounding to 2 decimal places
fmt.Println(g.Round(2)) // 7.90
Expand Down Expand Up @@ -125,24 +128,24 @@ pkg: github.com/govalues/decimal-tests
cpu: AMD Ryzen 7 3700C with Radeon Vega Mobile Gfx
```

| Test Case | Expression | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 | govalues vs cockroachdb | govalues vs shopspring |
| ------------- | -------------------- | -------: | -----------------------: | --------------------------: | ----------------------: | ---------------------: |
| Add | 5 + 6 | 16.06n | 74.88n | 140.90n | +366.22% | +777.33% |
| Mul | 2 * 3 | 16.93n | 62.20n | 146.00n | +267.40% | +762.37% |
| Quo (exact) | 2 ÷ 4 | 59.52n | 176.95n | 657.40n | +197.30% | +1004.50% |
| Quo (inexact) | 2 ÷ 3 | 391.60n | 976.80n | 2962.50n | +149.39% | +656.42% |
| Pow | 1.1^60 | 950.90n | 3302.50n | 4599.50n | +247.32% | +383.73% |
| Pow | 1.01^600 | 3.45µ | 10.67µ | 18.67µ | +209.04% | +440.89% |
| Pow | 1.001^6000 | 5.94µ | 20.50µ | 722.22µ | +244.88% | +12052.44% |
| Sqrt | √2 | 3.40µ | 4.96µ | 2101.86µ | +46.00% | +61755.71% |
| Exp | exp(0.5) | 8.35µ | 39.28µ | 20.06µ | +370.58% | +140.32% |
| Parse | 1 | 16.52n | 76.30n | 136.55n | +362.00% | +726.82% |
| Parse | 123.456 | 47.37n | 176.90n | 242.60n | +273.44% | +412.14% |
| Parse | 123456789.1234567890 | 85.49n | 224.15n | 497.95n | +162.19% | +482.47% |
| String | 1 | 5.11n | 19.57n | 198.25n | +283.21% | +3783.07% |
| String | 123.456 | 35.78n | 77.12n | 228.85n | +115.52% | +539.51% |
| String | 123456789.1234567890 | 70.72n | 239.10n | 337.25n | +238.12% | +376.91% |
| Telco | see [specification] | 137.00n | 969.40n | 3981.00n | +607.33% | +2804.78% |
| Test Case | Expression | govalues | [cockroachdb/apd] v3.2.1 | [shopspring/decimal] v1.4.0 | govalues vs cockroachdb | govalues vs shopspring |
| --------- | --------------------- | -------: | -----------------------: | --------------------------: | ----------------------: | ---------------------: |
| Add | 5 + 6 | 16.06n | 74.88n | 140.90n | +366.22% | +777.33% |
| Mul | 2 * 3 | 16.93n | 62.20n | 146.00n | +267.40% | +762.37% |
| Quo | 2 / 4 (exact) | 59.52n | 176.95n | 657.40n | +197.30% | +1004.50% |
| Quo | 2 / 3 (inexact) | 391.60n | 976.80n | 2962.50n | +149.39% | +656.42% |
| Pow | 1.1^60 | 950.90n | 3302.50n | 4599.50n | +247.32% | +383.73% |
| Pow | 1.01^600 | 3.45µ | 10.67µ | 18.67µ | +209.04% | +440.89% |
| Pow | 1.001^6000 | 5.94µ | 20.50µ | 722.22µ | +244.88% | +12052.44% |
| Sqrt | √2 | 3.40µ | 4.96µ | 2101.86µ | +46.00% | +61755.71% |
| Exp | exp(0.5) | 8.35µ | 39.28µ | 20.06µ | +370.58% | +140.32% |
| Parse | 1 | 16.52n | 76.30n | 136.55n | +362.00% | +726.82% |
| Parse | 123.456 | 47.37n | 176.90n | 242.60n | +273.44% | +412.14% |
| Parse | 123456789.1234567890 | 85.49n | 224.15n | 497.95n | +162.19% | +482.47% |
| String | 1 | 5.11n | 19.57n | 198.25n | +283.21% | +3783.07% |
| String | 123.456 | 35.78n | 77.12n | 228.85n | +115.52% | +539.51% |
| String | 123456789.1234567890 | 70.72n | 239.10n | 337.25n | +238.12% | +376.91% |
| Telco | (see [specification]) | 137.00n | 969.40n | 3981.00n | +607.33% | +2804.78% |

The benchmark results shown in the table are provided for informational purposes only and may vary depending on your specific use case.

Expand Down
Loading

0 comments on commit d5ed3ca

Please sign in to comment.