[Tutor] eval and floating point
Andre Engels
andreengels at gmail.com
Thu Jan 15 13:21:47 CET 2009
On Thu, Jan 15, 2009 at 1:08 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Mr Gerard Kelly" <s4027340 at student.uq.edu.au> wrote
>
>> I've noticed that the eval() function gives an integer, so eval("3/2")
>> gives back 1. float(eval("3/2")) doesn't seem to work, any way to get a
>> floating point number back with eval()?
>
> Move the float inside the eval:
>
> 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")
or
eval("3/float(2)")
Which, as said before, is written less complicatedly as:
eval("3.0/2")
--
André Engels, andreengels at gmail.com
More information about the Tutor
mailing list