-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
119 lines (108 loc) · 3.27 KB
/
conv.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
package ucum
import (
"fmt"
"github.com/iimos/ucum/internal/data"
"github.com/iimos/ucum/internal/types"
"math/big"
)
var (
bigZero = big.NewInt(0)
bigRatOne = big.NewRat(1, 1)
)
// PairConverter makes conversion between two UCUM units.
type PairConverter interface {
ConvRat(val *big.Rat) *big.Rat
ConvBigInt(val *big.Int) (converted *big.Int, exact bool)
ConvFloat64(val float64) float64
}
// NewPairConverter creates a new PairConverter.
func NewPairConverter(from, to Unit) (PairConverter, error) {
a := Normalize(from).u
b := Normalize(to).u
if len(a.Components) != len(b.Components) {
// Special units are not normalizable so if number of components doesn't match it might be that units are special
specConv, ok := newSpecialConverter(a, b)
if !ok {
return nil, fmt.Errorf("ucum: %q cannot be converted to %q", from.String(), to.String())
}
return specConv, nil
}
for key, expA := range a.Components {
expB, exists := b.Components[key] // normalized units are stripped from annotations, so we can look up directly by key
if !exists {
// Special units are not normalizable so try to interpret it as a special units if mismatched.
specConv, ok := newSpecialConverter(a, b)
if !ok {
return nil, fmt.Errorf("ucum: %q cannot be converted to %q", from.String(), to.String())
}
return specConv, nil
}
if expA != expB {
return nil, fmt.Errorf("ucum: %q cannot be converted to %q", from.String(), to.String())
}
}
ratio := new(big.Rat).Quo(a.Coeff, b.Coeff)
ratioFloat, _ := ratio.Float64()
return &linearConverter{
from: from,
to: to,
ratio: *ratio,
ratioFloat: ratioFloat,
}, nil
}
// newSpecialConverter creates convertor for special units.
// It assumes that the special units are already normalized.
func newSpecialConverter(from, to types.Unit) (PairConverter, bool) {
specialConv := func(u types.Unit) (conv data.SpecialUnitConv, ok bool) {
if len(u.Components) != 1 {
return data.SpecialUnitConv{}, false
}
for key, exp := range u.Components {
if exp != 1 {
// special units cannot be raised to a power
return data.SpecialUnitConv{}, false
}
conv, ok = data.SpecialUnits[key.AtomCode]
return conv, ok
}
return data.SpecialUnitConv{}, false
}
if fromConv, ok := specialConv(from); ok {
interm := MustParse([]byte(fromConv.Unit))
toConv, err := NewPairConverter(interm, Unit{u: to})
if err != nil {
return nil, false
}
return &specialConverter{
multiplyBefore: from.Coeff,
from: fromConv,
to: toConv,
divideAfter: bigRatOne,
}, true
}
if toConv, ok := specialConv(to); ok {
interm := MustParse([]byte(toConv.Unit))
fromConv, err := NewPairConverter(Unit{u: from}, interm)
if err != nil {
return nil, false
}
return &specialConverter{
multiplyBefore: bigRatOne,
from: fromConv,
to: toConv.Invert(),
divideAfter: to.Coeff,
}, true
}
return nil, false
}
func ConvBigInt(from, to Unit, val *big.Int) (result *big.Int, exact bool, err error) {
if from.u.Orig != "" && from.u.Orig == to.u.Orig {
return (&big.Int{}).Set(val), true, nil
}
converter, err := NewPairConverter(from, to)
if err != nil {
return nil, false, err
}
result, exact = converter.ConvBigInt(val)
return result, exact, nil
}