mirror of
https://github.com/gonum/gonum.git
synced 2025-10-31 02:26:59 +08:00
Cleaned up Nearest
This commit is contained in:
@@ -235,13 +235,12 @@ func Min(s []float64) (min float64, ind int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nearest returns the index of the element of s whose value is
|
// Nearest returns the index of the element of s whose value is
|
||||||
// nearest to v. If none of the values are closer than infinitely
|
// nearest to v. If several such indices exist, the lowest index
|
||||||
// far away (all of the values in s are either NaN or Inf, or s
|
// is returned
|
||||||
// has length 0), the index is returned as -1
|
|
||||||
// TODO: Add test
|
// TODO: Add test
|
||||||
func Nearest(s []float64, v float64) (ind int, dist float64) {
|
func Nearest(s []float64, v float64) (ind int, dist float64) {
|
||||||
dist = math.Inf(1)
|
dist = math.Abs(v - s[0])
|
||||||
ind = -1
|
ind = 0
|
||||||
for i, val := range s {
|
for i, val := range s {
|
||||||
newDist := math.Abs(v - val)
|
newDist := math.Abs(v - val)
|
||||||
if newDist < dist {
|
if newDist < dist {
|
||||||
|
|||||||
Reference in New Issue
Block a user