Skip to content

Commit 20279e5

Browse files
author
Release Manager
committed
sagemathgh-39486: Finish changing Rational's round method default rounding to even Follow-up to sagemath#35756 . (As is, the warning cannot be suppressed) Related: sagemath#37306, sagemath#21935 There's a little (large??) problem: previously `K2.<sqrt2> = QuadraticField(2); K2(9/2).round()` returns `5` **without** a warning (because sagemath#35756 forget to print the deprecation). Should we add the deprecation first **then** change this next year? ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39486 Reported by: user202729 Reviewer(s): Vincent Neiger
2 parents 07c5d22 + 774b89d commit 20279e5

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

src/sage/rings/number_field/number_field_element_quadratic.pyx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,8 +2382,6 @@ cdef class NumberFieldElement_quadratic(NumberFieldElement_absolute):
23822382
23832383
TESTS::
23842384
2385-
sage: import warnings
2386-
sage: warnings.filterwarnings("ignore", category=DeprecationWarning)
23872385
sage: K2.<sqrt2> = QuadraticField(2)
23882386
sage: K3.<sqrt3> = QuadraticField(3)
23892387
sage: K5.<sqrt5> = QuadraticField(5)
@@ -2398,15 +2396,15 @@ cdef class NumberFieldElement_quadratic(NumberFieldElement_absolute):
23982396
....: assert round(a+b*sqrt(5.)) == round(a+b*sqrt5), (a, b)
23992397
"""
24002398
n = self.floor()
2401-
test = 2 * (self - n).abs()
2399+
test = 2 * (self - n)
24022400
if test < 1:
24032401
return n
24042402
elif test > 1:
24052403
return n + 1
2406-
elif self > 0:
2407-
return n + 1
2408-
else:
2404+
elif n % 2 == 0:
24092405
return n
2406+
else:
2407+
return n + 1
24102408

24112409

24122410
cdef class NumberFieldElement_quadratic_sqrt(NumberFieldElement_quadratic):

src/sage/rings/rational.pyx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,11 +3375,9 @@ cdef class Rational(sage.structure.element.FieldElement):
33753375
mpz_tdiv_q(n.value, mpq_numref(self.value), mpq_denref(self.value))
33763376
return n
33773377

3378-
def round(Rational self, mode=None):
3378+
def round(Rational self, mode="even"):
33793379
"""
3380-
Return the nearest integer to ``self``, rounding away by default.
3381-
Deprecation: in the future the default will be changed to rounding to
3382-
even, for consistency with the builtin Python :func:`round`.
3380+
Return the nearest integer to ``self``, rounding to even by default.
33833381
33843382
INPUT:
33853383
@@ -3399,15 +3397,13 @@ cdef class Rational(sage.structure.element.FieldElement):
33993397
EXAMPLES::
34003398
34013399
sage: (9/2).round()
3402-
doctest:...: DeprecationWarning: the default rounding for rationals, currently `away`, will be changed to `even`.
3403-
See https://github.com/sagemath/sage/issues/35473 for details.
3404-
5
3400+
4
34053401
sage: n = 4/3; n.round()
34063402
1
34073403
sage: n = -17/4; n.round()
34083404
-4
34093405
sage: n = -5/2; n.round()
3410-
-3
3406+
-2
34113407
sage: n.round("away")
34123408
-3
34133409
sage: n.round("up")
@@ -3419,12 +3415,6 @@ cdef class Rational(sage.structure.element.FieldElement):
34193415
sage: n.round("odd")
34203416
-3
34213417
"""
3422-
if mode is None:
3423-
if self.denominator() == 2:
3424-
from sage.misc.superseded import deprecation
3425-
deprecation(35473,
3426-
"the default rounding for rationals, currently `away`, will be changed to `even`.")
3427-
mode = "away"
34283418
if not (mode in ['toward', 'away', 'up', 'down', 'even', 'odd']):
34293419
raise ValueError("rounding mode must be one of 'toward', 'away', 'up', 'down', 'even', or 'odd'")
34303420
if self.denominator() == 1:

0 commit comments

Comments
 (0)