-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.go
More file actions
51 lines (41 loc) · 1.05 KB
/
unit.go
File metadata and controls
51 lines (41 loc) · 1.05 KB
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
package quant
import "math"
// Unit converts values between a unit and the base unit for a dimension.
//
// Implementations should be small value types so conversions stay allocation-free
// and inline well.
type Unit[D any] interface {
toBase(float64) float64
fromBase(float64) float64
}
type scaleUnit[D any] struct {
factor float64
}
func (u scaleUnit[D]) toBase(v float64) float64 {
return v * u.factor
}
func (u scaleUnit[D]) fromBase(v float64) float64 {
return v / u.factor
}
type affineUnit[D any] struct {
factor float64
offset float64
}
func (u affineUnit[D]) toBase(v float64) float64 {
return (v + u.offset) * u.factor
}
func (u affineUnit[D]) fromBase(v float64) float64 {
return v/u.factor - u.offset
}
const (
usSurveyFootInMeters = 1200.0 / 3937.0
secondsPerDay = 24 * 60 * 60
secondsPerWeek = 7 * secondsPerDay
secondsPerYear = 365.25 * secondsPerDay
secondsPerMonth = secondsPerYear / 12
standardGravity = 9.80665
)
var (
metersPerSquareMile = 1609.344 * 1609.344
radiansPerDegree = math.Pi / 180
)