Making decimal default datatype for floats during runtime

Carsten Haese carsten at uniqsys.com
Thu Jun 21 09:14:04 EDT 2007


On Thu, 2007-06-21 at 09:34 +0200, Stefan Sonnenberg-Carstens wrote:
> Hi,
> I was wondering if there are any tricks around to
> do some sort of changing types, float in particular.
> I do many operations like summing etc on lots
> of floats and always have to do some extra checks where
> results are heading 0.0, like round(n,10) for example.
> My idea was to tell python in some way not to take the type
> float but decimal in an implicit fassion.
> It that possible ?

No. Explicit is better than implicit.

> And, is it still true for python 2.4/2.5 that one needs to do
> a = Decimal(str(aFloat))
> instead of
> a = Decimal(aFloat)
> as it is in python 2.3 ?

Of course:
>>> 0.1
0.10000000000000001

Should Decimal(0.1) be Decimal("0.1") or Decimal("0.10000000000000001")?
Until Python can read your mind, you'll have to specify which one you
want by passing in a string containing all the digits you want to keep.

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list