-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmath.go
73 lines (62 loc) · 1.16 KB
/
math.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package flex
import "math"
// from https://github.com/rkusa/gm/blob/master/math32/bits.go
const (
uvnan = 0x7FC00001
)
var (
NAN = math.Float32frombits(uvnan)
)
// NaN returns an IEEE 754 ``not-a-number'' value.
func NaN() float32 { return math.Float32frombits(uvnan) }
// IsNaN reports whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float32) (is bool) {
return f != f
}
func feq(a, b float32) bool {
if IsNaN(a) && IsNaN(b) {
return true
}
return a == b
}
// https://github.com/evanphx/ulysses-libc/blob/master/src/math/fmaxf.c
func fmaxf(a float32, b float32) float32 {
if IsNaN(a) {
return b
}
if IsNaN(b) {
return a
}
// TODO: signed zeros
if a > b {
return a
}
return b
}
// https://github.com/evanphx/ulysses-libc/blob/master/src/math/fminf.c
func fminf(a float32, b float32) float32 {
if IsNaN(a) {
return b
}
if IsNaN(b) {
return a
}
// TODO: signed zeros
if a < b {
return a
}
return b
}
func fabs(x float32) float32 {
switch {
case x < 0:
return -x
case x == 0:
return 0 // return correctly abs(-0)
}
return x
}
func fmodf(x, y float32) float32 {
res := math.Mod(float64(x), float64(y))
return float32(res)
}