Skip to content

Commit 18fce6b

Browse files
Merge pull request #339 from oscarbenjamin/pr_fmpq_float
feat: add float/complex conversion for fmpq
2 parents e8e716b + 45288eb commit 18fce6b

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

bin/build_dependencies_unix.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,15 @@ else
273273
echo --------------------------------------------
274274
echo
275275

276-
curl -O https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFRVER.tar.gz
276+
if [ $USE_GMP_GITHUB_MIRROR = "yes" ]; then
277+
if [ ! -d "gmp_mirror" ] ; then
278+
git clone https://github.com/oscarbenjamin/gmp_mirror.git
279+
fi
280+
cp gmp_mirror/mpfr-$MPFRVER.tar.gz .
281+
else
282+
curl -O https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFRVER.tar.gz
283+
fi
284+
277285
tar xf mpfr-$MPFRVER.tar.gz
278286
cd mpfr-$MPFRVER
279287
./configure --prefix=$PREFIX\

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ package = "flint"
8787
"spin.cmds.meson.run",
8888
]
8989

90+
[tool.pytest.ini_options]
91+
addopts = "--import-mode=importlib"
92+
9093
[tool.cibuildwheel]
9194
skip = "*-win32 *-manylinux_i686 *-manylinux_armv7l *-musllinux_*"
9295

src/flint/test/test_all.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ def test_fmpq() -> None:
888888
assert Q(-5,3).ceil() == flint.fmpz(-1)
889889

890890
assert type(int(Q(5,3))) is int
891+
assert type(float(Q(5,3))) is float
892+
assert type(complex(Q(5,3))) is complex
891893
assert type(math.floor(Q(5,3))) is flint.fmpz
892894
assert type(math.ceil(Q(5,3))) is flint.fmpz
893895
assert type(math.trunc(Q(5,3))) is flint.fmpz
@@ -897,6 +899,8 @@ def test_fmpq() -> None:
897899
assert type(round(Q(5,3), 1)) is flint.fmpq
898900

899901
assert int(Q(5,3)) == 1
902+
assert float(Q(5,3)) == 5/3
903+
assert complex(Q(5,3)) == 5/3 + 0j
900904
assert math.floor(Q(5,3)) == flint.fmpz(1)
901905
assert math.ceil(Q(5,3)) == flint.fmpz(2)
902906
assert math.trunc(Q(5,3)) == flint.fmpz(1)

src/flint/types/fmpq.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class fmpq(flint_scalar):
4040
def __repr__(self) -> _str: ...
4141

4242
def __int__(self) -> int: ...
43+
def __float__(self) -> float: ...
4344

4445
def __floor__(self) -> fmpz: ...
4546
def __ceil__(self) -> fmpz: ...

src/flint/types/fmpq.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flint.flint_base.flint_base cimport flint_scalar
22
from flint.utils.typecheck cimport typecheck
33
from flint.types.fmpz cimport fmpz_set_any_ref
4+
from flint.types.fmpz cimport fmpz_get_intlong
45
from flint.types.fmpz cimport fmpz
56
from flint.types.fmpz cimport any_as_fmpz
67

@@ -199,6 +200,11 @@ cdef class fmpq(flint_scalar):
199200
def __int__(self):
200201
return int(self.trunc())
201202

203+
def __float__(self) -> float:
204+
n = fmpz_get_intlong(fmpq_numref(self.val))
205+
d = fmpz_get_intlong(fmpq_denref(self.val))
206+
return n / d
207+
202208
def __floor__(self):
203209
return self.floor()
204210

0 commit comments

Comments
 (0)