Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32496
| Date | 2012-10-30 11:13 -0400 |
|---|---|
| From | Dave Angel <d@davea.name> |
| Subject | Re: Float to String "%.7e" - diff between Python-2.6 and Python-2.7 |
| References | <69712207-3e7c-4dc2-be36-9b1a94f34c24@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3094.1351610120.27098.python-list@python.org> (permalink) |
On 10/30/2012 10:47 AM, andrew.mackeith@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)? > >> c:\python26\python > r:\asiData\abaObjects\lib>c:\python26\python > Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on win32 > 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.0967322e+02 <<<<<<<< > 2.096732151e+02 2.0967322e+02 > 4.096732160e+00 4.0967322e+00 > Looks to me that the indicated value was rounded wrong in 2.6, so apparently that was fixed in 2.7 The actual binary fp value stored for 2.096732150 e+02 is slightly smaller than the decimal string specified, so it'll round towards 1.0967321 when you specify 7 digits. I don't know of any way to re-introduce the earlier version. But you could fix them both by using Decimal, where there's no quantization error. Or if this is a fake example, adapt by just validating that the results are 'close' -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Float to String "%.7e" - diff between Python-2.6 and Python-2.7 andrew.mackeith@3ds.com - 2012-10-30 07:47 -0700 Re: Float to String "%.7e" - diff between Python-2.6 and Python-2.7 Duncan Booth <duncan.booth@invalid.invalid> - 2012-10-30 15:10 +0000 Re: Float to String "%.7e" - diff between Python-2.6 and Python-2.7 Dave Angel <d@davea.name> - 2012-10-30 11:13 -0400 Re: Float to String Mark Dickinson <mdickinson@enthought.com> - 2012-10-31 15:39 +0000
csiph-web