C'S POW函数拒绝与变量指数工作 [英] C's pow function refuses to work with variable exponent

查看:229
本文介绍了C'S POW函数拒绝与变量指数工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有以下的code片断:

Let's say I have the following code snippet:

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

试图编译这个code产生一个​​未定义的引用'战俘'的错误。

Trying to compile this code yields an "undefined reference to `pow'" error.

包括或排除文件math.h 没什么区别,因为它结束了反正被包括在内。

Including or excluding math.h makes no difference, since it ends up being included anyway.

募集 2.0 来硬$ C $光盘电源工作正常,但如果我通过包含<$ C $一名前pression替代指数都失败C> I 。

Raising 2.0 to a hardcoded power works okay, but everything fails if I substitute the exponent by an expression that contains i.

我是什么做错了吗?谢谢你。

What am I doing wrong? Thanks.

推荐答案

这是一个非常有趣的行为,一个很好的学习的例子。

It's a very interesting behavior, and a good learning example.

要解决你的问题,添加

-lm

你的gcc命令行(前提是你正在使用gcc)。这告诉编译器对数学库链接。

to your gcc command line (provided you're using gcc). This tells the compiler to link against the math library.

什么似乎是怎么回事,就是如果你使用

What seems to be going on, is that if you're using

pow(2.0, 3);

编译器实现这个前pression计算结果为常数,不单纯的替代。

the compiler realizes this expression evaluates to a constant, and does mere substitution.

因此​​,没有库函数被调用。

Thus, no library function has to be called.

这篇关于C'S POW函数拒绝与变量指数工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆