-
-
Notifications
You must be signed in to change notification settings - Fork 510
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
Methods __round__, __trunc__, __floor__, __ceil__ #25827
Comments
New commits:
|
Commit: |
Branch: u/chapoton/25827 |
comment:2
Not sure what to do. This is not curing the disease. It seems that python3 builtin "round" is looking for |
comment:3
failing doctests |
comment:5
I've implemented a few |
comment:6
@embray, where is your latest python3 branch ? could you please provide a branch for here, with the implemented |
comment:7
Ok, I'll try to get back to you about that soon. |
comment:8
I think we need to think about exactly how to implement Sage's I find this a bit odd, but maybe there's a good reason. I wonder, because I find it surprising that I implemented a So two questions:
|
comment:9
So far in sage, almost no round method takes a second argument:
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:11
ok, here is a new branch, just adding some aliases EDIT: still missing would be |
comment:12
I would really rather think about this more carefully, and perhaps rework how Sage's built-in |
comment:13
That's because it's really meant to mean round-to-the-nearest-integer, which is simpler than what Python's built-in round does, which can return a float truncated to N digits, or an integer. Just making There's also now |
comment:14
Those do not appear in the python3 log. |
comment:15
FWIW here's my implementation for MPFR reals: diff --git a/src/sage/rings/real_mpfr.pyx b/src/sage/rings/real_mpfr.pyx
index 9b90c88..a82c4eb 100644
--- a/src/sage/rings/real_mpfr.pyx
+++ b/src/sage/rings/real_mpfr.pyx
@@ -3040,6 +3040,38 @@ cdef class RealNumber(sage.structure.element.RingElement)
"""
return mpfr_get_d(self.value, (<RealField_class>self._parent).rnd)
+ def __round__(self, ndigits=0):
+ """
+ Implement support for Python 3's `round` builtin.
+
+ This is mostly equivalent to simply calling ``round(float(x),
+ ndigits)`` where ``x`` is a `RealNumber`. The difference is that it is
+ still returns an arbitrary-precision `RealNumber` of precision
+ equivalent to ``self`` (or an `Integer` if ``ndigits=0``). It also
+ uses the rounding mode specified on the parent field.
+
+ EXAMPLES::
+
+ TODO
+ """
+ cdef Integer z
+ cdef RealNumber r
+ cdef char* s
+
+ if ndigits < 0:
+ return (<RealField_class>self._parent).zero()
+ elif ndigits == 0:
+ z = PY_NEW(Integer)
+ mpfr_get_z(z.value, self.value, (<RealField_class>self._parent).rnd
+ return z
+ else:
+ rnd = (<RealField_class>self._parent).rnd
+ mpfr_asprintf(&s, "%.*R*f", <int>ndigits, rnd, self.value)
+ r = self._new()
+ mpfr_set_str(r.value, s, 10, rnd)
+ mpfr_free_str(s)
+ return r
+ But I'm not 100% sure if it makes sense to do things this way or not (and it needs examples). However, it's broken with Sage's If anything |
comment:16
Perhaps it is a question that needs to be brought up on sage-devel, if we don't know the answers. For example, why does Sage have its own |
comment:17
I have made #26412 for the same thing in integer.pyx |
comment:38
Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity. |
comment:40
Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date. |
comment:41
Setting a new milestone for this ticket based on a cursory review. |
https://docs.python.org/3/reference/datamodel.html?highlight=__round__#object.__round__
We should define these special methods so that the built-in
round
andmath.trunc
,math.floor
,math.ceil
can operate on Sage numbers.This can help to eliminate imports of
floor
andceil
fromsage.functions
throughout the Sage library, which pulls in all ofsage.symbolic
.Related: In the global namespace, we have:
round
=sage.misc.functional.round
,floor
=sage.functions.other.floor
,ceil
=sage.functions.other.ceil
,trunc
- undefinedCC: @jdemeyer @tscrim
Component: python3
Author: Frédéric Chapoton
Branch/Commit: public/ticket/25827 @
53280c5
Issue created by migration from https://trac.sagemath.org/ticket/25827
The text was updated successfully, but these errors were encountered: