C extension=> pow(2, 1) gives DIFFERENT answers in different parts of C extension!?!?! Any ideas why?

Jeff Epler jepler at unpythonic.net
Mon Feb 2 08:06:46 EST 2004


This may be a simple "C" error.  If you call pow() without a prototype
in scope, a compiler will not necessarily give you an error.  However,
the return type of pow() will be int, not double.  In that case,
anything can happen.

$ cat seberino.c
#ifdef RIGHT
#include <math.h>
#endif

#include <stdio.h>

int main(void) { printf("%f\n", pow(2, 1)); }
$ gcc seberino.c -lm && ./a.out
-1.993340
$ gcc seberino.c -lm -DRIGHT && ./a.out
2.000000

You might consider enabling more compiler warnings:
$ gcc -Wall seberino.c -lm
seberino.c: In function `main':
seberino.c:7: warning: implicit declaration of function `pow'
seberino.c:7: warning: double format, different type arg (arg 2)
seberino.c:7: warning: control reaches end of non-void function

Jeff




More information about the Python-list mailing list