From 93a3cd98675e477a4614356297fcd20b95bfba8d Mon Sep 17 00:00:00 2001 From: david Date: Tue, 21 Jun 2022 20:47:50 +0300 Subject: [PATCH 1/3] Add NewFromBigRat function --- decimal.go | 19 +++++++++++++++++++ decimal_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/decimal.go b/decimal.go index c614ea79..a025bdbb 100644 --- a/decimal.go +++ b/decimal.go @@ -125,6 +125,25 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal { } } +// NewFromBigRat returns a new Decimal from a big.Rat. The nominator and +// denominator are divided and rounded to the given precision. +// +// Example: +// +// d1, err := NewFromBigRat(big.NewRat(0, 1), 4) +// d2, err := NewFromBigRat(big.NewRat(4, 5), 100) +// d3, err := NewFromBigRat(big.NewRat(10000, 3), 300) +// +func NewFromBigRat(value *big.Rat, precision int32) Decimal { + return Decimal{ + value: new(big.Int).Set(value.Num()), + exp: 0, + }.DivRound(Decimal{ + value: new(big.Int).Set(value.Denom()), + exp: 0, + }, precision) +} + // NewFromString returns a new Decimal from a string representation. // Trailing zeroes are not trimmed. // diff --git a/decimal_test.go b/decimal_test.go index 2b3a99e1..f9b964a2 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -556,6 +556,51 @@ func TestNewFromBigIntWithExponent(t *testing.T) { } } +func TestNewFromBigRat(t *testing.T) { + mustParseRat := func(val string) *big.Rat { + num, _ := new(big.Rat).SetString(val) + return num + } + + type Inp struct { + val *big.Rat + prec int32 + } + + tests := map[Inp]string{ + Inp{big.NewRat(0, 1), 16}: "0", + Inp{big.NewRat(4, 5), 16}: "0.8", + Inp{big.NewRat(10, 2), 16}: "5", + Inp{big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828", // rounded + Inp{big.NewRat(1, 434324545566634), 16}: "0.0000000000000023", + Inp{big.NewRat(1, 3), 16}: "0.3333333333333333", + Inp{big.NewRat(2, 3), 2}: "0.67", // rounded + Inp{big.NewRat(2, 3), 16}: "0.6666666666666667", // rounded + Inp{big.NewRat(10000, 3), 16}: "3333.3333333333333333", + Inp{mustParseRat("30702832066636633479"), 16}: "30702832066636633479", + Inp{mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753", + Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500", // rounded + Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753", // rounded + Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832", + } + + // add negatives + for p, s := range tests { + if p.val.Cmp(new(big.Rat)) > 0 { + tests[Inp{p.val.Neg(p.val), p.prec}] = "-" + s + } + } + + for input, s := range tests { + d := NewFromBigRat(input.val, input.prec) + if d.String() != s { + t.Errorf("expected %s, got %s (%s, %d)", + s, d.String(), + d.value.String(), d.exp) + } + } +} + func TestCopy(t *testing.T) { origin := New(1, 0) cpy := origin.Copy() From f3eac4fb6504eb4c706bd4f71a0362bf9858d11a Mon Sep 17 00:00:00 2001 From: david Date: Tue, 21 Jun 2022 21:09:28 +0300 Subject: [PATCH 2/3] Fix numerator typo --- decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index a025bdbb..c833a979 100644 --- a/decimal.go +++ b/decimal.go @@ -125,7 +125,7 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal { } } -// NewFromBigRat returns a new Decimal from a big.Rat. The nominator and +// NewFromBigRat returns a new Decimal from a big.Rat. The numerator and // denominator are divided and rounded to the given precision. // // Example: From acffc5529c28a1baa0de07c251fee158df882f52 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 18 Feb 2024 14:54:26 +0200 Subject: [PATCH 3/3] adjust comment --- decimal.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/decimal.go b/decimal.go index c833a979..117fbe93 100644 --- a/decimal.go +++ b/decimal.go @@ -130,9 +130,10 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal { // // Example: // -// d1, err := NewFromBigRat(big.NewRat(0, 1), 4) -// d2, err := NewFromBigRat(big.NewRat(4, 5), 100) -// d3, err := NewFromBigRat(big.NewRat(10000, 3), 300) +// d1 := NewFromBigRat(big.NewRat(0, 1), 0) // output: "0" +// d2 := NewFromBigRat(big.NewRat(4, 5), 1) // output: "0.8" +// d3 := NewFromBigRat(big.NewRat(1000, 3), 3) // output: "333.333" +// d4 := NewFromBigRat(big.NewRat(2, 7), 4) // output: "0.2857" // func NewFromBigRat(value *big.Rat, precision int32) Decimal { return Decimal{