-
Notifications
You must be signed in to change notification settings - Fork 1
/
capacitor.go
84 lines (73 loc) · 1.46 KB
/
capacitor.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
package main
import (
"flag"
"fmt"
"math"
"os"
"strconv"
)
var prefixes []string
func init() {
prefixes = []string{"p", "n", "u", "m", ""}
}
// return rounded version of x with prec precision.
func roundFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
intermed += .5
x = .5
if frac < 0.0 {
x = -.5
intermed -= 1
}
if frac >= x {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}
return rounder / pow
}
func getPrefix(value float64) string {
fmt.Println(math.Log10(value))
return prefixes[int(math.Log10(value))/3+1]
}
func simplify(value float64) float64 {
if roundFloat(value, 14) == value {
return roundFloat(value, 14)
}
return value
}
func Usage() {
flag.PrintDefaults()
fmt.Printf("Usage: %s <code>\n", os.Args[0])
os.Exit(0)
}
func main() {
flag.Parse()
code := flag.Arg(0)
if code == "" {
Usage()
}
if _, err := strconv.ParseUint(code, 10, 64); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
Usage()
}
base, _ := strconv.ParseInt(code[:2], 10, 16)
exponent := 0
if code[2:] != "" {
tmp, _ := strconv.ParseInt(code[2:], 10, 16)
exponent = int(tmp)
}
exponent++
prefix := prefixes[exponent/3]
value := simplify(float64(base) * math.Pow10(exponent%3))
fmt.Printf("%g %sF\n", value, prefix)
if value < 100 {
return
}
value /= 1000
prefix = prefixes[exponent/3+1]
fmt.Printf("%g %sF\n", value, prefix)
}