Skip to content
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

implement string formatting for elements in RR and CC #30515

Closed
mwageringel opened this issue Sep 6, 2020 · 12 comments
Closed

implement string formatting for elements in RR and CC #30515

mwageringel opened this issue Sep 6, 2020 · 12 comments

Comments

@mwageringel
Copy link

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.

CC: @slel @jcamp0x2a

Component: misc

Keywords: format

Author: Markus Wageringel

Branch: c118bf2

Reviewer: Dima Pasechnik

Issue created by migration from https://trac.sagemath.org/ticket/30515

@mwageringel mwageringel added this to the sage-9.2 milestone Sep 6, 2020
@mwageringel
Copy link
Author

Commit: c118bf2

@mwageringel
Copy link
Author

Author: Markus Wageringel

@mwageringel
Copy link
Author

Replying to @mwageringel:

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.

As those differences are small, I would suggest to leave that for a follow-up ticket and merge this ticket as is.


New commits:

c118bf230515: implement formatting for elements of RR and CC

@mwageringel
Copy link
Author

Branch: u/gh-mwageringel/30515

@mwageringel
Copy link
Author

Changed keywords from none to format

@dimpase
Copy link
Member

dimpase commented Sep 8, 2020

Reviewer: Dima Pasechnik

@dimpase
Copy link
Member

dimpase commented Sep 8, 2020

comment:2

lgtm

@mwageringel
Copy link
Author

comment:3

Thanks.

@vbraun
Copy link
Member

vbraun commented Sep 23, 2020

Changed branch from u/gh-mwageringel/30515 to c118bf2

@antonio-rojas
Copy link
Contributor

comment:5

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'
**********************************************************************

@antonio-rojas
Copy link
Contributor

Changed commit from c118bf2 to none

@mwageringel
Copy link
Author

comment:6

#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants