Skip to content

Commit eef6490

Browse files
committed
Fix arb powers with integer exponents
1 parent 848e579 commit eef6490

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/flint/test/test_arb.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import math
66

7-
from flint import arb, ctx
7+
from flint import arb, ctx, fmpz
88
from flint.test.helpers import is_close_arb, raises
99

1010
def assert_almost_equal(x: float | int, y: float | int, places: int = 7) -> None:
@@ -295,6 +295,13 @@ def test_arb_arithmetic() -> None:
295295
assert (x ** 2) == arb(4)
296296
assert (2 ** x) == arb(4)
297297

298+
interval = arb(0, 1)
299+
for exponent in (1, 2, fmpz(2)):
300+
power = interval ** exponent
301+
assert power.is_finite()
302+
assert power.contains(0)
303+
assert power.contains(1)
304+
298305
assert raises(lambda: x + "bad", TypeError) # type: ignore[operator]
299306
assert raises(lambda: "bad" + x, TypeError) # type: ignore[operator]
300307
assert raises(lambda: x ** "bad", TypeError) # type: ignore[operator]

src/flint/types/arb.pyx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from flint.flint_base.flint_context cimport thectx
66
from flint.flint_base.flint_base cimport flint_scalar
77
from flint.utils.typecheck cimport typecheck
88
from flint.utils.conversion cimport chars_from_str, str_from_chars
9-
from flint.types.fmpz cimport fmpz_set_pylong
9+
from flint.types.fmpz cimport fmpz_set_any_ref, fmpz_set_pylong
1010
from flint.types.arf cimport arf
1111
from flint.types.fmpq cimport fmpq
1212
from flint.types.fmpz cimport fmpz
@@ -695,9 +695,18 @@ cdef class arb(flint_scalar):
695695

696696
def __pow__(s, t, modulus):
697697
cdef arb_struct tval[1]
698+
cdef fmpz_struct exponent[1]
698699
cdef int ttype
700+
cdef int exponent_type
699701
if modulus is not None:
700702
raise TypeError("three-argument pow() not supported by arb type")
703+
exponent_type = fmpz_set_any_ref(exponent, t)
704+
if exponent_type != FMPZ_UNKNOWN:
705+
u = arb.__new__(arb)
706+
arb_pow_fmpz((<arb>u).val, (<arb>s).val, exponent, getprec())
707+
if exponent_type == FMPZ_TMP:
708+
fmpz_clear(exponent)
709+
return u
701710
ttype = arb_set_any_ref(tval, t)
702711
if ttype == FMPZ_UNKNOWN:
703712
return NotImplemented

0 commit comments

Comments
 (0)