This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Strange behaviour of decimal.Decimal
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, parmax
Priority: normal Keywords:

Created on 2010-01-04 05:41 by parmax, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg97192 - (view) Author: parmax (parmax) Date: 2010-01-04 05:41
>>> from decimal import Decimal
>>> dec = Decimal(2 ** 1024)
>>> dec
Decimal('179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216')
>>> dec += Decimal('0.1')
>>> dec
Decimal('1.797693134862315907729305191E+308')
>>> dec == Decimal(2 ** 1024) + Decimal('0.1')
True
>>> dec -= Decimal(2 ** 1024)
>>> dec
Decimal('2.109752663820230210576934273E+280')
>>> dec == Decimal('0.1')
False
>>>
msg97196 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-04 08:58
You don't say what behaviour you were expecting!   :)

By design, almost all Decimal operations (but not creation of a Decimal from an integer or string) round to the precision given by the current context.  By default that precision is 28 significant digits.  See the documentation, particularly the quick-start tutorial,

http://docs.python.org/library/decimal.html#quick-start-tutorial

for more information, including how to change the default precision.
msg97199 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-04 09:09
It might also help to note that, with decimal, what you see (from repr() of a Decimal instance) is *exactly* what you get.  So when you see

>>> dec
Decimal('1.797693134862315907729305191E+308')

that number really is exactly what's stored in dec:  there are no 'hidden'  digits waiting to appear.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51879
2010-01-04 09:09:12mark.dickinsonsetmessages: + msg97199
2010-01-04 08:58:28mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg97196

resolution: not a bug
2010-01-04 05:41:56parmaxcreate