Skip to content

Commit

Permalink
Range回傳改float64
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Sep 14, 2024
1 parent b73367a commit ed87c0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
21 changes: 12 additions & 9 deletions datalist.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type IDataList interface {
StdevP() float64
Var() float64
VarP() float64
Range() interface{}
Range() float64
Quartile(int) interface{}
IQR() interface{}
Percentile(float64) interface{}
Expand Down Expand Up @@ -1508,17 +1508,20 @@ func (dl *DataList) VarP() float64 {
}

// Range calculates the range of the DataList.
// Returns the range.
// Returns nil if the DataList is empty.
// Range returns the range of the DataList.
func (dl *DataList) Range() interface{} {
// Returns math.NaN() if the DataList is empty or if Max or Min cannot be calculated.
func (dl *DataList) Range() float64 {
if len(dl.data) == 0 {
LogWarning("DataList.Range(): DataList is empty, returning nil.")
return nil
LogWarning("DataList.Range(): DataList is empty.")
return math.NaN()
}

max := ToFloat64(dl.Max())
min := ToFloat64(dl.Min())
max := dl.Max()
min := dl.Min()

if math.IsNaN(max) || math.IsNaN(min) {
LogWarning("DataList.Range(): Max or Min calculation failed.")
return math.NaN()
}

return max - min
}
Expand Down
10 changes: 10 additions & 0 deletions insyra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ func TestVarP(t *testing.T) {
}
}

// 測試 Range 函數
func TestRange(t *testing.T) {
dl := NewDataList(1, 2, 3, 4)
r := dl.Range()

if !float64Equal(r, 3) {
t.Errorf("Expected range 3, got %v", r)
}
}

// 測試 SetName 和 GetName 函數
func TestSetName(t *testing.T) {
dl := NewDataList(1, 2, 3, 4)
Expand Down

0 comments on commit ed87c0a

Please sign in to comment.