tags:

views:

96

answers:

3

hi I want python to return 0.5 if I write 1/2 (and not 1.0/2.0). how do I make python to return the floating point? (I tried using getcontext().prec form decimal module)

thanks

Ariel

+4  A: 
from __future__ import division
Ignacio Vazquez-Abrams
+8  A: 

Use this, or switch to Python 3.0+

from __future__ import division
truppo
Also, you can use the `-Qnew` option when running Python 2.x
S.Lott
+3  A: 

Python 3.x works the way you want by default.

Python 2.2 and greater support from __future__ import division, which makes / return floating point. There is also the // operator that still performs integer division when needed.

Also see PEP238 - Changing the Division Operator.

Mark Tolonen