#30515 closed enhancement (fixed)
implement string formatting for elements in RR and CC
Reported by: | gh-mwageringel | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | sage-9.2 |
Component: | misc | Keywords: | format |
Cc: | slelievre, gh-jcamp0x2a | Merged in: | |
Authors: | Markus Wageringel | Reviewers: | Dima Pasechnik |
Report Upstream: | N/A | Work issues: | |
Branch: | c118bf2 (Commits, GitHub, GitLab) | Commit: | |
Dependencies: | Stopgaps: |
Description
Following #29514 (see also #27788 and #29212), this ticket implements __format__
for real and complex numbers.
sage: '{:.4f}'.format(1.2) '1.2000' sage: '{:.4f}'.format(CC(1.2, 0)) '1.2000 + 0.0000*I'
The complex case is implemented by reusing the code from CDF
.
The real case is implemented by delegating to Python's arbitrary precision Decimal
numbers. This works quite well, but has the caveat that the output is not completely the same as for the repr
of real numbers. Compare for example:
sage: from decimal import Decimal sage: '{!r} {!s}'.format(RR(oo), Decimal(float(oo))) '+infinity Infinity' sage: '{!r} {!s}'.format(RR(-oo), Decimal(float(-oo))) '-infinity -Infinity' sage: '{!r} {!s}'.format(RR(0), Decimal(str(RR(0)))) '0.00000000000000 0E-15' sage: '{!r} {:.10}'.format(RR(1e10), Decimal(float(1e10))) '1.00000000000000e10 1.000000000E+10'
It looks like this can only be avoided by a custom implementation of the format specification, rather than delegating to Decimal
, which is quite a complex task.
Change History (6)
comment:1 in reply to: ↑ description Changed 10 months ago by
- Branch set to u/gh-mwageringel/30515
- Commit set to c118bf242794aaa65767d201d7044d5ce765f4ea
- Keywords format added
- Status changed from new to needs_review
comment:2 Changed 9 months ago by
- Reviewers set to Dima Pasechnik
- Status changed from needs_review to positive_review
lgtm
comment:3 Changed 9 months ago by
Thanks.
comment:4 Changed 9 months ago by
- Branch changed from u/gh-mwageringel/30515 to c118bf242794aaa65767d201d7044d5ce765f4ea
- Resolution set to fixed
- Status changed from positive_review to closed
comment:5 Changed 9 months ago by
- Commit c118bf242794aaa65767d201d7044d5ce765f4ea deleted
I'm getting two test failures on my distro package, using system python 3.8.5
sage -t --long --random-seed=0 /usr/lib/python3.8/site-packages/sage/rings/complex_number.pyx ********************************************************************** File "/usr/lib/python3.8/site-packages/sage/rings/complex_number.pyx", line 574, in sage.rings.complex_number.ComplexNumber.__format__ Failed example: format(CC(3, 0), '#.4g') Expected: Traceback (most recent call last): ... ValueError: invalid format string Got: '3.000 + 0.e-15*I' ********************************************************************** File "/usr/lib/python3.8/site-packages/sage/rings/complex_number.pyx", line 578, in sage.rings.complex_number.ComplexNumber.__format__ Failed example: format(CC(0, 0), '+#.4') Expected: Traceback (most recent call last): ... ValueError: invalid format string Got: '+0.E-15' **********************************************************************
comment:6 Changed 9 months ago by
#30689 should fix this. It is nice to see that the format specification seems to be more fully implemented in your Python version. On my end, I cannot reproduce it with 3.8.5 though.
Replying to gh-mwageringel:
As those differences are small, I would suggest to leave that for a follow-up ticket and merge this ticket as is.
New commits:
30515: implement formatting for elements of RR and CC