-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
WIP: MAINT: Rewrite LongFloatFormat, trim zeros in scientific notation output #9919
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
a195592
to
e5b9ed6
Compare
e5b9ed6
to
958914b
Compare
def __call__(self, x): | ||
r = self.real_format(x.real) | ||
i = self.imag_format(x.imag) | ||
return r + i + 'j' |
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.
Why is this different from the ComplexFormat
version? Which one is preferable?
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.
I didn't notice they were different. I'll probably copy/subclass the ComplexFloat version.
Is there a reason not just to modify |
elif strip_zeros: | ||
z = s.rstrip('0') | ||
s = z + ' '*(len(s)-len(z)) | ||
return s | ||
|
||
|
||
class LongFloatFormat(FloatFormat): |
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.
I'd be inclined to make the base class FloatingFormatter
to match np.floating
, and use a derived class for FloatFormatter
too.
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.
will do
I left Also, |
Also, just a comment on the state of float printing in numpy and python: It's messy. We use C's Using C's Other languages like Julia, Go, Rust, javascript in some browsers, have decided that the OS Here are some links to discussion of these issues: Blog Post about Dragon4 and Grisu3 Probably in an ideal world numpy would include a good float-printing algorithm, perhaps customized for our requirements (eg, for output alignment). But we have lived without one until now, so I don't think it's very high priority. |
Hmm, some of the numpy problems in my last comment might be solved by using calls to |
One last comment: I don't think the |
note: this PR is addressing issue #9699 |
The main purpose of this PR is to implement the new
longfloat
arrayprint formatter, as promised in #9139.Now,
longfloat
arrays will print essentially identically tofloat
anddouble
arrays, using the same code paths. This meanslongfloat
arrays now align nicely and have the right number of spaces.This PR does two additional things which are somewhat unrelated, but are in the same code: I removed trailing zeros in scientific notation, so
1.0000e+100
becomes1.e+100
, and I made thelongfloat
andhalf
formatter precision customizable independently from the float/double precision.I think this is mostly done, I just need to add a few more tests, eg to test the "locale"-related code, and to test the new precision customization.
Sidenote: I implemented
format_longfloat
using a "trick" involving temporarily setting the "C" locale (if necessary) which avoids all the complicated manipulation of decimals used inNumpyOS_ascii_format_double
(which was copied from the CPython code). Resetting the locale was recommended on the gcc page on locales.