From fffa604c326fbba548ebb6d8e3276aaee4b98ea7 Mon Sep 17 00:00:00 2001 From: Gabriel Queiroz Date: Sun, 28 Jun 2015 12:28:58 -0300 Subject: [PATCH] Fixed severe bug with min and max functions. --- decimal.go | 8 ++++++-- decimal_test.go | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/decimal.go b/decimal.go index b0b56aa..7162318 100644 --- a/decimal.go +++ b/decimal.go @@ -489,9 +489,11 @@ func (d Decimal) MarshalText() (text []byte, err error) { func Min(numbers ...Decimal) Decimal { ans := Decimal{} + first := true for _, item := range numbers { - if item.Cmp(ans) <= 0 { + if item.Cmp(ans) <= 0 || first { ans = item + first = false } } return ans @@ -499,9 +501,11 @@ func Min(numbers ...Decimal) Decimal { func Max(numbers ...Decimal) Decimal { ans := Decimal{} + first := true for _, item := range numbers { - if item.Cmp(ans) >= 0 { + if item.Cmp(ans) >= 0 || first { ans = item + first = false } } return ans diff --git a/decimal_test.go b/decimal_test.go index 7c7a613..32ed0d3 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -778,9 +778,9 @@ func TestDecimal_Cmp2(t *testing.T) { func TestDecimal_Min(t *testing.T) { a := NewFromFloat(3.14) b := NewFromFloat(1.6) - c := NewFromFloat(-2) + c := NewFromFloat(0.02) - if !Min(a, b, c).Equals(c) { + if !Min(a, c, b).Equals(c) { t.Errorf("Error") } } @@ -790,7 +790,7 @@ func TestDecimal_Max(t *testing.T) { b := NewFromFloat(1.6) c := NewFromFloat(-2) - if !Max(a, b, c).Equals(a) { + if !Max(b, a, c).Equals(a) { t.Errorf("Error") } }