From: Thomas Jollans on
On Thursday 12 August 2010, it occurred to Bradley Hintze to exclaim:
> Hi all.
>
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

The conversion from decimal to binary and vice versa is inexact -- but they're
the same number internally:

Python 2.6.6rc1+ (r266rc1:83691, Aug 5 2010, 17:07:04)
[GCC 4.4.5 20100728 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 34.52
34.520000000000003
>>> 34.520000000000003 == 34.52
True
>>>


Python 3 is a bit smarter when printing, so you don't really notice what's
going on:

Python 3.1.2 (release31-maint, Jul 8 2010, 09:18:08)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 34.52
34.52
>>> 34.520000000000003
34.52
>>>

From: Chris Rebert on
On Thu, Aug 12, 2010 at 1:43 PM, Bradley Hintze
<bradley.h(a)aggiemail.usu.edu> wrote:
> Hi all.
>
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

<stuff repeating Gary's answer redacted>

See also: http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

Another option is to use the `decimal` module, which *can* exactly
represent decimal numbers (to a user-specified finite precision) and
is generally a bit more intuitive but /substantially/ slower:
http://docs.python.org/library/decimal.html

Cheers,
Chris
--
http://blog.rebertia.com
From: Christian Heimes on
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

This isn't a Python issue. Python uses IEEE 754 [1] double precision
floats like most other languages. 34.52 can't be stored in a float. The
next valid float is 34.5200000002.

Christian

[1] http://en.wikipedia.org/wiki/IEEE_754

From: Nobody on
On Thu, 12 Aug 2010 18:19:40 -0700, Benjamin Kaplan wrote:

> But that's not keeping the number the way it was typed. It's just not
> showing you the exact approximation.

Nor is 34.520000000000003 showing you the "exact approximation".

The closest "double" to 34.52 is 4858258098025923 / 2**47, whose exact
decimal representation is:

34.52000000000000312638803734444081783294677734375

The decimal representation of 1/(2**n) for n>0 always ends in 5. As a
consequence of this, the exact decimal representation of any binary
floating-point value which isn't itself an integer must end in 5.

From: Mark Dickinson on
On Aug 12, 9:43 pm, Bradley Hintze <bradle...(a)aggiemail.usu.edu>
wrote:
> Hi all.
>
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

Nitpick: unless you're on very unusual hardware, you're missing some
zeros here. On my machine, under Python 2.6, the float 34.52 displays
as 34.520000000000003, and the value stored internally is actually
34.52000000000000312638803734444081783294677734375; so that's within
1 part in 10**16 of the value you entered.

Why do you care? That's a serious question, and its answer goes a
long way to determining what you should do.

- If you're doing calculations with this number, then the difference
between the number Python stores and 34.52 is so miniscule that in
normal situations it's not going to matter. In particular, if it
represents some physical quantity then any error in the representation
will be swamped by the inherent measurement error. IOW, it's not
worth worrying about.

- If you're printing this number, and you just want the output to
look nice (why? perhaps because you're showing this to other
people?), then use float formatting operations to limit the number of
decimal places you're printing. For example, '%.6f' % my_float, or
format(my_float, '.6f'), will give my_float to 6 places after the
decimal point. Or, as others have mentioned, it just so happens that
Python 2.7 and 3.x will output a nice representation for this float
automatically. That wouldn't necessarily be true if the result were
coming from a calculation, though, so you shouldn't rely on repr
producing nice results in those versions of Python.

- If you *really* need a number that represents the *exact* value
34.52, then use the decimal module, or perhaps consider using a simple
home-brewed fixed-point representation. One situation where you might
care is when doing financial calculations, and in particular when
rounding a quantity to a smaller number of decimal digits. Here
binary floats can give unpredictable results in halfway cases. (E.g.,
round(2.675, 2) might give 2.68 or 2.67, depending on what version of
Python you're using, and also possibly depending on your platforms.)

--
Mark