Strange Errors with Python-2.2.2

Neal Norwitz neal at metaslash.com
Sun Dec 22 10:21:28 EST 2002


On Sun, 22 Dec 2002 07:32:32 -0500, lemonite wrote:

> After searching through Python's source code, I recognized, that the
> funtion float_int() in floatobject.c is causing my trouble and its
> implementation has changed compared with Python-2.1.3. It seems, as if
> the call to modf() returns wrong results and to be sure I've tested
> modf() with the following C testfile:
> 
> #include <stdio.h>
> #include <math.h>
> 
> int main()
> {
> 	double x = 1.1;
> 	double y;
> 	
> 	printf("Calling modf() for %f\n", x); 
>       printf("fractional part = %f\ninteger part = %f\n",
> 					modf(x, &y), y);
> 	
> 	return 0;
> }
> }
> The expected output should look like this:
> 
> Calling modf() for 1.100000
> fractional part = 0.100000
> integer part = 1.000000

Actually, that expected output is not guaranteed AFAIK.

> but GCC actually gave me:
> 
> $ gcc test.c -o test
> $ ./test
> Calling modf() for 1.100000
> fractional part = 1.100000
> integer part = -0.000000

Try changing the last printf() in your program to this:

        x = modf(x, &y);
        printf("fractional part = %f\ninteger part = %f\n", x, y);

I get the correct answer with the above change, using gcc 2.96-98
(Redhat).

There was a change in this area for Python 2.3.  The change should be
backported for 2.2.3.

What version of gcc are you using and on what operating system?
Can you print the values of aslong, wholepart, (long)wholepart,
and (double)aslong in float_int()?  Something like:

	printf("a=%ld, w=%.20f, (d)a=%.20f, (l)w=%ld\n",
		aslong, wholepart, (double)aslong, (long)wholepart);


Regards,
Neal



More information about the Python-list mailing list