[Tutor] eval and floating point

Alan Gauld alan.gauld at btinternet.com
Thu Jan 15 19:20:24 CET 2009

"Andre Engels" <andreengels at gmail.com> wrote

>> eval("float(3/2)")
> That still does not work, because the 'float' comes after the
> division. 3/2 equals 1, so float(3/2) equals 1.0. To make it work,
> you'll have to put the float inside the division:
> eval("float(3)/2")

Ahem! Quite. That was what I actually intended to post!

But having seen the other posts suggesting using future 
I had a rethink.

I don't think either float() or the "3.0" approach will help the 
OP because I suspect he is trying to eval a string stored 
in a variable. In that case the "import future" trick is probably 
the only solution that will work, short of parsing the string and 
doing the division without using eval...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list