Floating point weirdness

J

Joshua Ginsberg

WTF, mate?
'2.3.3 (#1, May 7 2004, 10:31:40) \n[GCC 3.3.3 20040412 (Red Hat Linux
3.3.3-7)]'19.899999999999999

Why can't I just get 19.95?

-jag
 
B

Benjamin Niemann

Joshua said:
WTF, mate?


'2.3.3 (#1, May 7 2004, 10:31:40) \n[GCC 3.3.3 20040412 (Red Hat Linux
3.3.3-7)]'
19.949999999999999

19.949999999999999

19.899999999999999

Why can't I just get 19.95?
floats are represented as 64 (or 128?) bit values and there's no value
that exactly represents 19.95 or 19.9 (=round(19.949, 1)). The last
example results in the closest value to 19.9, but not exactly. Python
prints this value with maximum precision. If you want a string
representation of a float with e.g. 2 digits precision, use
"%.2f" % 19.949
 

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
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top