By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

GNU pow( ) works only sometimes

P: 2
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...
Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: 27
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main () {
  7.  
  8. float x, y, z;
  9.  
  10. x = 2.1;
  11. z = 2.5;
  12.  
  13. cout << powf ( x, z ) << endl;
  14. cout << pow ( x, z );
  15.  
  16. return 0;
  17. }
  18.  
output:

6.3907
6.3907
Sep 22 '06 #2

P: 61
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.
Sep 22 '06 #3

P: 2
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:)
Sep 22 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.