-
Notifications
You must be signed in to change notification settings - Fork 194
Implement mpf.__format__ #819
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
Conversation
The method is only implemented for the _mpf type, and it follows the same conventions as in https://docs.python.org/3/library/string.html#formatstrings, but extended to mpf floats. Also added tests to this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good start!
Please take look on comments. Also, it seems the code is not fully covered by tests (see CI failure).
…haracters after type specification
… test case for when both 0-padding and fill char are selected.
The 'z' feature was included in Python 3.11, so it makes no sense to test for it if we are using an older Python version.
* change imports * move helper up * spaces
* unident some code * kill some comments * move up _FLOAT_FORMAT_SPECIFICATION_MATCHER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with one comment on round_digits(). Please address this.
I took liberty to do some stylistic adjustments. Please take look, lets be sure your are OK with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you should test also subnormal values for floats. Either in test_mpf_float() or in test_mpf_fmt_cpython().
mpf has no restrictions on exponent range, yet I think that output should here match Python's as well.
If I understand you correctly, you mean mpf initialized from floats? Such as num = 2e-315
print('{:22.15f}'.format(num))
print('{:22.15f}'.format(mpf(num)))
print('{:22.15g}'.format(num))
print('{:22.15g}'.format(mpf(num)))
print('{:22.15e}'.format(num))
print('{:22.15e}'.format(mpf(num))) resulting in
But I would expect num_str = '2e-315'
print('{:22.15f}'.format(mpf(num_str)))
print('{:22.15g}'.format(mpf(num_str)))
print('{:22.15e}'.format(mpf(num_str))) to result into
because in this case the mpf is correctly initialized. |
Yes, as in other your compatibility tests. And with subnormal values, like in shown example.
Nothing incorrect in using mpf(float) for default settings. |
* Added this functionality to format_scientific and format_fixed functions * Added random tests for very small numbers to test_format.py
* Added 10 digits to the calls to to_digits_exp, this fixes wrong formatting for some subnormal floats. * Also removed code for no_neg_0 checking in format_scientific, since it was never called.
Formatting support must be documented, probably here: https://mpmath.readthedocs.io/en/latest/general.html#conversion-and-printing But maybe we can do this later in a separate pr. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with few comments
# String formatting # | ||
#----------------------------------------------------------------------------# | ||
|
||
blog2_10 = 3.3219280948873626 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This constant should be moved up (near math_float_inf, I think) and reused in a couple of places:
$ git grep 3.321928 mpmath/libmp/libmpf.py
mpmath/libmp/libmpf.py: return max(1, int(round(int(n)/3.3219280948873626)-1))
mpmath/libmp/libmpf.py: return max(1, int(round((int(n)+1)*3.3219280948873626)))
mpmath/libmp/libmpf.py: prec = int(len(x.as_tuple()[1])*3.3219280948873626)
mpmath/libmp/libmpf.py: blog2 = 3.3219280948873626
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@javierelpianista, thank you! |
@javierelpianista, are you planing to work on |
I could give it a try in the future, but not right now. |
Implemented the
__format__
method for the_mpf
type.Currently,
f
,F
,g
,G
,e
, andE
formats are implemented, following the same specifications as specified for regular floats here.Only nearest rounding is implemented.
Partially resolves #337.