Fixed severe bug with min and max functions.

This commit is contained in:
Gabriel Queiroz 2015-06-28 12:28:58 -03:00
parent 65c6084205
commit fffa604c32
2 changed files with 9 additions and 5 deletions

View file

@ -489,9 +489,11 @@ func (d Decimal) MarshalText() (text []byte, err error) {
func Min(numbers ...Decimal) Decimal { func Min(numbers ...Decimal) Decimal {
ans := Decimal{} ans := Decimal{}
first := true
for _, item := range numbers { for _, item := range numbers {
if item.Cmp(ans) <= 0 { if item.Cmp(ans) <= 0 || first {
ans = item ans = item
first = false
} }
} }
return ans return ans
@ -499,9 +501,11 @@ func Min(numbers ...Decimal) Decimal {
func Max(numbers ...Decimal) Decimal { func Max(numbers ...Decimal) Decimal {
ans := Decimal{} ans := Decimal{}
first := true
for _, item := range numbers { for _, item := range numbers {
if item.Cmp(ans) >= 0 { if item.Cmp(ans) >= 0 || first {
ans = item ans = item
first = false
} }
} }
return ans return ans

View file

@ -778,9 +778,9 @@ func TestDecimal_Cmp2(t *testing.T) {
func TestDecimal_Min(t *testing.T) { func TestDecimal_Min(t *testing.T) {
a := NewFromFloat(3.14) a := NewFromFloat(3.14)
b := NewFromFloat(1.6) 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") t.Errorf("Error")
} }
} }
@ -790,7 +790,7 @@ func TestDecimal_Max(t *testing.T) {
b := NewFromFloat(1.6) b := NewFromFloat(1.6)
c := NewFromFloat(-2) c := NewFromFloat(-2)
if !Max(a, b, c).Equals(a) { if !Max(b, a, c).Equals(a) {
t.Errorf("Error") t.Errorf("Error")
} }
} }