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

Discussion in 'Python' started by Christian Seberino, Feb 2, 2004.

  1. 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
    Christian Seberino, Feb 2, 2004
    #1
    1. Advertising

  2. Christian Seberino

    Paul Rubin Guest

    (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).
    Paul Rubin, Feb 2, 2004
    #2
    1. Advertising

  3. Christian Seberino

    Jeff Epler Guest

    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
    Jeff Epler, Feb 2, 2004
    #3
  4. Re: C extension=> pow(2, 1) gives DIFFERENT answers in different parts of C extension!?!?! Any ideas why?

    Jeff

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

    Chris


    Jeff Epler <> wrote in message news:<>...
    > 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, Feb 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clueless Moron

    math.pow vs pow

    Clueless Moron, Nov 27, 2003, in forum: Python
    Replies:
    5
    Views:
    921
    John J. Lee
    Nov 28, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  3. Michel Rouzic

    pow(2, 1/2) != pow(2, 0.5) problem

    Michel Rouzic, Jun 15, 2005, in forum: C Programming
    Replies:
    52
    Views:
    1,665
    Alan Balmer
    Jun 20, 2005
  4. Replies:
    3
    Views:
    595
    Keith Thompson
    Mar 31, 2007
  5. Replies:
    1
    Views:
    948
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2007
Loading...

Share This Page