[Tutor] Unexpected result from decimal

Tony Meyer tameyer at ihug.co.nz
Thu Jan 20 04:35:12 CET 2005


> >>> import decimal
> >>> decimal.getcontext().prec = 2
> >>> a = decimal.Decimal(2)
> >>> b = decimal.Decimal(3)
> >>> 100*a/b
> Decimal("67")
> >>> print 100*a/b

This prints "67".

> try - 
> 
> a=decimal.Decimal(2.0)

This will not work.  You can't convert a float directly to a decimal.Decimal
(I believe this is so that you are forced to understand that there are
precision issues involved).  'a = decimal.Decimal("2.0")' will do what you
meant, though.

> b = decimal.Decimal(3)
> print 100*a/b

However, this will print out "67", just as the above did.  The reason is the
one that Tim outlined: precision isn't the number of digits after the
decimal place - when I was at school the latter was called "decimal places"
and precision was "significant digits".

> Jacob- one slight flaw/quirk in Python is if you want floating point
> computations you have to specify a floating point.
[...]
> Same as writing 100/3.0 as opposed to 100/3. Try it. 

Note that you can do 'from __future__ import division' and 100/3 will be the
same as 100/3.0 (and 100//3 will give you 3).

=Tony.Meyer



More information about the Tutor mailing list