-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprecision_miltiplier.go
More file actions
76 lines (70 loc) · 1.95 KB
/
precision_miltiplier.go
File metadata and controls
76 lines (70 loc) · 1.95 KB
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
package dec
import (
"math/big"
"sync"
)
var (
zMultiplier, _ = (&big.Int{}).SetString("1", BASE)
deciMultiplier, _ = (&big.Int{}).SetString("10", BASE)
centiMultiplier, _ = (&big.Int{}).SetString("100", BASE)
milliMultiplier, _ = (&big.Int{}).SetString("1000", BASE)
microMultiplier, _ = (&big.Int{}).SetString("1000000", BASE)
nanoMultiplier, _ = (&big.Int{}).SetString("1000000000", BASE)
picoMultiplier, _ = (&big.Int{}).SetString("1000000000000", BASE)
femtoMultiplier, _ = (&big.Int{}).SetString("1000000000000000", BASE)
attoMultiplier, _ = (&big.Int{}).SetString("1000000000000000000", BASE)
zeptoMultiplier, _ = (&big.Int{}).SetString("1000000000000000000000", BASE)
yoctoMultiplier, _ = (&big.Int{}).SetString("1000000000000000000000000", BASE)
rontoMultiplier, _ = (&big.Int{}).SetString("1000000000000000000000000000", BASE)
quectoMultiplier, _ = (&big.Int{}).SetString("1000000000000000000000000000000", BASE)
multiplierCache = struct {
m map[Precision]*big.Int
l *sync.RWMutex
}{
m: make(map[Precision]*big.Int),
l: &sync.RWMutex{},
}
)
func (p Precision) multiplierOnlyForReadIPromise() *big.Int {
switch p {
case Z:
return zMultiplier
case Deci:
return deciMultiplier
case Centi:
return centiMultiplier
case Milli:
return milliMultiplier
case Micro:
return microMultiplier
case Nano:
return nanoMultiplier
case Pico:
return picoMultiplier
case Femto:
return femtoMultiplier
case Atto:
return attoMultiplier
case Zepto:
return zeptoMultiplier
case Yocto:
return yoctoMultiplier
case Ronto:
return rontoMultiplier
case Quecto:
return quectoMultiplier
}
multiplierCache.l.RLock()
if value, ok := multiplierCache.m[p]; ok {
multiplierCache.l.RUnlock()
return value
}
multiplierCache.l.RUnlock()
value := &big.Int{}
value.SetUint64(BASE)
value.Exp(value, big.NewInt(int64(p)), nil)
multiplierCache.l.Lock()
multiplierCache.m[p] = value
multiplierCache.l.Unlock()
return value
}