mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Fixed severe bug with min and max functions.
This commit is contained in:
parent
65c6084205
commit
fffa604c32
2 changed files with 9 additions and 5 deletions
|
@ -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
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue