英文:
Finding Max Value Below Zero
问题
我正在尝试从一个差分序列中获取最大值,该差分序列为 n[i] - n[i-1]。切片的第一个值始终为零。以下是代码:
func MaxBelowZero(n ...float64) float64 {
var maxValue float64
if len(n) == 1 {
return n[0]
} else if len(n) == 0 {
return 0.
}
for i := range n {
if i == 0 {
maxValue = math.SmallestNonzeroFloat64
continue
}
if maxValue < n[i] && n[i] < 0 {
maxValue = n[i]
}
}
return maxValue
}
var sliceTest = []float64{0, 1, 2, -1, -2, -10, 10, 20}
MaxBelowZero(sliceTest...)
输出结果为 5e-324,而应该是 -1。我做错了什么?我希望能得到一些帮助。代码在 playground 上:链接
英文:
I'm trying to get the max value from a diff, n[i] - n[i-1], timeseries. The first value is always zero from the slice, here is the code:
func MaxBelowZero(n ...float64) float64 {
var maxValue float64
if len(n) == 1 {
return n[0]
} else if len(n) == 0 {
return 0.
}
for i := range n {
if i == 0 {
maxValue = math.SmallestNonzeroFloat64
continue
}
if maxValue < n[i] && n[i] < 0 {
maxValue = n[i]
}
}
return maxValue
}
var sliceTest = []float64{0, 1, 2, -1, -2, -10, 10, 20}
MaxBelowZero(sliceTest...)
Output: 5e-324
It supossed to be -1. What am I doing wrong? I would appreciate some help.
The code in playground: link
答案1
得分: 5
math.SmallestNonzeroFloat64
是离0最近但不为0的数字,而不是离0最远的数字。你可以尝试以下代码:
func MaxBelowZero(values ...float64) float64 {
if len(values) == 0 {
return 0
} else if len(values) == 1 {
return values[0]
}
max := -math.MaxFloat64
for _, n := range values {
if n >= 0 {
continue
}
if n > max {
max = n
}
}
return max
}
你可以在Go Playground上运行这段代码。
英文:
math.SmallestNonzeroFloat64
is the number closest to 0 that is not 0, not the number furthest from zero. Try this:
func MaxBelowZero(values ...float64) float64 {
if len(values) == 0 {
return 0
} else if len(values) == 1 {
return values[0]
}
max := -math.MaxFloat64
for _, n := range values {
if n >= 0 {
continue
}
if n > max {
max = n
}
}
return max
}
答案2
得分: 1
func MaxBelowZero(n ...float64) float64 {
maxValue := -math.MaxFloat64 // 初始值必须为负数
if len(n) == 1 {
return n[0]
} else if len(n) == 0 {
return 0.
}
for i := 1; i < len(n); i++ {
diff := n[i] - n[i-1] // 正确的逻辑在这里
if diff > maxValue && diff < 0 {
maxValue = diff
}
}
return maxValue
}
英文:
func MaxBelowZero(n ...float64) float64 {
maxValue := -math.MaxFloat64 // Initial value must be negative
if len(n) == 1 {
return n[0]
} else if len(n) == 0 {
return 0.
}
for i := 1; i < len(n); i++ {
diff := n[i] - n[i-1] // Correct logic here
if diff > maxValue && diff < 0 {
maxValue = diff
}
}
return maxValue
}
答案3
得分: -1
你可以对切片进行逆向排序,然后找到第一个负数:
package main
import "sort"
func main() {
a := []float64{0, 1, 2, -1, -2, -10, 10, 20}
sort.Slice(a, func(d, e int) bool {
return a[e] < a[d]
})
n := sort.Search(len(a), func(n int) bool {
return a[n] < 0
})
println(a[n] == -1)
}
或者按符号排序,然后按绝对值排序:
package main
import "math"
type sFunc func(a, b float64) bool
var sFuncs = []sFunc{
func(a, b float64) bool {
return math.Copysign(1, a) < math.Copysign(1, b)
},
func(a, b float64) bool {
return math.Abs(a) < math.Abs(b)
},
}
结果:
package main
import (
"fmt"
"sort"
)
func main() {
floats := []float64{0, 1, 2, -1, -2, -10, 10, 20}
sort.Slice(floats, func(a, b int) bool {
fa, fb := floats[a], floats[b]
for _, sf := range sFuncs {
if sf(fa, fb) {
return true
}
if sf(fb, fa) {
break
}
}
return false
})
fmt.Println(floats) // [-1 -2 -10 0 1 2 10 20]
}
英文:
You could reverse sort the slice, then find first negative number:
package main
import "sort"
func main() {
a := []float64{0, 1, 2, -1, -2, -10, 10, 20}
sort.Slice(a, func(d, e int) bool {
return a[e] < a[d]
})
n := sort.Search(len(a), func(n int) bool {
return a[n] < 0
})
println(a[n] == -1)
}
Or sort by sign, then by absolute value:
package main
import "math"
type sFunc func(a, b float64) bool
var sFuncs = []sFunc{
func(a, b float64) bool {
return math.Copysign(1, a) < math.Copysign(1, b)
},
func(a, b float64) bool {
return math.Abs(a) < math.Abs(b)
},
}
Result:
package main
import (
"fmt"
"sort"
)
func main() {
floats := []float64{0, 1, 2, -1, -2, -10, 10, 20}
sort.Slice(floats, func(a, b int) bool {
fa, fb := floats[a], floats[b]
for _, sf := range sFuncs {
if sf(fa, fb) {
return true
}
if sf(fb, fa) {
break
}
}
return false
})
fmt.Println(floats) // [-1 -2 -10 0 1 2 10 20]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论