[Python-bugs-list] [ python-Bugs-721402 ] Random on PPC is outside acceptable range.

SourceForge.net noreply@sourceforge.net
Sat, 19 Apr 2003 19:47:49 -0700


Bugs item #721402, was opened at 2003-04-14 16:43
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=721402&group_id=5470

Category: Extension Modules
>Group: 3rd Party
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Kyle Wheeler (memoryhole)
>Assigned to: Tim Peters (tim_one)
Summary: Random on PPC is outside acceptable range.

Initial Comment:
On LinuxPPC, with gcc 2.96, python's (2.2.2) random function is outside the acceptable range. For example:

>>> import random
>>> print random.random()
1.35933812391

This random function, I believe, is supposed to produce numbers between 0 and 1.

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2003-04-19 22:47

Message:
Logged In: YES 
user_id=31435

Hmm!  You could try disabling optimization *only* for 
floatobject.c.  Compiler optimization bugs are usually shy, 
and sometimes specific to one block of code that manages 
to trigger an untested endcase in the optimizer.  I wouldn't 
worry that optimization is generally unsafe.

The CONVERT_TO_DOUBLE macro in floatobject.c contains 
some pretty excruciating logic, and my bet is that the 
compiler is screwing up there.  float_rem() doesn't really do 
anything else before calling fmod().  You could play with 
that in a debugger or via inserting printfs.  If you can get a 
small failing test case out of it, I'm sure the compiler 
authors would appreciate seeing it.

Closing as 3rdParty, since there's no evidence of a Python 
bug here.

----------------------------------------------------------------------

Comment By: Kyle Wheeler (memoryhole)
Date: 2003-04-19 21:52

Message:
Logged In: YES 
user_id=17596

I'm guessing it's the compiler in some very strange way. I
recompiled python without optimization, and the problem went
away.

The optimization problem, however, must be particular to
whatever the optimization situation is in floatobject.c,
because a very simple C program using fmod() doesn't report
incorrect values no matter how much optimization I tell gcc
to perform.

Thanks for helping me! (hopefully, in the not entirely
distant future, I'll upgrade to gcc 3.2 or so, and will be
able to safely recompile python with optimization)

~Kyle

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-04-14 17:35

Message:
Logged In: YES 
user_id=31435

Heh.  Sorry about the "1.,0" -- that was a typo.  "1.0" was 
intended.

For the rest, floating mod is clearly broken in this Python, 
and that's why random() is goofy (the last step in random() is 
a fload mod).   It's not broken on any platform I have, so I 
can't be much help.

First thing to try is to recompile Python with optimizations 
disabled.  If it still fails, step thru floatobject.c's float_rem 
function in a debugger and see where it's getting an insane 
result.

If you know how to write C code, it could be that printing the 
result of C

#import <math.h>
double x = fmod(1.35933812391, 1.0);

will show a bug instantly (in which case your C libm has the 
bug).

----------------------------------------------------------------------

Comment By: Kyle Wheeler (memoryhole)
Date: 2003-04-14 17:19

Message:
Logged In: YES 
user_id=17596

Python 2.2.2 (#1, Apr 13 2003, 16:19:28) 
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print 1.35933812391 % 1,.0
1.35933812391 0.0
>>> print 1.35933812391 % 1.0
1.35933812391
>>> print 4%2                
0
>>> print 1.3%2
3.3

Any ideas?

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-04-14 17:14

Message:
Logged In: YES 
user_id=31435

What does the following statement print under this Python:

print 1.35933812391 % 1,.0

?

Note that there's almost no chance this is a bug in Python 
(the implementation of random() hasn't changed in about 10 
years).  There's a good chance it's a bug in the specific  C 
compiler or C library release you're using.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=721402&group_id=5470