[FLOAT] rounding error, what is the best way to deal with it?

Deon

Lt. of Mordor
Joined
Jan 13, 2008
Messages
2,956
Location
St.Petersburg, Russian Federation
Hello, I started to learn python just yesterday (using python.org), wrote a few programs with lists and dictionaries (now I know how to make and edit a database :p LoL) and stumbled upon a problem...
First time i saw
Code:
>>> print [39.95]
[39.950000000000003]
I was really disappointed. Then I've found through google that this is the usual "float"-type rounding error caused by different bases and precisions.

So I have a question, what if I really need 39.95 in some cases (like float('39.95') )? How to tell the python the required base(10) and precision and what is the best way to deal with this problem in general?

[Offtopic] Also I'm a bit lost so I'd appreciate some help from a Guru, at least a finger at some useful tutorial for the CivIV. Because now when I started opening all those files in others' mods to see what can be done, the most complicated thing appears to be in fact the number of different functions, lists, dictionaries and variables the game already uses (like pPlayer, pPlot and others). Some of them are very clear by name, but some are just Alien Artifacts...
 
Python API


You can try str("%g" %39.95). If that's not enough then perhaps you can seperate it into two variables, 39 and 95? I've never had a reason to use floats in python myself, so there may be better solutions than this.
 
You should be able to use printf style format strings in python. So, if you want a string from a float with two digits of precision, you'd do:

str("%.2f" %39.950000003)
 
Thank you, iamfishhead. This helps alot =). I solved it another way, split it with [,] to 2 strings, converted to int and then used err... how to say.. binary adding (I don't know this word in english, you convert number to binary format and make a+b).
I'm not a programmer at all, so I should learn to think like the programmer. I think it'll take some time =).
 
If you need to deal with exact float numbers, then what you want is the decimal module. Check the documentation in the Python Library Reference.
 
Back
Top Bottom