pow function in math.h

B

Black Eagle

Hey guys,

Under linux, I tried this :
#include <stdio.h>
#include <math.h>
main()
{
double a= 2.0;
double b= 3.0;
printf("%f^%f=%f",a,b,pow(a,b));
}


gcc main.c -o main

But it's not working, it says that pow function is undefined, any idea ?

Thx
 
M

Martin Ambuhl

J

Jatin Kukreja

Is it better to use
x*x

or pow(x,2) ???

Just asking cause someone told me that the former is better


: Black Eagle wrote:
:> Hey guys,
:>
:> Under linux, I tried this :
:> #include <stdio.h>
:> #include <math.h>
:> main()
:> {
:> double a= 2.0;
:> double b= 3.0;
:> printf("%f^%f=%f",a,b,pow(a,b));
:> }
:>
:>
:> gcc main.c -o main
:>
:> But it's not working, it says that pow function is undefined, any idea ?

: try

: gcc main.c -lm -o main

: -lm means to link the math lib in.

: Tom
 
M

Mark McIntyre

Is it better to use
x*x

or pow(x,2) ???

Just asking cause someone told me that the former is better

AFAIK the Standard doesn't say. Indeed its possible for your compiler
to optimise the latter into the former, should the 2nd parameter be an
integer.

If it really matters to you (and it should not) then do some
extensive tests in your implementation and remember the results may be
different on a different computer, compiler etc.
 
J

John Tsiombikas (Nuclear / the Lab)

Jatin said:
Is it better to use
x*x

or pow(x,2) ???

Just asking cause someone told me that the former is better

the first one is certainly better in terms of performance (if the
compiler does not detect and substitute it) since you skip the function
call overhead which is far more than the code for the simple operation
x*x just placed inline with the rest of the code.

I usually define something like this: #define SQ(x) ((x) * (x)) and
use for such cases

-- Nuclear / the Lab --
 
C

Chris Dollin

John said:
the first one is certainly better in terms of performance (if the
compiler does not detect and substitute it) since you skip the function
call overhead

What function call overhead? The compiler is free to inline pow if it
likes. If it doesn't like, it may have good reason. It likely depends
on the compiler flags.

[If x is integral, then the results of x*x and pow(x,2) are *different*,
so you must pick the correct one, and arguments about efficiency cut
little grass.]
which is far more than the code for the simple operation
x*x just placed inline with the rest of the code.

Unlikely. The overhead of pow is its generality; the function-call
overhead might be as little as two instructions plus a pipeline
glitch.
I usually define something like this: #define SQ(x) ((x) * (x)) and
use for such cases

Well, let's hope your arguments to SQ are never complicated and never
have side-effects.
 
M

Malcolm

Jatin Kukreja said:
Is it better to use
x*x

or pow(x,2) ???

Just asking cause someone told me that the former is better
Generally x*x will be much faster, since pow() involves a call to a
complicated function that can handle arbitrary reals. I don't believe that
many compilers would optimise to x*x, though I'm open to correction on that
one.

However unless your calculation is in an inner loop, it is unlikely to make
any noticeable difference to the program's running time. A program is a
means of communication between programmers as well as a series of
instructions for a computer. If you think that pow(x,2) makes your
expression easier to read, or communicates your intention better (eg if we
are calculating a series pow(x,0) + pow(x, 1) + pow(x, 2) + pow(x,3) ...)
then go ahead and use it.
 
J

John Tsiombikas (Nuclear / the Lab)

What function call overhead? The compiler is free to inline pow if it
likes. If it doesn't like, it may have good reason. It likely depends
on the compiler flags.

Well that was my point actually... you don't know and if we're talking
about a performance critical part of the code, you might want to make
sure that it ends up to x*x by actually doing it like that yourself :)
Unlikely. The overhead of pow is its generality; the function-call
overhead might be as little as two instructions plus a pipeline
glitch.

Indeed, you're right. So we're talking for even greater overhead (I
would do it x*x, even if it was just the function call overhead
involved, in an inner loop).

-- Nuclear / the Lab --
 

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

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top