Skip to content

BUG: str(np.float) should print with the same number of digits as python str(float) - #9966

Merged
charris merged 4 commits into
numpy:masterfrom
ahaldane:dragon4_scalarstr_fix
Nov 12, 2017
Merged

charris merged 4 commits into
numpy:masterfrom
ahaldane:dragon4_scalarstr_fix

Conversation

@ahaldane

@ahaldane ahaldane commented Nov 5, 2017 •

Copy link
Copy Markdown
Member

See pandas-dev/pandas#18123

This makes numpy floating-point scalars str and repr (ie, str(np.float64(1.2345))) print in the same way as python floats (done in the 2nd commit). That is:

  • str uses 8 digits, in fixed mode for 1e-4 < x < 1e11 and scientific mode otherwise.
  • repr uses as many digits as needed to specify the value uniquely, in fixed mode for 1e-4 < x < 1e16 and scientific mode otherwise.

(Updated: Both str and repr now print all digits, like floats in python3.)

The PR is so big because I needed to expose control of the digit cutoff mode in the Dragon4 algorithm, which I had left hidden before. That's all in the 1st commit. This allows you to control whether precision refers to the number of digits past 0, or the number of significant digits. An example:

>>> np.format_float_positional(0.00123456789, precision=4, fractional=True)
'0.0012'
>>> np.format_float_positional(0.00123456789, precision=4, fractional=False)
'0.001235'

This was required to fix the scalar str because Python outputs floats with fractional=False, while in numpy arrays we use fractional=True.

@ahaldane
ahaldane force-pushed the dragon4_scalarstr_fix branch 6 times, most recently from abe916b to 3f6f653 Compare November 5, 2017 23:13
@ahaldane

ahaldane commented Nov 6, 2017

Copy link
Copy Markdown
Member Author

Interesting: In python3, the str of floats was changed to be identical to the repr, so it has enough digits to be uniquely specified.

See https://bugs.python.org/issue9337
https://stackoverflow.com/questions/25898733/why-does-strfloat-return-more-digits-in-python-3-than-python-2

So I guess the design decision here is, should we ouput the shorter str for numpy-floats in python2, or should we always use the exact floats? I'm going to say "always use the unique (longer) value".

@charris

charris commented Nov 6, 2017

Copy link
Copy Markdown
Member

"always use the unique (longer) value"

Sounds right to me. Python 2 is starting to recede in the rear mirror, time to prepare folks for 3 :0

Comment thread numpy/core/src/multiarray/dragon4.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look safe to me. Shouldn't it be do {} while(0)?

@eric-wieser eric-wieser Nov 6, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have :dragon4_positional after the list of types, to produce a more useful error.

Comment thread numpy/core/src/multiarray/dragon4.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining the purpose of cutoffNumber >= 0? the only comment i can see in this diff about cutoffNumber == -1 is a deletion!

Comment thread doc/release/1.14.0-notes.rst Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling int*j -> inf*j

@ahaldane
ahaldane force-pushed the dragon4_scalarstr_fix branch from 5ea534e to ac8de9e Compare November 6, 2017 16:06
@ahaldane

ahaldane commented Nov 6, 2017

Copy link
Copy Markdown
Member Author

Updated.

Although, curiously I see no effect of adding :funcname to the end of the thy PyArg... argument string.

@ahaldane
ahaldane force-pushed the dragon4_scalarstr_fix branch 3 times, most recently from 7161d83 to edc5dee Compare November 6, 2017 18:04
@ahaldane
ahaldane force-pushed the dragon4_scalarstr_fix branch from edc5dee to 386b8b4 Compare November 6, 2017 19:26
@ahaldane
ahaldane force-pushed the dragon4_scalarstr_fix branch from 386b8b4 to 719afcc Compare November 6, 2017 19:29
@ahaldane ahaldane added this to the 1.14.0 release milestone Nov 9, 2017
@charris

charris commented Nov 11, 2017

Copy link
Copy Markdown
Member

Is this ready?

@ahaldane

Copy link
Copy Markdown
Member Author

As far as I am concerned, yes.

@charris
charris merged commit 2890b94 into numpy:master Nov 12, 2017
@charris

charris commented Nov 12, 2017

Copy link
Copy Markdown
Member

OK, in it goes. Thanks Allan.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants