curious problem with large numbers

  • Thread starter Chris Fonnesbeck
  • Start date
C

Chris Fonnesbeck

I have been developing a python module for Markov chain Monte Carlo
estimation, in which I frequently compare variable values with a very
large number, that I arbitrarily define as:

inf = 1e10000

However, on Windows (have tried on Mac, Linux) I get the following behaviour:
1.0

while I would have expected:

1.#INF

Smaller numbers, as expected, yield:
1e+100

Obviously, I cannot use the former to compare against large (but not
infinite) numbers, at least not to get the result I expect. Has anyone
seen this behaviour?

Thanks,
Chris
 
D

David M. Cooke

Chris Fonnesbeck said:
I have been developing a python module for Markov chain Monte Carlo
estimation, in which I frequently compare variable values with a very
large number, that I arbitrarily define as:

inf = 1e10000

However, on Windows (have tried on Mac, Linux) I get the following behaviour:

1.0

while I would have expected:

1.#INF

Smaller numbers, as expected, yield:

1e+100

Obviously, I cannot use the former to compare against large (but not
infinite) numbers, at least not to get the result I expect. Has anyone
seen this behaviour?

I don't do Windows, so I can't say this will work, but try

I think your problem is how the number is being parsed; perhaps
Windows is punting on all those zeros? Computing the infinity may or
may not work, but it gets around anything happening in parsing.

Alternatively, if you have numarray installed (which you should
probably consider if you're doing numerical stuff ;-) you could use
inf

(there's minus_inf, nan, plus_zero, and minus_zero also)
 
K

Kristian Zoerhoff

On Apr 7, 2005 3:34 PM, David M. Cooke
I don't do Windows, so I can't say this will work, but try

I *do* do Windows, and that does work. The value of inf then becomes
'1.#INF' as expected. Strangely, doing

float('1.#INF')

still fails on Windows. Weird, weird.
 
L

Laszlo Zsolt Nagy

I thought it will be the same for FreeBSD, but here are the results:

FreeBSD 4.8 with Python 2.3.4

Python 2.3.4 (#2, Nov 10 2004, 05:08:39)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):



FreeBSD 4.8 with Python 2.4

Python 2.4 (#2, Jan 27 2005, 17:11:08)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):



--
_________________________________________________________________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: (e-mail address removed)

Python forever!
 
D

Dan Bishop

Chris said:
I have been developing a python module for Markov chain Monte Carlo
estimation, in which I frequently compare variable values with a very
large number, that I arbitrarily define as:

inf = 1e10000

Don't forget that you can write your own Infinity.

(Warning: Buggy code, especially for unsigned Infinity)

class Infinity(object):
def __init__(self, sign=1):
self._sign = cmp(sign, 0)
def __repr__(self):
return "Infinity(%d)" % self._sign
def __str__(self):
if self._sign == 0:
return "UInf"
elif self._sign < 0:
return "-Inf"
else:
return "+Inf"
def __eq__(self, other):
return isinstance(other, Infinity) and self._sign == other._sign
def __ne__(self, other):
return not (self == other)
def __le__(self, other):
if self._sign == 0:
raise ValueError("Unsigned Infinity is incomparable")
elif self._sign < 0:
return True
else:
return False
def __ge__(self, other):
if self._sign == 0:
raise ValueError("Unsigned Infinity is incomparable")
elif self._sign < 0:
return False
else:
return True
def __lt__(self, other):
return not (self >= other)
def __gt__(self, other):
return not (self <= other)
def __add__(self, other):
if isinstance(other, Infinity):
if self._sign == other._sign:
return self
else:
raise ValueError("%s + %s is undefined" % (self, other))
else:
return self
__radd__ = __add__
def __neg__(self):
return Infinity(-self._sign)
def __sub__(self, other):
return self + (-other)
def __rsub__(self, other):
return -self
def __mul__(self, other):
return Infinity(self._sign * other)
__rmul__ = __mul__
def __div__(self, other):
if instance(other, Infinity):
raise ValueError("Inf/Inf is undefined")
else:
return Infinity(self._sign * other)
__truediv__ = __div__
__floordiv__ = __div__
def __rtruediv__(self, other):
return 0
__rdiv__ = __rtruediv__
def __rfloordiv__(self, other):
if self._sign * cmp(other, 0) < 0:
return -1
else:
return 0

POSITIVE_INFINITY = Infinity(1)
NEGATIVE_INFINITY = Infinity(-1)
UNSIGNED_INFINITY = Infinity(0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top