Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   C extension=> pow(2,1) gives DIFFERENT answers in different parts of C extension!?!?! Any ideas why? (http://www.velocityreviews.com/forums/t327767-c-extension-pow-2-1-gives-different-answers-in-different-parts-of-c-extension-any-ideas-why.html)

 Christian Seberino 02-02-2004 08:18 AM

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

I put this print statement in various parts of my c extension...

printf("XXX %lf\n", pow(2,1));

(I replaced XXX with name of function it was in.)

It gave 2.0 at first but when I called another C function I made
and entered it... then value dropped to 0.0!???!?

Any ideas why?

Chris

 Paul Rubin 02-02-2004 08:48 AM

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

seberino@spawar.navy.mil (Christian Seberino) writes:
> I put this print statement in various parts of my c extension...
>
> printf("XXX %lf\n", pow(2,1));

Try pow(2.0, 1.0).

 Jeff Epler 02-02-2004 01:06 PM

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

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

 Christian Seberino 02-05-2004 04:36 AM

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

Jeff

was EXACTLY right.

Chris

Jeff Epler <jepler@unpythonic.net> wrote in message news:<mailman.1122.1075727212.12720.python-list@python.org>...
> 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

 All times are GMT. The time now is 08:32 PM.