C's pow function refuses to work with variable exponent

后端 未结 5 1508
遥遥无期
遥遥无期 2021-01-17 20:59

Let\'s say I have the following code snippet:

int i; double value;
for(i = 0; i < CONSTANT; i++) {
  value = (double)pow(2, i);
}

Trying

5条回答
  •  一生所求
    2021-01-17 21:46

    You must link against the math library:

    gcc program.c -lm
    

    The reason is that GCC (and some other compilers) have a built-in pow() function for literal constants. So if you call pow() with 2.0 manually, the compiler will actually figure-out what the answer is and substitute that for you. With a variable input, the compiler must rely on the math library, which you must link against.

提交回复
热议问题