From 8a304aedcf024fc8b97dd0399582bff1bbe61a5c Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 2 Jan 2021 23:33:47 +1100 Subject: [PATCH] Fix/tweak numbers implementation Round fixed point decimals in initialiser to match Python behaviour Add pretty printing for Python --- pyRCV2/numbers/fixed_js.py | 2 +- pyRCV2/numbers/fixed_py.py | 2 ++ pyRCV2/numbers/native_py.py | 2 ++ pyRCV2/numbers/rational_py.py | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pyRCV2/numbers/fixed_js.py b/pyRCV2/numbers/fixed_js.py index 7115022..c81b438 100644 --- a/pyRCV2/numbers/fixed_js.py +++ b/pyRCV2/numbers/fixed_js.py @@ -28,7 +28,7 @@ class Fixed: if isinstance(val, Fixed): self.impl = val.impl else: - self.impl = Big(val) + self.impl = Big(val).round(Big.DP) def pp(self, dp): """Pretty print to specified number of decimal places""" diff --git a/pyRCV2/numbers/fixed_py.py b/pyRCV2/numbers/fixed_py.py index e92b72e..5c28ae3 100644 --- a/pyRCV2/numbers/fixed_py.py +++ b/pyRCV2/numbers/fixed_py.py @@ -34,6 +34,8 @@ class Fixed: else: self.impl = Decimal(val).quantize(_quantize_exp) + def __repr__(self): + return ''.format(str(self.impl)) def pp(self, dp): """Pretty print to specified number of decimal places""" return format(self.impl, '.{}f'.format(dp)) diff --git a/pyRCV2/numbers/native_py.py b/pyRCV2/numbers/native_py.py index b4ee3a2..cdbb764 100644 --- a/pyRCV2/numbers/native_py.py +++ b/pyRCV2/numbers/native_py.py @@ -27,6 +27,8 @@ class Native: else: self.impl = float(val) + def __repr__(self): + return ''.format(str(self.impl)) def pp(self, dp): """Pretty print to specified number of decimal places""" return format(self.impl, '.{}f'.format(dp)) diff --git a/pyRCV2/numbers/rational_py.py b/pyRCV2/numbers/rational_py.py index 4720c3f..862915d 100644 --- a/pyRCV2/numbers/rational_py.py +++ b/pyRCV2/numbers/rational_py.py @@ -28,6 +28,8 @@ class Rational: else: self.impl = Fraction(val) + def __repr__(self): + return ''.format(str(self.impl)) def pp(self, dp): """ Pretty print to specified number of decimal places