Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32527

Re: Float to String

From Mark Dickinson <mdickinson@enthought.com>
Subject Re: Float to String
Date 2012-10-31 15:39 +0000
References <69712207-3e7c-4dc2-be36-9b1a94f34c24@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3122.1351698307.27098.python-list@python.org> (permalink)

Show all headers | View raw


 <andrew.mackeith <at> 3ds.com> writes:

> 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?

Yes, in a sense.  Python <= 2.6 uses the OS-provided functionality (e.g., the C
library's strtod, dtoa and sprintf functions) to do float-to-string and
string-to-float conversions, and hence behaves differently from platform to
platform.  In particular, it's common for near halfway cases (like the one
you're looking at here) and tiny numbers to give different results on different
platforms.  Python >= 2.7 has its own built-in code for performing
float-to-string and string-to-float conversions, so those conversions are
platform- independent and always correctly rounded.  (Nitpick: it's still
theoretically possible for Python 2.7 to use the OS code if it can't determine
the floating-point format, or if it can't find a way to ensure the proper FPU
settings, but I don't know of any current platforms where that's the case.)

> Is there any way of forcing the Python-2.6 behavior (for compatibility
> reasons when testing)?

Not easily, no.

--
Mark

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


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