If you have any insight to share on the rationale of Sage's existing
built-in round() behavior it would also be very appreciated,
especially for this ticket: https://trac.sagemath.org/ticket/25827
To summarize the discussion thus far:
* Sage currently has its own `round()` function defined in
sage.misc.functional , which in the global namespace of the Sage
interpreter replaces the Python built-in `round()`; the main reason
for this appears to be that it was not possible in the past to
override how `round()` acts on arbitrary types: It would just convert
them to floats first if they have a `__float__()` method, and then
round that value.
* However, Python 3 introduces a `__round__(self, n=0)` special method
that types can implement to be called when `round()` is called on
instances of that type.
* Here, I proposed a new `__round__()` method for MPFR reals that
takes into account the rounding mode of the parent field (e.g. RNDU,
RNDN, etc.) This seems to work, but would be a change in behavior
over the old `.round()` method on that class.
* It's not clear why the old `.round()` method ignored the rounding
mode and whether or not there's a good reason for that. It's also not
clear if we even need to replace the built-in `round()` on Python 3 if
types can implement `__round__()`. These are two separate questions
though, at least I think.
On Thu, Jan 31, 2019 at 8:42 AM David Coudert wrote:
>
> Does anyone know a suitable way to fix the following issue with round in
> Python3 ?
>
> sage: G = Graph([(0, 1)])
> sage: f = g._build_flow_graph({(0, 1): 1}, True)
> ---
> TypeError Traceback (most recent call last)
> in ()
> > 1 f = g._build_flow_graph({(Integer(0), Integer(1)): Integer(1)}, True)
>
> /Users/dcoudert/sage3/sage/local/lib/python3.6/site-packages/sage/graphs/generic_graph.py
> in _build_flow_graph(self, flow, integer)
>9094 g.delete_edge(u, v)
>9095 else:
> -> 9096 g.set_edge_label(u, v, int(round(l)))
>9097
>9098 # returning a graph with the same embedding, the
> corresponding name, etc ...
>
> TypeError: type sage.rings.integer.Integer doesn't define __round__ method
> sage:
> sage: f = g._build_flow_graph({(0, 1): 1.0}, True)
> ---
> TypeError Traceback (most recent call last)
> in ()
> > 1 f = g._build_flow_graph({(Integer(0), Integer(1)):
> RealNumber('1.0')}, True)
>
> /Users/dcoudert/sage3/sage/local/lib/python3.6/site-packages/sage/graphs/generic_graph.py
> in _build_flow_graph(self, flow, integer)
>9094 g.delete_edge(u, v)
>9095 else:
> -> 9096 g.set_edge_label(u, v, int(round(l)))
>9097
>9098 # returning a graph with the same embedding, the
> corresponding name, etc ...
>
> TypeError: type sage.rings.real_mpfr.RealLiteral doesn't define __round__
> method
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-devel@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.