Very basic question
Bryan Olson
fakeaddress at nowhere.org
Tue Dec 23 06:36:16 EST 2008
Sengly wrote:
> I can hack it by doing eval('1.0*12/5') but is there any better method?
Where did you get the string? If you generated it, you might as well
make one or both the operands float to begin with. If you got it as
input, calling eval() on it is a world of security hurt.
The right way would be to parse the expression, so you can evaluate it
as you wish.
Security defects aside, just prepending '1.0 *' doesn't work in general,
because the string could be something like '5 + 12 / 5'. If you replace
each '/' that isn't immediately followed by another '/' with '* 1.0 /',
that might work... or maybe someone fill find counter-examples.
Python 3 does what you want. The / operator is float division. The //
operator is still integer division.
--
--Bryan
More information about the Python-list
mailing list