Numeric comparison anomaly

Klaus Alexander Seistrup spam at magnetic-ink.dk
Wed Feb 19 08:06:46 EST 2003


Paul Rubin wrote:

> I want to define an "infinite" numeric object, that's greater than
> any regular number.  I do the obvious (Python 2.2):
> 
>     >>> class _infinity:
>     ...   def __lt__(self, n): return 0
>     ...   def __gt__(self, n): return 1
>     ... 
>     >>> infinity = _infinity()
>     >>> infinity < 9
>     0
>     >>> infinity > 99999
>     1
>     >>> 99999 < infinity
>     1
> 
> So far so good.  But here comes the surprise:
> 
>     >>> infinity >= 3
>     0

Python has builtin infinity:

#v+

$ python
Python 2.2.2 (#1, Jan 18 2003, 10:18:59) 
[GCC 3.2.2 20030109 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.95
for nice experiences hit <tab> multiple times
>>> infinity = float('Inf')
>>> infinity < 9
0
>>> infinity > 99999
1
>>> 99999 < infinity
1
>>> infinity >= 3
1
>>> ^D
$ 

#v-

You can use "float('-Inf')" and "float('NaN')", too.


  // Klaus

-- 
 ><> 	vandag, môre, altyd saam




More information about the Python-list mailing list