Strange Errors with Python-2.2.2

lemonite lemonite at t-online.de
Sun Dec 22 10:51:40 EST 2002


Thanks for your reply.

On Sun, 22 Dec 2002 10:21:28 +0000 Neal Norwitz wrote:
> Try changing the last printf() in your program to this:
> 
>         x = modf(x, &y);
>         printf("fractional part = %f\ninteger part = %f\n", x, y);
> 

Well, this change should not make any difference at all since modf()
returns the fractional part, while as a side effect storing the integer
part into the object pointed to by its second argument.

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

And the original listing fails to give you a correct result?

> 
> 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?

See my initial posting (gcc-3.2.1, glibc-2.3.1, Gentoo Linux 1.4rc).

> 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);

Yes of course. In fact this is what I tried before writing the test
program above. float_int() looks as follows:

float_int(PyObject *v)
{
	double x = PyFloat_AsDouble(v);
	double wholepart;	/* integral portion of x, rounded toward 0 */
	long aslong;		/* (long)wholepart */
	
	printf("%f\n", x);

	(void)modf(x, &wholepart);
#ifdef RISCOS
	/* conversion from floating to integral type would raise exception */
	if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
		PyErr_SetString(PyExc_OverflowError, "float too large to convert");
		return NULL;
	}
#endif
	/* doubles may have more bits than longs, or vice versa; and casting
	   to long may yield gibberish in either case.  What really matters
	   is whether converting back to double again reproduces what we
	   started with. */
	aslong = (long)wholepart;
	printf("a=%ld, w=%.20f, (d)a=%.20f, (l)w=%ld\n", 
			aslong, wholepart, (double)aslong, (long)wholepart);
	if ((double)aslong == wholepart)
		return PyInt_FromLong(aslong);
	PyErr_SetString(PyExc_OverflowError, "float too large to convert");
	return NULL;
}

and running the compiled Python looks like this:

Python 2.2.2 (#2, Dec 22 2002, 16:48:16) 
[GCC 3.2.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int(1.1)
1.100000
a=0, w=-0.00000000000000000000, (d)a=0.00000000000000000000, (l)w=0
0
>>>

As I said in my previous post, the problem seems to be GCC or glibc
related. I've already posted to gnu.gcc.help, asking for support.

> 
> 
> Regards,
> Neal


Thanks for your answer again.

Kind regards,
lemonite



More information about the Python-list mailing list