我正在尝试编写一个程序来近似 pi。它基本上取 0.00 到 1.00 之间的随机点并将它们与圆的边界进行比较,圆内的点与总点的比率应该接近 pi(一个非常快速的解释,规范更深入)。
但是,使用 gcc 编译时出现以下错误:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
这是怎么回事?我以前从未见过此错误,也不知道为什么会出现此错误。这是我的代码(虽然我没有完全测试它,因为我无法克服错误):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}