-
Notifications
You must be signed in to change notification settings - Fork 2
/
unit.py
63 lines (48 loc) · 1.69 KB
/
unit.py
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
#!/usr/bin/env python3
"""
Unit conversion module.
.. This file is a part of MAHOS project, which is released under the 3-Clause BSD license.
.. See included LICENSE file or https://github.com/ToyotaCRDL/mahos/blob/main/LICENSE for details.
The SI_scale function is taken from the pyqtgraph project (https://github.com/pyqtgraph/pyqtgraph),
which is licensed under the MIT License:
Copyright 2012 Luke Campagnola, University of North Carolina at Chapel Hill.
The function is redefined here because we sometimes want to avoid importing pyqtgraph
(from non-GUI codes) only for this function.
"""
from __future__ import annotations
import math
import decimal
SI_PREFIXES = "yzafpnµm kMGTPEZY"
SI_PREFIXES_ASCII = "yzafpnum kMGTPEZY"
def _clip(val, vmin, vmax):
return vmin if val < vmin else vmax if val > vmax else val
def SI_scale(x: float | int | decimal.Decimal, min_val=1e-25, use_unicode=True):
"""
Return the recommended scale factor and SI prefix string for x.
Example::
siScale(0.0001) # returns (1e6, 'μ')
# This indicates that the number 0.0001 is best represented as 0.0001 * 1e6 = 100 μUnits
"""
if isinstance(x, decimal.Decimal):
x = float(x)
try:
if not math.isfinite(x):
return (1, "")
except Exception:
raise
if abs(x) < min_val:
m = 0
else:
m = int(_clip(math.floor(math.log(abs(x)) / math.log(1000)), -9.0, 9.0))
if m == 0:
pref = ""
elif m < -8 or m > 8:
pref = "e%d" % (m * 3)
else:
if use_unicode:
pref = SI_PREFIXES[m + 8]
else:
pref = SI_PREFIXES_ASCII[m + 8]
m1 = -3 * m
p = 10.0**m1
return (p, pref)