GNU pow( ) works only sometimes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • binladen
    New Member
    • Sep 2006
    • 2

    GNU pow( ) works only sometimes

    anyone know why this works:

    float y;

    y = powf ( 2.0, 2.0 );


    ...and this does not (undefined reference to 'powf')?

    float x, y, z;

    x = 2.0;
    z = 2.0;

    y = powf ( x, z );

    ...can't be a library issue, as alluded to by the error msg, because using literals instead of variables works. The horror...
  • dush
    New Member
    • Sep 2006
    • 27

    #2
    Hi

    I am using g++ and including <math.h>, everything works fine.
    Have you tried pow instead of powf ?
    pow is overloaded for float arguments too

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main () {
    	
    float x, y, z;
    
    x = 2.1;
    z = 2.5;
    
    cout << powf ( x, z ) << endl;
    cout << pow ( x, z );
    
    return 0;
    }
    output:

    6.3907
    6.3907

    Comment

    • pukur123
      New Member
      • Sep 2006
      • 61

      #3
      Its library problem......

      compile like this

      gcc -lm program.c

      Here lm option specifies that the C file contains some functions belonging to the libm library.

      Comment

      • binladen
        New Member
        • Sep 2006
        • 2

        #4
        Thanks for the tips!
        yeah, Im just wondering why it works with literals and not variables.

        the -lm links in the math lib & variables work, sure.
        And pow had the same hangup.

        Its my first day using this compiler & I thought it a bit strange...

        My code works..maybe I should just be happy:)

        Comment

        Working...