diff --git a/src/flint/test/test_arb.py b/src/flint/test/test_arb.py index dfabe06e..fe887fd6 100644 --- a/src/flint/test/test_arb.py +++ b/src/flint/test/test_arb.py @@ -4,7 +4,7 @@ import math -from flint import arb, ctx +from flint import arb, ctx, fmpz from flint.test.helpers import is_close_arb, raises def assert_almost_equal(x: float | int, y: float | int, places: int = 7) -> None: @@ -295,6 +295,13 @@ def test_arb_arithmetic() -> None: assert (x ** 2) == arb(4) assert (2 ** x) == arb(4) + interval = arb(0, 1) + for exponent in (1, 2, fmpz(2)): + power = interval ** exponent + assert power.is_finite() + assert power.contains(0) + assert power.contains(1) + assert raises(lambda: x + "bad", TypeError) # type: ignore[operator] assert raises(lambda: "bad" + x, TypeError) # type: ignore[operator] assert raises(lambda: x ** "bad", TypeError) # type: ignore[operator] diff --git a/src/flint/types/arb.pyx b/src/flint/types/arb.pyx index 56b9ba07..05f3bc33 100644 --- a/src/flint/types/arb.pyx +++ b/src/flint/types/arb.pyx @@ -6,7 +6,7 @@ from flint.flint_base.flint_context cimport thectx from flint.flint_base.flint_base cimport flint_scalar from flint.utils.typecheck cimport typecheck from flint.utils.conversion cimport chars_from_str, str_from_chars -from flint.types.fmpz cimport fmpz_set_pylong +from flint.types.fmpz cimport fmpz_set_any_ref, fmpz_set_pylong from flint.types.arf cimport arf from flint.types.fmpq cimport fmpq from flint.types.fmpz cimport fmpz @@ -695,9 +695,18 @@ cdef class arb(flint_scalar): def __pow__(s, t, modulus): cdef arb_struct tval[1] + cdef fmpz_struct exponent[1] cdef int ttype + cdef int exponent_type if modulus is not None: raise TypeError("three-argument pow() not supported by arb type") + exponent_type = fmpz_set_any_ref(exponent, t) + if exponent_type != FMPZ_UNKNOWN: + u = arb.__new__(arb) + arb_pow_fmpz((u).val, (s).val, exponent, getprec()) + if exponent_type == FMPZ_TMP: + fmpz_clear(exponent) + return u ttype = arb_set_any_ref(tval, t) if ttype == FMPZ_UNKNOWN: return NotImplemented