#include< math.h> [英] #include <math.h>

查看:114
本文介绍了#include< math.h>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我似乎遇到了一些数学函数(pow,sqrt,

acos)。他们是我在我的代码中使用的唯一一个,他们阻止程序

编译。我得到一个未定义的引用''pow''错误。这是我的代码的

相关部分。


我们将非常感谢您的帮助。谢谢!


*遗传算法模块

*

* /


#包括< stdio.h>

#include< stdlib.h>

#include< time.h>

#include< ; math.h>

#include" string.h"

/ * GetSD

*

*此函数返回dataT *数组中特征向量的标准差

*。

* /

静态双GetSD(dataT * dataArr [],int data_size,int features [],double

mean []){

int i,j;

double data_vect [GENES_PER_CHROM ],data_sum,mean_sum,ang_sum,dotprod,

data_norm,mean_norm,angle;

double sd;


ang_sum = 0 ;


for(i = 0; i< data_size; i ++){

for(j = 0; j< GENES_PER_CHROM; j ++){

data_vect [j] = dataArr [i] - > val [(features [j])];

}


//得到两个向量的点积

dotprod = 0; ; j< GENES_PER_CHROM; j ++){

dotprod + = data_vect [j] *表示[j];

}


//获得规范

data_sum = 0;

mean_sum = 0;

for(j = 0; j< GENES_PER_CHROM; j ++){

data_sum + = pow(data_vect [j],2);

mean_sum + = pow(mean [j],2);

}

data_norm = sqrt(data_sum);

mean_norm = sqrt(mean_sum);


//计算角度

angle = acos(dotprod /(data_norm * mean_norm));


ang_sum + = pow(angle,2);

}


sd = sqrt(ang_sum /(data_size - 1));


返回sd;

}

Hi,

I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They''re the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to ''pow''" error. Here is the
relevant portion of my code.

Your help would be appreciated. Thanks!

* Genetic Algorithm module
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "string.h"
/* GetSD
*
* This function returns the standard deviation of the feature vectors
* in a dataT * array.
*/
static double GetSD(dataT *dataArr[], int data_size, int features[], double
mean[]) {
int i, j;
double data_vect[GENES_PER_CHROM], data_sum, mean_sum, ang_sum, dotprod,
data_norm, mean_norm, angle;
double sd;

ang_sum = 0;

for(i = 0; i < data_size; i++) {
for(j = 0; j < GENES_PER_CHROM; j++) {
data_vect[j] = dataArr[i]->val[(features[j])];
}

// get dot product of the two vectors
dotprod = 0;
for(j = 0; j < GENES_PER_CHROM; j++) {
dotprod += data_vect[j] * mean[j];
}

// get the norms
data_sum = 0;
mean_sum = 0;
for(j = 0; j < GENES_PER_CHROM; j++) {
data_sum += pow(data_vect[j], 2);
mean_sum += pow(mean[j], 2);
}
data_norm = sqrt(data_sum);
mean_norm = sqrt(mean_sum);

// compute the angle
angle = acos(dotprod/(data_norm * mean_norm));

ang_sum += pow(angle, 2);
}

sd = sqrt(ang_sum / (data_size - 1));

return sd;
}

推荐答案

Darius Fatakia< da ************ @ yahoo.com>写道:
Darius Fatakia <da************@yahoo.com> wrote:

我似乎遇到了一些数学函数(pow,sqrt,
acos)。它们是我在我的代码中使用的唯一的,它们阻止程序编译。我得到一个未定义的引用''pow''错误。这是我的代码的相关部分。
Hi, I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They''re the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to ''pow''" error. Here is the
relevant portion of my code.




您很可能遇到链接问题。阅读你的系统

文档,了解如何在数学库中链接。


< OT>将-lm附加到编译行可能会有效。 < / OT>


Alex



You are most likely having a linking problem. Read your system
documentation to find out how to link in the math library.

<OT> Appending -lm to your compile line might work. </OT>

Alex


Darius Fatakia写道:
Darius Fatakia wrote:
I似乎在我的一些数学函数(pow,sqrt,
acos)上遇到了麻烦。它们是我在我的代码中使用的唯一的,它们阻止程序编译。我得到一个未定义的引用''pow''错误。这是我的代码的相关部分。
cat gam.c
typedef struct dataT {

double * val;

} dataT;


#define GENES_PER_CHROM 1024

/ *

*遗传算法模块

* /


#include< stdio.h>

#include< stdlib.h>

#include< time.h>

#include< math.h>

#include" string.h"

/ * GetSD

*

*此函数返回dataT *数组中特征向量的标准差

*。

* /


static

double GetSD(dataT * dataArr [],

int data_size,int features [],double mean []){

double data_vect [GENES_PER_CHROM];

double ang_sum = 0.0;


for(int i = 0; i< data_size; ++ i){

for(int j = 0; j< GENES_PER_CHROM; ++ j){

data_vect [j] = dataArr [i] - > val [(features [j]) ];

}


//得到两者的点积向量

double dotprod = 0.0;

for(int j = 0; j< GENES_PER_CHROM; ++ j){

dotprod + = data_vect [j] *表示[j];

}


//得到规范

double data_sum = 0.0;

double mean_sum = 0.0;

for(int j = 0; j< GENES_PER_CHROM; ++ j){

data_sum + = pow(data_vect [j],2);

mean_sum + = pow(mean [j],2);

}

double data_norm = sqrt(data_sum);

double mean_norm = sqrt(mean_sum);


//计算角度

double angle = acos(dotprod /(data_norm * mean_norm));

ang_sum + = pow(angle,2);

}

返回sqrt(ang_sum /(data_size - 1));

}


int main(int argc,char * argv []){

dataT * dataArr [GENES_PER_CHROM];

int data_size = 1024;

int features [1024];

双重均值[GENES_PER_CHROM];

GetSD(dataArr,data_size,features,mean);


返回0;

}

gcc -Wall -std = c99 -pedantic -o gam gam.c -lm
I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They''re the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to ''pow''" error. Here is the
relevant portion of my code. cat gam.c typedef struct dataT {
double* val;
} dataT;

#define GENES_PER_CHROM 1024

/*
* Genetic Algorithm module
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "string.h"
/* GetSD
*
* This function returns the standard deviation
* of the feature vectors in a dataT* array.
*/

static
double GetSD(dataT* dataArr[],
int data_size, int features[], double mean[]) {
double data_vect[GENES_PER_CHROM];
double ang_sum = 0.0;

for(int i = 0; i < data_size; ++i) {
for(int j = 0; j < GENES_PER_CHROM; ++j) {
data_vect[j] = dataArr[i]->val[(features[j])];
}

// get dot product of the two vectors
double dotprod = 0.0;
for(int j = 0; j < GENES_PER_CHROM; ++j) {
dotprod += data_vect[j]*mean[j];
}

// get the norms
double data_sum = 0.0;
double mean_sum = 0.0;
for(int j = 0; j < GENES_PER_CHROM; ++j) {
data_sum += pow(data_vect[j], 2);
mean_sum += pow(mean[j], 2);
}
double data_norm = sqrt(data_sum);
double mean_norm = sqrt(mean_sum);

// compute the angle
double angle = acos(dotprod/(data_norm*mean_norm));
ang_sum += pow(angle, 2);
}
return sqrt(ang_sum/(data_size - 1));
}

int main(int argc, char* argv[]) {
dataT* dataArr[GENES_PER_CHROM];
int data_size = 1024;
int features[1024];
double mean[GENES_PER_CHROM];
GetSD(dataArr, data_size, features, mean);

return 0;
}
gcc -Wall -std=c99 -pedantic -o gam gam.c -lm




如果包含math.h,则需要在libm.a中使用-lm选项进行链接。



If you include math.h, you need to link in libm.a with the -lm option.


On星期一,2003年11月24日16:52:07 -0800,Darius Fatakia写道:
On Mon, 24 Nov 2003 16:52:07 -0800, Darius Fatakia wrote:


我似乎遇到了一些问题功能(pow,sqrt,
acos)。它们是我在我的代码中使用的唯一的,它们阻止程序编译。我得到一个未定义的引用''pow''错误。这是我的代码的相关部分。
Hi,

I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They''re the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to ''pow''" error. Here is the
relevant portion of my code.




听起来像链接器错误 - 您是否链接数学库?某些特别受欢迎的编译器,特别是在Linux世界中广泛使用的,有一些特殊的编译器,

,它具有不包括数学的最终脑死亡习惯
默认情况下
库。假设你正在使用这个编译器,试试这样的东西




gcc -lm file.c



Sounds like a linker error - are you linking the math libraries? There''s
a certain, popular compiler, especially widely used in the Linux world,
which has the terminally brain-dead habit of not including the math
libraries by default. Assuming you''re using this compiler, try something
like this:

gcc -lm file.c


这篇关于#include&lt; math.h&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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