[issue14028] random.choice hits ValueError: cannot convert float NaN to integer

Raymond Hettinger report at bugs.python.org
Thu Feb 16 12:09:08 CET 2012


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

The hypothesis that time.time() is returning NaN doesn't match the provided traceback.  If time.time() had returned NaN, the exception would have happened earlier, on line 113 in random.py:  long(time.time() * 256)

I'm wondering if the NaN arises in the C code for random():

random_random(RandomObject *self)
{
    unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
    return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
}

Upstream from that, only integers are used, so this would be the earliest a NaN could arise when running the code in choice():  ``return seq[int(self.random() * len(seq))]``

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14028>
_______________________________________


More information about the Python-bugs-list mailing list