[Python-ideas] Disallow orderring comparison to NaN

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Apr 29 18:14:02 CEST 2011


On Fri, Apr 29, 2011 at 3:00 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
..
> decimal already works that way:
>
>>>> from decimal import Decimal as d
>>>> nan = d("nan")
>>>> nan
> Decimal('NaN')
>>>> nan < 1
> ..
> decimal.InvalidOperation: comparison involving NaN

That's what I thought and contrary to what Robert said early in the
thread.  By default, decimal operations trap InvalidOperation,
DivisionByZero, and Overflow:

>>> decimal.getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999,
Emax=999999999, capitals=1, clamp=0, flags=[],
traps=[InvalidOperation, DivisionByZero, Overflow])

The advantage that decimal has over float is that user can control
what is trapped:

>>> from decimal import *
>>> with localcontext(Context(traps=[])):
...      print(Decimal('NaN') < Decimal('0'))
...
False



More information about the Python-ideas mailing list