Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ def test_fmpq():
assert Q(-2,-4) == Q(1,2)
assert Q("1") == Q(1)
assert Q("1/2") == Q(1,2)
assert Q(Q(1,2),Q(3,4)) == Q(2,3)
assert Q(1,Q(2,3)) == Q(3,2)
assert Q(Q(1,2),3) == Q(1,6)
assert Q(flint.fmpz(1),Q(2,3)) == Q(3,2)
assert Q(Q(1,2),flint.fmpz(3)) == Q(1,6)
assert raises(lambda: Q("1.0"), ValueError)
assert raises(lambda: Q("1.5"), ValueError)
assert raises(lambda: Q("1/2/3"), ValueError)
Expand Down
2 changes: 1 addition & 1 deletion src/flint/types/fmpq.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class fmpq(flint_scalar):
@overload
def __init__(self, arg: ifmpq | _str, /) -> None: ...
@overload
def __init__(self, num: ifmpz, den: ifmpz, /) -> None: ...
def __init__(self, num: ifmpq, den: ifmpq, /): ...

@property
def p(self) -> fmpz: ...
Expand Down
21 changes: 19 additions & 2 deletions src/flint/types/fmpq.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,27 @@ cdef class fmpq(flint_scalar):

p2 = any_as_fmpz(p)
if p2 is NotImplemented:
raise TypeError("cannot create fmpq from object of type %s" % type(p))
# Allow fmpq(fmpq, fmpq/fmpz)
p2 = any_as_fmpq(p)
if p2 is NotImplemented:
raise TypeError("cannot create fmpz/fmpq from object of type %s" % type(p))
q2 = any_as_fmpq(q)
if q2 is NotImplemented:
raise TypeError("cannot create fmpz/fmpq from object of type %s" % type(q))
p3 = p2 / q2
fmpq_set(self.val, (<fmpq>p3).val)
return

q2 = any_as_fmpz(q)
if q2 is NotImplemented:
raise TypeError("cannot create fmpq from object of type %s" % type(q))
# fmpq(fmpz, fmpq)
q2 = any_as_fmpq(q)
if q2 is NotImplemented:
raise TypeError("cannot create fmpz/fmpq from object of type %s" % type(q))
p3 = p2 / q2
fmpq_set(self.val, (<fmpq>p3).val)
return

if fmpz_is_zero((<fmpz>q2).val):
raise ZeroDivisionError("cannot create rational number with zero denominator")

Expand Down
Loading