Skip to content

Commit

Permalink
Max回傳值改float64
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Sep 14, 2024
1 parent 3d1f210 commit 018c115
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
38 changes: 21 additions & 17 deletions datalist.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type IDataList interface {
Capitalize() *DataList
// Statistics
Sum() float64
Max() interface{}
Max() float64
Min() interface{}
Mean() float64
WeightedMean(weights interface{}) interface{}
Expand Down Expand Up @@ -727,14 +727,14 @@ func (dl *DataList) Normalize() *DataList {
go dl.updateTimestamp()
}()
min, max := dl.Min(), dl.Max()
if min == nil || max == nil {
if min == nil || math.IsNaN(max) {
LogWarning("Normalize: Cannot normalize due to invalid Min/Max values.")
return nil
}

for i, v := range dl.Data() {
vfloat := conv.ParseF64(v)
dl.data[i] = (vfloat - min.(float64)) / (max.(float64) - min.(float64))
dl.data[i] = (vfloat - min.(float64)) / (max - min.(float64))
}
return dl
}
Expand Down Expand Up @@ -1086,33 +1086,37 @@ func (dl *DataList) Sum() float64 {
}

// Max returns the maximum value in the DataList.
// Returns the maximum value.
// Returns nil if the DataList is empty.
// Max returns the maximum value in the DataList, or nil if the data types cannot be compared.
func (dl *DataList) Max() interface{} {
defer func() {
r := recover()
if r != nil {
LogWarning("DataList.Max(): Data types cannot be compared, returning nil.")
}
}()
// Returns math.NaN() if the DataList is empty or if no elements can be converted to float64.
func (dl *DataList) Max() float64 {
if len(dl.data) == 0 {
return nil
LogWarning("DataList.Max(): DataList is empty.")
return math.NaN()
}

var max = math.NaN()
var max float64
var foundValid bool

for _, v := range dl.data {
vfloat := conv.ParseF64(v)
if math.IsNaN(max) {
vfloat, ok := ToFloat64Safe(v)
if !ok {
LogWarning("DataList.Max(): Element %v is not a numeric type, skipping.", v)
continue
}
if !foundValid {
max = vfloat
foundValid = true
continue
}
if vfloat > max {
max = vfloat
}
}

if !foundValid {
LogWarning("DataList.Max(): No valid elements to compute maximum.")
return math.NaN()
}

return max
}

Expand Down
2 changes: 1 addition & 1 deletion insyra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestMax(t *testing.T) {
dl := NewDataList(1, 2, 3, 4)
max := dl.Max()

if v, ok := max.(float64); !ok || !float64Equal(v, 4) {
if !float64Equal(max, 4) {
t.Errorf("Expected max 4, got %v", max)
}
}
Expand Down

0 comments on commit 018c115

Please sign in to comment.