[Python-ideas] checking for identity before comparing built-in objects

Case Van Horsen casevh at gmail.com
Tue Oct 9 05:38:13 CEST 2012


On Mon, Oct 8, 2012 at 6:37 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Mon, Oct 8, 2012 at 9:07 PM, Guido van Rossum <guido at python.org> wrote:
>> If we want to do *anything* I think we should first introduce a
>> floating point context similar to the Decimal context. Then we can
>> talk.
>
> +float('inf')

I implemented a floating point context manager for gmpy2 and the MPFR
floating point library. By default, it enables a non-stop mode where
infinities and NaN are returned but you can also raise exceptions. You
can experiment with gmpy2: http://code.google.com/p/gmpy/

Some examples

>>> import gmpy2
>>> gmpy2.get_context()
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        trap_expbound=False,
        allow_complex=False)
>>> gmpy2.log(0)
mpfr('-inf')
>>> gmpy2.get_context()
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=True,
        trap_expbound=False,
        allow_complex=False)
>>> gmpy2.get_context().clear_flags()
>>> gmpy2.get_context().trap_divzero=True
>>> gmpy2.log(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gmpy2.DivisionByZeroError: 'mpfr' division by zero in log()
>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.nan()==gmpy2.nan()
False
>>> gmpy2.get_context()
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=True,
        trap_divzero=False, divzero=False,
        trap_expbound=False,
        allow_complex=False)
>>> gmpy2.get_context().trap_erange=True
>>> gmpy2.nan()==gmpy2.nan()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gmpy2.RangeError: comparison with NaN
>>>

Standard disclaimers:

* I'm the maintainer of gmpy2.

* Please use SVN or beta2 (when it is released) to avoid a couple of
embarrassing bugs. :(

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list