From 557474c7dd35a1a666eee816f6c06f84190ba8ba Mon Sep 17 00:00:00 2001 From: GDeLaurentis Date: Sat, 23 Mar 2024 11:58:19 +0000 Subject: [PATCH] Improved instantiation from strings. --- pyadic/finite_field.py | 4 ++-- tests/test_finite_field.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pyadic/finite_field.py b/pyadic/finite_field.py index 7de8b1f..8fd7401 100644 --- a/pyadic/finite_field.py +++ b/pyadic/finite_field.py @@ -68,8 +68,8 @@ def __init__(self, n, p=None): self.p = n.p ** n.k elif p is None and isinstance(n, str): self.n, self.p = self.__rstr__(n) - elif isinstance(n, str) and n.isnumeric(): - self.n, self.p = int(n), p + elif isinstance(n, str) and (n.isnumeric() or n.lstrip("+-").isnumeric()): + self.n, self.p = int(n) % int(p), p else: raise TypeError('Bad finite field constructor, (n, p) of value:({}, {}) and type:({}, {}).'.format(n, p, type(n), type(p))) diff --git a/tests/test_finite_field.py b/tests/test_finite_field.py index 073eff5..3fa4003 100644 --- a/tests/test_finite_field.py +++ b/tests/test_finite_field.py @@ -30,6 +30,8 @@ def test_same_class_instantiation_and_unary_plus(): def test_instantiation_from_padic(): assert ModP(PAdic(123, 2 ** 31 - 19, 3)) == ModP(123, (2 ** 31 - 19) ** 3) +def test_instantiation_from_string(): + assert ModP('+123', 2 ** 31 - 1) == ModP('-2147483524', 2 ** 31 - 1) == ModP(123, 2 ** 31 - 1) def test_instantiation_with_complex(): p = 2 ** 31 - 19