Floating numbers

B

Bradley Hintze

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.

--
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799
 
G

Grant Edwards

Is there a way I can keep my floating point number as I typed it?
No.

For example, I want 34.52 to be 34.52 and NOT 34.5200000002.

You can't represent 34.52 using base-2 IEEE floating point (the HW
floating point format used by pretty much all modern computers).
34.5200000002 is as close as you can get.

When you enter a base-10 floating-point number, the computer will use
the closest base-2 IEEE floating point number.

Here are the nitty-gritty details:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Can you explain what your actual problem is?
 
G

Grant Edwards

You can't represent 34.52 using base-2 IEEE floating point (the HW
floating point format used by pretty much all modern computers).
34.5200000002 is as close as you can get.

When you enter a base-10 floating-point number, the computer will use
the closest base-2 IEEE floating point number.

Here are the nitty-gritty details:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Here is a gentler intro:

http://pyfaq.infogami.com/why-are-floating-point-calculations-so-inaccurate
 
M

Mark Dickinson

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.)
 
J

jmfauth

A quick question.

I understand how to get these numbers

34.52000000000000312638803734444081783294677734375

and

47 (from 2**47)

and the sign.

with the decimal module, but I fail to find this one

4858258098025923

Possible?
 
M

Mark Dickinson

A quick question.

I understand how to get these numbers

34.52000000000000312638803734444081783294677734375

and

47 (from 2**47)

and the sign.

with the decimal module, but I fail to find this one

4858258098025923

Possible?

See the float.as_integer_ratio method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top