寻找小于零的最大值

huangapple go评论81阅读模式
英文:

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 &lt; n[i] &amp;&amp; n[i] &lt; 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:

go playground

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 &gt;= 0 {
			continue
		}
		if n &gt; 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 &lt; len(n); i++ {
		diff := n[i] - n[i-1] // Correct logic here
		if diff &gt; maxValue &amp;&amp; diff &lt; 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 &quot;sort&quot;

func main() {
   a := []float64{0, 1, 2, -1, -2, -10, 10, 20}
   sort.Slice(a, func(d, e int) bool {
      return a[e] &lt; a[d]
   })
   n := sort.Search(len(a), func(n int) bool {
      return a[n] &lt; 0
   })
   println(a[n] == -1)
}

Or sort by sign, then by absolute value:

package main
import &quot;math&quot;

type sFunc func(a, b float64) bool

var sFuncs = []sFunc{
   func(a, b float64) bool {
      return math.Copysign(1, a) &lt; math.Copysign(1, b)
   },
   func(a, b float64) bool {
      return math.Abs(a) &lt; math.Abs(b)
   },
}

Result:

package main

import (
   &quot;fmt&quot;
   &quot;sort&quot;
)

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]
}

huangapple
  • 本文由 发表于 2021年6月29日 05:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68170365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定