-
Notifications
You must be signed in to change notification settings - Fork 0
/
angularspeed.go
67 lines (52 loc) · 1.69 KB
/
angularspeed.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
package unit
import (
"encoding"
"fmt"
"math"
"strconv"
)
// AngularSpeed is a measure of rotation rate.
type AngularSpeed float64
var _ encoding.TextUnmarshaler = (*AngularSpeed)(nil)
// RadianPerSecond is the SI unit for measuring AngularSpeed.
const RadianPerSecond AngularSpeed = 1.0
const radianPerSecondSymbol = "rad/s"
// RPM is the number of revolutions in one minute.
const RPM = RadianPerSecond * (2 * math.Pi) / 60
const rpmSymbol = "RPM"
// DegreePerSecond is angular speed measured in degrees per second.
const DegreePerSecond = RadianPerSecond / 180 * math.Pi
const degreePerSecondSymbol = "°/s"
// RadiansPerSecond returns a with the unit of rad/s.
func (a AngularSpeed) RadiansPerSecond() float64 {
return float64(a)
}
// DegreesPerSecond returns a with the unit of °/s.
func (a AngularSpeed) DegreesPerSecond() float64 {
return float64(a * 180 / math.Pi)
}
// Get returns a with the unit of as.
func (a AngularSpeed) Get(as AngularSpeed) float64 {
return float64(a) / float64(as)
}
// String implements fmt.Stringer.
func (a AngularSpeed) String() string {
return strconv.FormatFloat(float64(a), 'f', -1, 64) + radianPerSecondSymbol
}
// UnmarshalString sets *a from s.
func (a *AngularSpeed) UnmarshalString(s string) error {
parsed, err := parse(s, map[string]float64{
radianPerSecondSymbol: float64(RadianPerSecond),
rpmSymbol: float64(RPM),
degreePerSecondSymbol: float64(DegreePerSecond),
})
if err != nil {
return fmt.Errorf("unmarshal angular speed: %w", err)
}
*a = AngularSpeed(parsed)
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (a *AngularSpeed) UnmarshalText(text []byte) error {
return a.UnmarshalString(string(text))
}