[Numpy-discussion] Printing individual array elements with at least 15 significant digits

Derek Homeier derek@astro.physik.uni-goettingen...
Sat Oct 15 14:34:10 CDT 2011


On 15.10.2011, at 9:21PM, Hugo Gagnon wrote:

> I need to print individual elements of a float64 array to a text file.
> However in the file I only get 12 significant digits, the same as with:
> 
>>>> a = np.zeros(3)
>>>> a.fill(1./3)
>>>> print a[0]
> 0.333333333333
>>>> len(str(a[0])) - 2
> 12
> 
> whereas
> 
>>>> len(repr(a[0])) - 2
> 17
> 
> which makes more sense since I am expecting at least 15 significant
> digits…
> 
> So how can I print a np.float64 with at least 15 significant digits
> (without repr!)?

You mean like 
>>> '%.15e' % (1./3)
'3.333333333333333e-01'
? 

If you are using e.g. savetxt to print to the file, you can specify the format 
the same way (actually the default for savetxt is already "%.18e", which 
should satisfy your demands).

HTH,
						Derek




More information about the NumPy-Discussion mailing list