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

  • Thread starter Christian Seberino
  • Start date
C

Christian Seberino

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
 
J

Jeff Epler

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
 
C

Christian Seberino

Jeff

I can't tell you how glad I am for your post. Your hunch
was EXACTLY right.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top