Float to String "%.7e" - diff between Python-2.6 and Python-2.7
Duncan Booth
duncan.booth at invalid.invalid
Tue Oct 30 11:10:54 EDT 2012
andrew.mackeith at 3ds.com wrote:
> When formatting a float using the exponential format, the rounding is
> different in Python-2.6 and Python-2.7. See example below. Is this
> intentional?
>
> Is there any way of forcing the Python-2.6 behavior (for compatibility
> reasons when testing)?
>
It isn't Python 2.6 behaviour, it looks more like a bug in your particular
version of 2.6. This one matches what you are seeing on 2.7:
[dbooth at localhost ~]$ /opt/local/bin/python2.6
Python 2.6.7 (r267:88850, Jan 5 2012, 16:18:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+
02,2.096732160+02]
>>> for a in x:
... print ' %.9e %.7e'%(a,a)
...
2.096732130e+02 2.0967321e+02
2.096732140e+02 2.0967321e+02
2.096732150e+02 2.0967321e+02
2.096732151e+02 2.0967322e+02
4.096732160e+00 4.0967322e+00
Note that the rounding shown here is correct; the actual value is slightly
less than 5 in the last place:
[dbooth at localhost ~]$ /opt/local/bin/python2.6 -c "print('%.20e'%
2.096732150e+02,'%.7e'%2.096732150e+02)"
('2.09673214999999999009e+02', '2.0967321e+02')
What do you get printing the value on 2.6 with a '%.20e' format? I seem to
remember that 2.7 rewrote float parsing because previously it was buggy.
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list