set_printoptions precision and single floats

Hi, numpy.set_printoptions(precision=...) doesn't affect single floats, even if they are numpy floats rather than Python floats. Is this a bug or is there some reason for this behaviour? I ask because I have a class that derives from numpy.float64 and adds some extra information, and I'd like to be able to control the precision. I could fix it to use the precision set by numpy.set_printoptions, but then it would be inconsistent with how numpy itself handles precision. Thoughts? Dan

On Fri, Oct 5, 2012 at 5:17 PM, Dan Goodman <dg.gmane@thesamovar.net> wrote:
Hi,
numpy.set_printoptions(precision=...) doesn't affect single floats, even if they are numpy floats rather than Python floats. Is this a bug or is there some reason for this behaviour? I ask because I have a class that derives from numpy.float64 and adds some extra information, and I'd like to be able to control the precision. I could fix it to use the precision set by numpy.set_printoptions, but then it would be inconsistent with how numpy itself handles precision. Thoughts?
Do you mean scalars or arrays? For me set_printoptions only affects arrays and not scalars. Both float32 and float64 arrays work as advertised: In [28]: np.set_printoptions(precision=4) In [29]: np.array([np.float32(1.234567891011011101111012345679)]) Out[29]: array([ 1.2346], dtype=float32) In [30]: np.array([np.float64(1.234567891011011101111012345679)]) Out[30]: array([ 1.2346]) In [31]: np.set_printoptions(precision=8) In [32]: np.array([np.float32(1.234567891011011101111012345679)]) Out[32]: array([ 1.23456788], dtype=float32) In [33]: np.array([np.float64(1.234567891011011101111012345679)]) Out[33]: array([ 1.23456789]) But for scalars it doesn't work: In [34]: np.float32(1.234567891011011101111012345679) Out[34]: 1.2345679 In [35]: np.float64(1.234567891011011101111012345679) Out[35]: 1.2345678910110112 In [36]: np.set_printoptions(precision=4) In [37]: np.float32(1.234567891011011101111012345679) Out[37]: 1.2345679 In [38]: np.float64(1.234567891011011101111012345679) Out[38]: 1.2345678910110112 Ralf

On Sat, Oct 6, 2012 at 12:17 PM, Ralf Gommers <ralf.gommers@gmail.com>wrote:
On Fri, Oct 5, 2012 at 5:17 PM, Dan Goodman <dg.gmane@thesamovar.net>wrote:
Hi,
numpy.set_printoptions(precision=...) doesn't affect single floats, even if they are numpy floats rather than Python floats. Is this a bug or is there some reason for this behaviour? I ask because I have a class that derives from numpy.float64 and adds some extra information, and I'd like to be able to control the precision. I could fix it to use the precision set by numpy.set_printoptions, but then it would be inconsistent with how numpy itself handles precision. Thoughts?
Do you mean scalars or arrays? For me set_printoptions only affects arrays and not scalars. Both float32 and float64 arrays work as advertised:
In [28]: np.set_printoptions(precision=4)
In [29]: np.array([np.float32(1.234567891011011101111012345679)]) Out[29]: array([ 1.2346], dtype=float32)
In [30]: np.array([np.float64(1.234567891011011101111012345679)]) Out[30]: array([ 1.2346])
In [31]: np.set_printoptions(precision=8)
In [32]: np.array([np.float32(1.234567891011011101111012345679)]) Out[32]: array([ 1.23456788], dtype=float32)
In [33]: np.array([np.float64(1.234567891011011101111012345679)]) Out[33]: array([ 1.23456789])
But for scalars it doesn't work:
In [34]: np.float32(1.234567891011011101111012345679) Out[34]: 1.2345679
In [35]: np.float64(1.234567891011011101111012345679) Out[35]: 1.2345678910110112
In [36]: np.set_printoptions(precision=4)
In [37]: np.float32(1.234567891011011101111012345679) Out[37]: 1.2345679
In [38]: np.float64(1.234567891011011101111012345679) Out[38]: 1.2345678910110112
Ralf
It also does not affect zero-dimensional (i.e. scalar) arrays (e.g. array(1.2345)): In [1]: x = array(1./3) In [2]: x Out[2]: array(0.3333333333333333) In [3]: set_printoptions(precision=3) In [4]: x Out[4]: array(0.3333333333333333) In [5]: type(x) Out[5]: numpy.ndarray `y` is a 1-d array, so this works as expected: In [6]: y = array([1./3]) In [7]: y Out[7]: array([ 0.333]) Warren
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Sun, Oct 7, 2012 at 6:36 PM, Dan Goodman <dg.gmane@thesamovar.net> wrote:
On 06/10/2012 18:17, Ralf Gommers wrote:
Do you mean scalars or arrays? For me set_printoptions only affects arrays and not scalars. Both float32 and float64 arrays work as advertised:
Yep, I mean scalars (although as Warren noted, it also affects e.g. array(1.23456789)).
My impression from previous discussions on the mailing list is that this is by design: printing of scalars and scalar arrays is handed off to python which doesn't have a set_printoptions. printing of arrays is under the control of numpy I don't see a way to influence the __str__ or __repr__ of a python float. Josef
Dan
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Dan Goodman
-
josef.pktd@gmail.com
-
Ralf Gommers
-
Warren Weckesser