[Numpy-discussion] set_printoptions precision and single floats

Warren Weckesser warren.weckesser at enthought.com
Sat Oct 6 12:26:22 EDT 2012


On Sat, Oct 6, 2012 at 12:17 PM, Ralf Gommers <ralf.gommers at gmail.com>wrote:

>
>
> On Fri, Oct 5, 2012 at 5:17 PM, Dan Goodman <dg.gmane at 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121006/5873c96b/attachment.html>


More information about the NumPy-Discussion mailing list