FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow fmpq(fmpq, fmpq) constructor by oscarbenjamin · Pull Request #292 · flintlib/python-flint · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (1) .pyi  (1) .pyx  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions src/flint/test/test_all.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL