-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
41 lines (34 loc) · 937 Bytes
/
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
// Copyright 2021 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pps
import "math"
// sign returns the sign, also called signum, of i: -1 for a negative number,
// 0 for the number zero, and +1 for a positive number.
func sign(i int) int {
switch {
case i < 0:
return -1
case i > 0:
return +1
}
return 0
}
// deg2rad converts the measurement of an angle from degrees to radians.
func deg2rad(deg float64) (rad float64) {
return deg * (math.Pi / 180)
}
func wrap(n, max float64) float64 {
mod := math.Mod(n, max)
if mod < 0 {
return max + mod
}
return mod
}
const epsilon = 1e-10
// nearEq compares two floating-point numbers for equality within an
// absolute difference tolerance of epsilon.
// This relation is not transitive, except for ε=0.
func nearEq(a, b, ε float64) bool {
return math.Abs(a-b) <= ε
}