New SI prefixes added - Ronna, Quetta - Yeah! (#570)

pull/572/head
Holger Müller 2022-11-24 16:42:52 +01:00 zatwierdzone przez GitHub
rodzic 9c5b1e01ea
commit fb50f4a01b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 22 dodań i 18 usunięć

Wyświetl plik

@ -22,8 +22,8 @@ from decimal import Context, Decimal, InvalidOperation
from typing import NamedTuple
from numbers import Number, Real
PREFIXES = ("y", "z", "a", "f", "p", "n", "µ", "m",
"", "k", "M", "G", "T", "P", "E", "Z", "Y")
PREFIXES = ("q", "r", "y", "z", "a", "f", "p", "n", "µ", "m",
"", "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q")
def clamp_value(value: Real, rmin: Real, rmax: Real) -> Real:
@ -56,8 +56,8 @@ class Format(NamedTuple):
fix_decimals: bool = False
space_str: str = ""
assume_infinity: bool = True
min_offset: int = -8
max_offset: int = 8
min_offset: int = -10
max_offset: int = 10
allow_strip: bool = False
allways_signed: bool = False
printable_min: float = -math.inf
@ -71,11 +71,11 @@ class Format(NamedTuple):
class Value:
CTX = Context(prec=60, Emin=-27, Emax=27)
CTX = Context(prec=60, Emin=-33, Emax=33)
def __init__(self, value: Real = Decimal(0), unit: str = "", fmt=Format()):
assert 1 <= fmt.max_nr_digits <= 30
assert -8 <= fmt.min_offset <= fmt.max_offset <= 8
assert -10 <= fmt.min_offset <= fmt.max_offset <= 10
assert fmt.parse_clamp_min < fmt.parse_clamp_max
assert fmt.printable_min < fmt.printable_max
self._unit = unit
@ -128,7 +128,7 @@ class Value:
if self.fmt.allow_strip and "." in result:
result = result.rstrip("0").rstrip(".")
return result + fmt.space_str + PREFIXES[offset + 8] + self._unit
return result + fmt.space_str + PREFIXES[offset + 10] + self._unit
def __int__(self):
return round(self._value)
@ -163,7 +163,7 @@ class Value:
if self.fmt.parse_sloppy_kilo and value[-1] in ("K", "m", "g"):
value = value[:-1] + value[-1].swapcase()
if value[-1] in PREFIXES:
factor = 10 ** ((PREFIXES.index(value[-1]) - 8) * 3)
factor = 10 ** ((PREFIXES.index(value[-1]) - 10) * 3)
value = value[:-1]
if self.fmt.assume_infinity and value == "\N{INFINITY}":

Wyświetl plik

@ -29,9 +29,9 @@ F_DEFAULT = Format()
F_ASSERT_DIGITS_0 = Format(max_nr_digits=0)
F_ASSERT_DIGITS_2 = Format(max_nr_digits=31)
F_ASSERT_OFFSET_1 = Format(min_offset=-9)
F_ASSERT_OFFSET_2 = Format(max_offset=9)
F_ASSERT_OFFSET_3 = Format(min_offset=9)
F_ASSERT_OFFSET_1 = Format(min_offset=-11)
F_ASSERT_OFFSET_2 = Format(max_offset=11)
F_ASSERT_OFFSET_3 = Format(min_offset=11)
F_ASSERT_CLAMP = Format(parse_clamp_min=10, parse_clamp_max=9)
F_DIGITS_1 = Format(max_nr_digits=1, min_offset=-2,
@ -81,8 +81,10 @@ class TestSIToolsValue(unittest.TestCase):
self.assertEqual(str(Value(1e18)), "1.00000E")
self.assertEqual(str(Value(1e21)), "1.00000Z")
self.assertEqual(str(Value(1e24)), "1.00000Y")
self.assertEqual(str(Value(1e27)), "\N{INFINITY}")
self.assertEqual(str(Value(-1e27)), "-\N{INFINITY}")
self.assertEqual(str(Value(1e27)), "1.00000R")
self.assertEqual(str(Value(1e30)), "1.00000Q")
self.assertEqual(str(Value(1e34)), "\N{INFINITY}")
self.assertEqual(str(Value(-1e34)), "-\N{INFINITY}")
self.assertEqual(str(Value(nan)), "-")
self.assertEqual(float(Value(1e27)), 1e27)
self.assertEqual(
@ -100,11 +102,13 @@ class TestSIToolsValue(unittest.TestCase):
self.assertEqual(str(Value(1e-18)), "1.00000a")
self.assertEqual(str(Value(1e-21)), "1.00000z")
self.assertEqual(str(Value(1e-24)), "1.00000y")
self.assertEqual(str(Value(1e-27)), "0.00100y")
self.assertEqual(str(Value(1e-29)), "0.00001y")
self.assertEqual(str(Value(-1e-29)), "-0.00001y")
self.assertEqual(str(Value(1e-30)), "0.00000")
self.assertEqual(float(Value(1e-30)), 1e-30)
self.assertEqual(str(Value(1e-27)), "1.00000r")
self.assertEqual(str(Value(1e-30)), "1.00000q")
self.assertEqual(str(Value(1e-33)), "0.00100q")
self.assertEqual(str(Value(1e-35)), "0.00001q")
self.assertEqual(str(Value(-1e-35)), "-0.00001q")
self.assertEqual(str(Value(1e-36)), "0.00000")
self.assertEqual(float(Value(1e-36)), 1e-36)
def test_format_digits_1(self):
v = Value(fmt=F_DIGITS_1)