[Tutor] crash when importing random or sys module, in my embedded python

Magnus Lyckå magnus@thinkware.se
Wed Jun 18 22:17:01 2003


At 21:55 2003-06-18 -0400, R. Alan Monroe wrote:
>Here's the error:
>'computed value for TWOPI deviates too much (computed 6.28319, expected 
>6.28319)',

What platform are you working on?

What result do you get if you do:

 >>> import math
 >>> math.pi * 2

???

You should get something like 6.2831853071795862

As you see below, you must stay roughly between 6.2831852
and 6.2831854 for random.py to be happy.

The code that causes the exception is in random.py
(as you might guess, and you can obviously look at
that.)

...
from math import log as _log, exp as _exp, pi as _pi, e as _e
...
def _verify(name, computed, expected):
     if abs(computed - expected) > 1e-7:
         raise ValueError(
             "computed value for %s deviates too much "
             "(computed %g, expected %g)" % (name, computed, expected))
...
TWOPI = 2.0*_pi
_verify('TWOPI', TWOPI, 6.28318530718)
...

If you change %g to %r or %.20g etc in the function above, you will
see how far off you were in calculating pi.

You can naturally muck with this by removing the verification or
increasing the fuzz factor, but I assume the test is there for a
reason, so you'll get a less good random generation.


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language