-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.go
211 lines (175 loc) · 5.44 KB
/
distance.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package distance
import (
"encoding/json"
"fmt"
"math"
)
type Distance int64
const (
minDistance Distance = -1 << 63
maxDistance Distance = 1<<63 - 1
)
const (
Nanometer Distance = 1
Micrometer = 1000 * Nanometer
Micron = Micrometer
Millimeter = 1000 * Micrometer
Centimeter = 10 * Millimeter
Decimeter = 100 * Millimeter
Meter = 1000 * Millimeter
Dekameter = 10 * Meter
Hectometer = 100 * Meter
Kilometer = 1000 * Meter
Megameter = 1000 * Kilometer
Gigameter = 1000 * Megameter
// Terameter = 1000 * Gigameter
)
// Change output of String
var Imperial bool = false
const (
Thou Distance = 25400
Mil = Thou
Barleycorn = 846670
Inch = Mil * 1000
Hand = Inch * 4
Foot = Inch * 12
Yard = Foot * 3
Chain = Foot * 66
Furlong = Foot * 660
Mile = Foot * 5280
League = Foot * 15840
Fathom = Thou * 72000
Cable = Fathom * 100
NauticalMile = Cable * 10
Link = Thou * 7920
Rod = Thou * 198000
)
func (d Distance) String() string {
var val float64
var out string
//neg := d < 0
if Imperial {
switch {
case d < Inch:
val = float64(d) / float64(Mil)
return fmt.Sprintf("%smil", fmtWholeOrFrac(val))
case d >= Inch && d < Mile:
ft := d / Foot
in := float64(d%Foot) / float64(Inch)
return fmt.Sprintf("%dft%sin", ft, fmtWholeOrFrac(in))
case d >= Mile:
val = float64(d) / float64(Mile)
return fmt.Sprintf("%smi", fmtWholeOrFrac(val))
}
} else {
out = ""
switch {
case d >= Kilometer:
val = float64(d) / float64(Kilometer)
return fmt.Sprintf("%skm", fmtWholeOrFrac(val))
case d < Kilometer && d >= Meter:
// Show meters
M := d / Meter
if M > 0 {
out += fmt.Sprintf("%dm", M)
}
d -= M * Meter
fallthrough
case d < Meter && d >= Centimeter:
cm := d / Centimeter
if cm > 0 {
out += fmt.Sprintf("%dcm", cm)
}
d -= cm * Centimeter
fallthrough
case d < Centimeter && d >= Millimeter:
mm := d / Millimeter
if mm > 0 {
out += fmt.Sprintf("%dmm", mm)
}
d -= mm * Millimeter
fallthrough
case d < Millimeter && d >= Micrometer:
um := d / Micrometer
if um > 0 {
out += fmt.Sprintf("%dμm", um)
}
}
}
return out
}
func fmtWholeOrFrac(v float64) string {
w, f := math.Modf(v)
if f == 0 {
// Whole Number
return fmt.Sprintf("%.0f", w)
}
return fmt.Sprintf("%.2f", v)
}
func (d Distance) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d Distance) Nanometers() int64 { return int64(d) }
func (d Distance) Micrometers() float64 { return float64(d) / float64(Micrometer) }
func (d Distance) Millimeters() float64 { return float64(d) / float64(Millimeter) }
func (d Distance) Centimeters() float64 { return float64(d) / float64(Centimeter) }
func (d Distance) Decimeters() float64 { return float64(d) / float64(Decimeter) }
func (d Distance) Meters() float64 { return float64(d) / float64(Meter) }
func (d Distance) Dekameters() float64 { return float64(d) / float64(Dekameter) }
func (d Distance) Hectometers() float64 { return float64(d) / float64(Hectometer) }
func (d Distance) Kilometers() float64 { return float64(d) / float64(Kilometer) }
func (d Distance) Thous() float64 { return float64(d) / float64(Thou) }
func (d Distance) Mils() float64 { return d.Thous() }
func (d Distance) Barleycorns() float64 { return float64(d) / float64(Barleycorn) }
func (d Distance) Inches() float64 { return float64(d) / float64(Inch) }
func (d Distance) Feet() float64 { return float64(d) / float64(Foot) }
func (d Distance) Yards() float64 { return float64(d) / float64(Yard) }
func (d Distance) Furlongs() float64 { return float64(d) / float64(Furlong) }
func (d Distance) Miles() float64 { return float64(d) / float64(Mile) }
func (d Distance) Fathoms() float64 { return float64(d) / float64(Fathom) }
func (d Distance) Cables() float64 { return float64(d) / float64(Cable) }
func (d Distance) NauticalMiles() float64 { return float64(d) / float64(NauticalMile) }
func (d Distance) Links() float64 { return float64(d) / float64(Link) }
func (d Distance) Rods() float64 { return float64(d) / float64(Rod) }
// Truncate returns the result of rounding d toward zero to a multiple of m.
// If m <= 0, Truncate returns d unchanged.
func (d Distance) Truncate(m Distance) Distance {
if m <= 0 {
return d
}
return d - d%m
}
// lessThanHalf reports whether x+x < y but avoids overflow,
// assuming x and y are both positive (Distance is signed).
func lessThanHalf(x, y Distance) bool {
return uint64(x)+uint64(x) < uint64(y)
}
// Round returns the result of rounding d to the nearest multiple of m.
// The rounding behavior for halfway values is to round away from zero.
// If the result exceeds the maximum (or minimum)
// value that can be stored in a Distance,
// Round returns the maximum (or minimum) duration.
// If m <= 0, Round returns d unchanged.
func (d Distance) Round(m Distance) Distance {
if m <= 0 {
return d
}
r := d % m
if d < 0 {
r = -r
if lessThanHalf(r, m) {
return d + r
}
if d1 := d - m + r; d1 < d {
return d1
}
return minDistance // overflow
}
if lessThanHalf(r, m) {
return d - r
}
if d1 := d + m - r; d1 > d {
return d1
}
return maxDistance // overflow
}