Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish changing Rational's round method default rounding to even #39486

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
10 changes: 4 additions & 6 deletions src/sage/rings/number_field/number_field_element_quadratic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2382,8 +2382,6 @@ cdef class NumberFieldElement_quadratic(NumberFieldElement_absolute):

TESTS::

sage: import warnings
sage: warnings.filterwarnings("ignore", category=DeprecationWarning)
sage: K2.<sqrt2> = QuadraticField(2)
sage: K3.<sqrt3> = QuadraticField(3)
sage: K5.<sqrt5> = QuadraticField(5)
Expand All @@ -2398,15 +2396,15 @@ cdef class NumberFieldElement_quadratic(NumberFieldElement_absolute):
....: assert round(a+b*sqrt(5.)) == round(a+b*sqrt5), (a, b)
"""
n = self.floor()
test = 2 * (self - n).abs()
test = 2 * (self - n)
if test < 1:
return n
elif test > 1:
return n + 1
elif self > 0:
return n + 1
else:
elif n % 2 == 0:
return n
else:
return n + 1


cdef class NumberFieldElement_quadratic_sqrt(NumberFieldElement_quadratic):
Expand Down
18 changes: 4 additions & 14 deletions src/sage/rings/rational.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3375,11 +3375,9 @@ cdef class Rational(sage.structure.element.FieldElement):
mpz_tdiv_q(n.value, mpq_numref(self.value), mpq_denref(self.value))
return n

def round(Rational self, mode=None):
def round(Rational self, mode="even"):
"""
Return the nearest integer to ``self``, rounding away by default.
Deprecation: in the future the default will be changed to rounding to
even, for consistency with the builtin Python :func:`round`.
Return the nearest integer to ``self``, rounding to even by default.

INPUT:

Expand All @@ -3399,15 +3397,13 @@ cdef class Rational(sage.structure.element.FieldElement):
EXAMPLES::

sage: (9/2).round()
doctest:...: DeprecationWarning: the default rounding for rationals, currently `away`, will be changed to `even`.
See https://github.com/sagemath/sage/issues/35473 for details.
5
4
sage: n = 4/3; n.round()
1
sage: n = -17/4; n.round()
-4
sage: n = -5/2; n.round()
-3
-2
sage: n.round("away")
-3
sage: n.round("up")
Expand All @@ -3419,12 +3415,6 @@ cdef class Rational(sage.structure.element.FieldElement):
sage: n.round("odd")
-3
"""
if mode is None:
if self.denominator() == 2:
from sage.misc.superseded import deprecation
deprecation(35473,
"the default rounding for rationals, currently `away`, will be changed to `even`.")
mode = "away"
if not (mode in ['toward', 'away', 'up', 'down', 'even', 'odd']):
raise ValueError("rounding mode must be one of 'toward', 'away', 'up', 'down', 'even', or 'odd'")
if self.denominator() == 1:
Expand Down
Loading