Rappresenting infinite

Facundo Batista facundo at taniquetil.com.ar
Sat Jun 30 01:32:09 CEST 2007


mmanns at gmx.net wrote:


>> No. You can make one that fits your requirements, though.
>
> I am struggling to oversee the implications of design choices for inf
> behaviour - especially if it comes to comparison with float type inf.
> The type in my application contains a gmpy.mpq and a float that is
> always kept between -1 and 1 by adding to the mpq on each operation.
> I am looking for a robust inf design for this type. (Note: mpq and
> float inf do not compare).

Infinity, and all its behaviours are well defined in the General Decimal
Arithmetic Specification, here at http://www2.hursley.ibm.com/decimal/.

You can always pass your float to Decimal through string, and then do
there *all* the operations:

>>> from decimal import *
>>> a = 3.4
>>> b = 1.0e1000
>>> Decimal(str(a))
Decimal("3.4")
>>> Decimal(str(b))
Decimal("Infinity")
>>> Decimal(str(b)) / 0
Decimal("Infinity")
>>> Decimal(str(b)) * 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/decimal.py", line 1140, in __mul__
    return context._raise_error(InvalidOperation, '(+-)INF * 0')
  File "/usr/lib/python2.5/decimal.py", line 2325, in _raise_error
    raise error, explanation
decimal.InvalidOperation: (+-)INF * 0
>>> setcontext(ExtendedContext)
>>> Decimal(str(b)) * 0
Decimal("NaN")
>>> 

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/





More information about the Python-list mailing list