You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [1]: np.array(.1)[()] == np.array(.1).item()
Out[1]: True
In [2]: np.array(.1)[()] # a np.float64
Out[2]: 0.10000000000000001
In [3]: np.array(.1).item() # a python (64-bit) float
Out[3]: 0.1
Yeah, this bugs me a bit too :-). The complication though is that when
printing arrays, it's pretty important that the different elements take up
the same number of columns of screen space, so any solution to this would
need some way to address that. Maybe print the shortest equivalent
representation and then pad to spaces if it's shorter than the allotted
space, or only do this for scalars, or something like that.
On Jul 31, 2015 10:51, "Antony Lee" [email protected] wrote:
In [1]: np.array(.1)[()] == np.array(.1).item()
Out[1]: True
In [2]: np.array(.1)[()] # a np.float64
Out[2]: 0.10000000000000001
In [3]: np.array(.1).item() # a python (64-bit) float
Out[3]: 0.1
Array printing passes through separate code from scalar repr. In fact, repr on arrays intentionally does not produce an invertible representation of the array (it prints according to printoptions). But this could be fixed for np.float64 without too much difficulty. np.float32 could be converted to double, which will sometimes produce more digits than necessary for inversion. np.longdouble can't go through python floats, so it would require implementing the algorithm to get a tidy representation; probably not worth it. The code would go in numpy/core/src/multiarray/scalartypes.c.src in the templated function @name@type_@kind@; the repr version would just convert to a python float and use repr.
I think it would be better to write the same kind of type-precision respecting print that Python has rather than trying to go through Python's float-printing code. It would be nice for float32 to print shorter numbers automatically.
Activity
njsmith commentedon Aug 1, 2015
Yeah, this bugs me a bit too :-). The complication though is that when
printing arrays, it's pretty important that the different elements take up
the same number of columns of screen space, so any solution to this would
need some way to address that. Maybe print the shortest equivalent
representation and then pad to spaces if it's shorter than the allotted
space, or only do this for scalars, or something like that.
On Jul 31, 2015 10:51, "Antony Lee" [email protected] wrote:
anntzer commentedon Aug 1, 2015
It's already space-padded:
aarchiba commentedon Aug 29, 2015
Array printing passes through separate code from scalar repr. In fact, repr on arrays intentionally does not produce an invertible representation of the array (it prints according to printoptions). But this could be fixed for np.float64 without too much difficulty. np.float32 could be converted to double, which will sometimes produce more digits than necessary for inversion. np.longdouble can't go through python floats, so it would require implementing the algorithm to get a tidy representation; probably not worth it. The code would go in numpy/core/src/multiarray/scalartypes.c.src in the templated function @name@type_@kind@; the repr version would just convert to a python float and use repr.
NeilGirdhar commentedon May 18, 2016
I think it would be better to write the same kind of type-precision respecting print that Python has rather than trying to go through Python's float-printing code. It would be nice for
float32to print shorter numbers automatically.ahaldane commentedon Nov 5, 2017
Closed by #9941