This repository has been archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
length.go
92 lines (83 loc) · 2.07 KB
/
length.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
// This file is autogenerated by github.com/gonum/unit/autogen
// Changes should be made to the autogenerated template rather than this one
// Copyright ©2014 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package unit
import (
"errors"
"fmt"
"math"
)
// Length represents a length in meters
type Length float64
const (
Yottameter Length = 1e24
Zettameter Length = 1e21
Exameter Length = 1e18
Petameter Length = 1e15
Terameter Length = 1e12
Gigameter Length = 1e9
Megameter Length = 1e6
Kilometer Length = 1e3
Hectometer Length = 1e2
Decameter Length = 1e1
Meter Length = 1.0
Decimeter Length = 1e-1
Centimeter Length = 1e-2
Millimeter Length = 1e-3
Micrometer Length = 1e-6
Nanometer Length = 1e-9
Picometer Length = 1e-12
Femtometer Length = 1e-15
Attometer Length = 1e-18
Zeptometer Length = 1e-21
Yoctometer Length = 1e-24
)
// Unit converts the Length to a *Unit
func (l Length) Unit() *Unit {
return New(float64(l), Dimensions{
LengthDim: 1,
})
}
// Length allows Length to implement a Lengther interface
func (l Length) Length() Length {
return l
}
// From converts the unit into the receiver. From returns an
// error if there is a mismatch in dimension
func (l *Length) From(u Uniter) error {
if !DimensionsMatch(u, Meter) {
*l = Length(math.NaN())
return errors.New("Dimension mismatch")
}
*l = Length(u.Unit().Value())
return nil
}
func (l Length) Format(fs fmt.State, c rune) {
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "%T(%v)", l, float64(l))
return
}
fallthrough
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
switch {
case pOk && wOk:
fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(l))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64(l))
case wOk:
fmt.Fprintf(fs, "%*"+string(c), w, float64(l))
default:
fmt.Fprintf(fs, "%"+string(c), float64(l))
}
fmt.Fprint(fs, " m")
default:
fmt.Fprintf(fs, "%%!%c(%T=%g m)", c, l, float64(l))
return
}
}