Why does this work
print "{:e}".format(array([1e-10],dtype="float64")[0])
1.000000e-10
but not this?
print "{:e}".format(array([1e-10],dtype="float32")[0])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-9a0800b4df65> in <module>()
----> 1 print "{:e}".format(array([1e-10],dtype="float32")[0])
ValueError: Unknown format code 'e' for object of type 'str
Update: I tried with numpy version 1.6.1 and Python 2.7.3.
me@serv8:~$ python -V
Python 2.7.3
me@serv8:~$ python -c "import numpy; print numpy.__version__"
1.6.1
me@serv8:~$ python -c "from numpy import array; print \"{:e}\".format(array([1e-10],dtype=\"float32\")[0])"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: Unknown format code 'e' for object of type 'str'
This is a bug which was fixed in Numpy 1.6.2 (Change log here).
Hmm... it looks like we can get the types okay:
>>> from numpy import array
>>> a64 = array([1e-10],dtype="float64")[0]
>>> a32 = array([1e-10],dtype="float32")[0]
>>> type(a32)
<type 'numpy.float32'>
>>> type(a64)
<type 'numpy.float64'>
So, let's try printing now:
>>> print a32
1e-10
>>> print a64
1e-10
Okay, that seems to work. Let's try printing with exponent notation:
>>> print('{0:e}'.format(a64))
1.000000e-10
>>> print('{0:e}'.format(a32))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'e' for object of type 'str'
Doing some googling, I found a similar reference to the bug #1675 which is supposedly fixed in Numpy version 1.6.2. (Change log here)
Based on this, I subsequently installed 1.6.2 and tried what you tried above. It works.
>>> from numpy import array
>>> print "{:e}".format(array([1e-10],dtype="float64")[0])
1.000000e-10
>>> print "{:e}".format(array([1e-10],dtype="float32")[0])
1.000000e-10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With