[Numpy-discussion] set_printoptions precision and single floats

Ralf Gommers ralf.gommers at gmail.com
Sat Oct 6 12:17:00 EDT 2012

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121006/35885c5a/attachment.html>

More information about the NumPy-Discussion mailing list