-
Notifications
You must be signed in to change notification settings - Fork 7
/
tol.go
48 lines (42 loc) · 1.03 KB
/
tol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2019, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package metric
import (
"math"
"cogentcore.org/core/math32"
)
///////////////////////////////////////////
// Tolerance
// Tolerance32 sets a = b for any element where |a-b| <= tol.
// This can be called prior to any metric function.
func Tolerance32(a, b []float32, tol float32) {
if len(a) != len(b) {
panic("metric: slice lengths do not match")
}
for i, av := range a {
bv := b[i]
if math32.IsNaN(av) || math32.IsNaN(bv) {
continue
}
if math32.Abs(av-bv) <= tol {
a[i] = bv
}
}
}
// Tolerance64 sets a = b for any element where |a-b| <= tol.
// This can be called prior to any metric function.
func Tolerance64(a, b []float64, tol float64) {
if len(a) != len(b) {
panic("metric: slice lengths do not match")
}
for i, av := range a {
bv := b[i]
if math.IsNaN(av) || math.IsNaN(bv) {
continue
}
if math.Abs(av-bv) <= tol {
a[i] = bv
}
}
}