Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pow function in C

Tags:

c

gcc

pow

math.h

I write a C code that have power function that is from math.h library. when I compiled my program, I received an error which is " undefined reference to 'pow' function ", I compile my program using gcc compiler (fedora 9).

I insert -lm flag to gcc then, the error is omitted but the output of the pow function is 0.

#include<math.h>
main()
{
double a = 4, b = 2;
b = pow(b,a);
}

Can anyone help me? Is there is a problem in my compiler??

Thanks.

like image 967
hamb Avatar asked May 27 '12 13:05

hamb


People also ask

What is the use of POW in C++?

C pow() The pow() function computes the power of a number. The pow() function takes two arguments (base value and power value) and, returns the power raised to the base number. For example,

How to calculate exponent value in C using pow () function?

Given two numbers base and exponent, pow () function finds x raised to the power of y i.e. x y. Basically in C exponent value is calculated using the pow () function. Parameters: The method takes two arguments:

How does the pow () function work with integers?

Working of pow() function with integers. The pow() function takes ‘double’ as the arguments and returns a ‘double’ value. This functions does not always work for integers. One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers.

What is the difference between POW () and parameter?

Parameters: The method takes two arguments: // Storing the answer in result. The pow () function takes ‘double’ as the arguments and returns a ‘double’ value. This function does not always work for integers. One such example is pow (5, 2).


Video Answer


3 Answers

There is confusion here regarding base and exponent. This is not immediately apparent because both 2^4 and 4^2 equal 16.

void powQuestion()
{
    double a, b, c;

    a = 4.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 4 ^ 2 = 16

    a = 9.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 9 ^ 2 = 512  >> Wrong result; 512 should be 81 <<


    // K & R, Second Edition, Fifty Second Printing, p 251: pow(x,y) x to the y

    double x, y, p;

    x = 9.0;
    y = 2.0;
    p = pow(x, y);

    printf("%g ^ %g = %g\n", x, y, p);      // Output: 9 ^ 2 = 81


    // even more explicitly

    double base, exponent, power;

    base = 9.0;
    exponent = 2.0;
    power = pow(base, exponent);

    printf("%g ^ %g = %g\n", base, exponent, power);    // Output: 9 ^ 2 = 81
}
like image 54
Tom Bernard Avatar answered Nov 07 '22 06:11

Tom Bernard


For everyone else who seek such an answer:

This will not work:

gcc my_program.c -o my_program

It will produce something like this:

/tmp/cc8li91s.o: In function `main':
my_program.c:(.text+0x2d): undefined reference to `pow'
collect2: ld returned 1 exit status

This will work:

gcc my_program.c -o my_program -lm
like image 40
goFrendiAsgard Avatar answered Nov 07 '22 05:11

goFrendiAsgard


Your program doesn't output anything.

The 0 you are referring to is probably the exit code, which will be 0 if you don't explicitly return from main.

Try changing it to a standards-compliant signature and return b:

int main(void) {
  ...
  return b;
}

Note that the return values is essentially limited to 8 bits-worth of information, so very, very limited.

Use printf to display the value.

#include <stdio.h>
...
  printf("%f\n", b);
...

You must use a floating point conversion specifier (f, g or e) to print double values. You cannot use d or others and expect consistent output. (This would in fact be undefined behavior.)

like image 29
Mat Avatar answered Nov 07 '22 05:11

Mat