id summary reporter owner description type status priority milestone component resolution keywords cc merged author reviewer upstream work_issues branch commit dependencies stopgaps
27788 Make Sage reals and rationals more friendly to Python string formatting slelievre "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.
For example this came up in
- [https://ask.sagemath.org/question/46408 Ask Sage question 46408: tqdm's ""total"" parameter bug]
- [https://ask.sagemath.org/question/52617 Ask Sage question 52617: f string float not working]
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.
This is part of #29728 (Meta-ticket: improve compatibility with the Python library)." enhancement new major numerical format slelievre gh-jcamp0x2a N/A