Skip to content

Commit e4079c1

Browse files
committed
Add rounding methods for fmpz and tests
1 parent cba4721 commit e4079c1

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/flint/fmpq.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ cdef class fmpq(flint_scalar):
130130
return "%s/%s" % (self.p.str(**kwargs), self.q.str(**kwargs))
131131

132132
def __int__(self):
133-
if self.p >= 0:
134-
return int(self.p // self.q)
135-
else:
136-
return - int((-self.p) // self.q)
133+
return int(self.trunc())
137134

138135
def __floor__(self):
139136
return self.floor()

src/flint/fmpz.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ cdef class fmpz(flint_scalar):
115115
def __float__(self):
116116
return float(fmpz_get_intlong(self.val))
117117

118+
def __floor__(self):
119+
return self
120+
121+
def __ceil__(self):
122+
return self
123+
124+
def __trunc__(self):
125+
return self
126+
127+
def __round__(self, ndigits=None):
128+
if ndigits is None:
129+
return self
130+
else:
131+
return fmpz(round(int(self), ndigits))
132+
118133
def __richcmp__(s, t, int op):
119134
cdef bint res = 0
120135
cdef long tl

test/test.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ def test_fmpz():
161161
assert type(operator.index(f)) is int
162162
assert float(f) == 2.0
163163
assert type(float(f)) is float
164+
assert round(f) == 2
165+
assert type(round(f)) is flint.fmpz
166+
assert round(f, 1) == 2
167+
assert type(round(f, 1)) is flint.fmpz
168+
assert round(f, -1) == 0
169+
assert type(round(f, -1)) is flint.fmpz
170+
assert math.trunc(f) == 2
171+
assert type(math.trunc(f)) is flint.fmpz
172+
assert math.floor(f) == 2
173+
assert type(math.floor(f)) is flint.fmpz
174+
assert math.ceil(f) == 2
175+
assert type(math.ceil(f)) is flint.fmpz
164176

165177
assert flint.fmpz(2) != []
166178
assert +flint.fmpz(0) == 0
@@ -811,21 +823,27 @@ def test_fmpq():
811823
assert Q(5,3).ceil() == flint.fmpz(2)
812824
assert Q(-5,3).ceil() == flint.fmpz(-1)
813825

814-
assert int(Q(5,3)) == flint.fmpz(1)
826+
assert type(int(Q(5,3))) is int
827+
assert type(math.floor(Q(5,3))) is flint.fmpz
828+
assert type(math.ceil(Q(5,3))) is flint.fmpz
829+
assert type(math.trunc(Q(5,3))) is flint.fmpz
830+
assert type(round(Q(5,3))) is flint.fmpz
831+
assert type(round(Q(5,3))) is flint.fmpz
832+
assert type(round(Q(5,3), 0)) is flint.fmpq
833+
assert type(round(Q(5,3), 1)) is flint.fmpq
834+
835+
assert int(Q(5,3)) == 1
815836
assert math.floor(Q(5,3)) == flint.fmpz(1)
816837
assert math.ceil(Q(5,3)) == flint.fmpz(2)
817838
assert math.trunc(Q(5,3)) == flint.fmpz(1)
818-
assert round(Q(5,3)) == 2
839+
assert round(Q(5,3)) == flint.fmpz(2)
819840

820841
assert int(Q(-5,3)) == flint.fmpz(-1)
821842
assert math.floor(Q(-5,3)) == flint.fmpz(-2)
822843
assert math.ceil(Q(-5,3)) == flint.fmpz(-1)
823844
assert math.trunc(Q(-5,3)) == flint.fmpz(-1)
824845
assert round(Q(-5,3)) == -2
825846

826-
assert type(round(Q(5,3))) is flint.fmpz
827-
assert type(round(Q(5,3), 0)) is flint.fmpq
828-
assert type(round(Q(5,3), 1)) is flint.fmpq
829847
assert round(Q(100,3), 2) == Q(3333,100)
830848
assert round(Q(100,3), 0) == Q(33,1)
831849
assert round(Q(100,3), -1) == Q(30,1)

0 commit comments

Comments
 (0)