-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path17-00_floating_point_test.go
57 lines (48 loc) · 1 KB
/
17-00_floating_point_test.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
49
50
51
52
53
54
55
56
57
package hd_test
import (
"math"
"math/rand/v2"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func absFloat32(x float32) float32 {
if x < 0 {
return -x
}
return x
}
func basicRSqrt(x float32) float32 { return float32(1 / math.Sqrt(float64(x))) }
func FuzzRSqrtFloat32(f *testing.F) {
f.Fuzz(func(t *testing.T, x float32) {
if x <= 0 || math.IsInf(float64(x), 1) || math.IsNaN(float64(x)) {
t.Skip()
}
exp := basicRSqrt(x)
got := hd.RSqrtFloat32(x)
if absFloat32(exp-got)/got > hd.SqrtFloat32ErrorRate {
t.Error("x", x, "exp", exp, "got", got)
}
})
}
func BenchmarkRSqrtFloat32(b *testing.B) {
var vals []float32
for i := 0; i < 10000; i++ {
vals = append(vals, rand.Float32())
}
vs := []struct {
name string
f func(x float32) float32
}{
{"basic", basicRSqrt},
{"RSqrtFloat32", hd.RSqrtFloat32},
}
for _, v := range vs {
b.Run(v.name, func(b *testing.B) {
for b.Loop() {
for j := 0; j < len(vals)-1; j++ {
v.f(vals[j])
}
}
})
}
}