Opened 13 months ago

Last modified 12 months ago

#27788 new enhancement

Make Sage reals and rationals more friendly to Python string formatting

Reported by: slelievre Owned by:
Priority: major Milestone:
Component: numerical Keywords: format
Cc: slelievre Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

Let us make Sage reals and rationals more friendly to formatting commands that expect Python floats.

For example, with a Python float one can do

sage: a = 1.25r
sage: "{:0.3f}".format(a)
'1.250'

but a similar command fails for a Sage real or rational:

sage: a = 1.25
sage: "{:0.3f}".format(a)
Traceback (most recent call last)
...
TypeError: unsupported format string passed to
sage.rings.real_mpfr.RealLiteral.__format__
sage: a = 5/4
sage: "{:0.3f}".format(a)
Traceback (most recent call last)
...
TypeError: unsupported format string passed to
sage.rings.rational.Rational.__format__

This puzzles users especially when using Python packages with Sage. As things stand one needs to know to work around Sage's preparser. Most recently this came up in

The case of Sage integers was dealt with in #12788 and can provide inspiration.

We could also consider doing the same for number field elements, algebraic numbers, complex floating-point numbers, continued fractions... and other numbers in Sage for which it makes sense.

Change History (1)

comment:1 Changed 12 months ago by embray

  • Milestone sage-8.8 deleted

As the Sage-8.8 release milestone is pending, we should delete the sage-8.8 milestone for tickets that are not actively being worked on or that still require significant work to move forward. If you feel that this ticket should be included in the next Sage release at the soonest please set its milestone to the next release milestone (sage-8.9).

Note: See TracTickets for help on using tickets.