Friday, September 24, 2010

Forcing Floating Point Calculation in Python

Sources:
  • http://docs.python.org/tutorial/floatingpoint.html
  • http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python

Method-1:Just write any of the parameters in the division operation in floating-point format (eg. 4.0, 3.0)
>>> 4.0 /  3
or, >>> 4 / 3.0

Method-2:
from __future__ import devision
Method-3:
>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')

0 comments:

Post a Comment