Skip to content

Floating point __str__ #6908

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

Closed
anntzer opened this issue Dec 30, 2015 · 10 comments
Closed

Floating point __str__ #6908

anntzer opened this issue Dec 30, 2015 · 10 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Dec 30, 2015

Related to (but different from) #6136: float64.__str__ can truncate different floats to the same string, even when printoptions is set to a high enough precision:

In [12]: np.set_printoptions(precision=1000)

In [13]: x = np.float64(0.20000000000000004)

In [14]: x > np.float64(.2)
Out[14]: True

In [15]: str(x)
Out[15]: '0.2' # oops

Note that repr(x) correctly gives '0.20000000000000004'.

@gfyoung
Copy link
Contributor

gfyoung commented Jan 12, 2016

Is it me, or are the print options only respected when we want to print arrays? Here's another example of "bad" behavior with scalars:

>>> import numpy as np
>>> x = np.float64(0.12345678901234567890123456789)
>>> m = np.array([x])
>>>
>>> str(x) # default precision is 8 decimal places
'0.123456789012' # oops
>>> str(m) # printing an array containing 'x' respects the precision
'[ 0.12345679]'

@sam09
Copy link

sam09 commented Jan 16, 2016

Is anyone working on this issue?
If not i would like to give it a go

@gfyoung
Copy link
Contributor

gfyoung commented Jan 16, 2016

I don't know of anyone who is. Generally, IIUC the etiquette is that unless someone is really talking about it or has already opened a PR to fix the issue, it is completely up for grabs by anyone. So go for it! Good luck!

@anntzer
Copy link
Contributor Author

anntzer commented Jan 16, 2016

Please consider fixing #6136 at the same time :)

@sam09
Copy link

sam09 commented Jan 20, 2016

It seems the printoptions are set in numpy/core/arrayprint.py. It has functions defined for printing an array but not single numpy values. Is this behavior intended? I mean is printoptions set to work only for arrays? Here is link to an answered question on stackoverflow. Please tell me if I am missing something

@gfyoung
Copy link
Contributor

gfyoung commented Jan 20, 2016

Judging from the labeling by @rgommers , I suspect not. I think you may very well have to add a good amount of new code in order to enforce printing for numbers.

@sam09
Copy link

sam09 commented Jan 25, 2016

I think the float values are also printed by array2string function defined in numpy/core/arrayprint.py.
This is where I think it happens

@gfyoung
Copy link
Contributor

gfyoung commented Jan 25, 2016

You still have to cast a as an array object. It's just one without a shape, so the function still does not apply to pure literals like a = 5 for example.

@NeilGirdhar
Copy link
Contributor

I just ran into this issue. printoptions should be respected by all three:

In [21]: np.set_printoptions(precision=3)

In [22]: print(np.ones(()) * (0.123456789123))
0.123456789123

In [23]: print(np.float32(0.123456789123))
0.123457

In [24]: print(np.ones((1,)) * (0.123456789123))
[ 0.123]

yingzong pushed a commit to yingzong/numpy that referenced this issue Aug 7, 2016
prints distinguishing parentheses for scalar types
addresses issue numpy#7355, numpy#7913, related to numpy#6908
@ahaldane
Copy link
Member

ahaldane commented Nov 5, 2017

Closed by #9941

@ahaldane ahaldane closed this as completed Nov 5, 2017
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

6 participants