gpt4 book ai didi

c++ - M_PI 标记为未声明的标识符

转载 作者:IT老高 更新时间:2023-10-28 12:58:34 27 4
gpt4 key购买 nike

当我编译下面的代码时,我收到了这些错误消息:

(Error  1   error C2065: 'M_PI' : undeclared identifier 
2 IntelliSense: identifier "M_PI" is undefined)

这是什么?

#include <iostream>
#include <math.h>

using namespace std;

double my_sqrt1( double n );`enter code here`

int main() {
double k[5] = {-100, -10, -1, 10, 100};
int i;

for ( i = 0; i < 5; i++ ) {
double val = M_PI * pow( 10.0, k[i] );
cout << "n: "
<< val
<< "\tmysqrt: "
<< my_sqrt1(val)
<< "\tsqrt: "
<< sqrt(val)
<< endl;
}

return 0;
}

double my_sqrt1( double n ) {
int i;
double x = 1;


for ( i = 0; i < 10; i++ ) {
x = ( x + n / x ) / 2;
}

return x;
}

最佳答案

根据 their docs 的说法,听起来你正在使用 MS 的东西

Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h.

所以你需要类似的东西

#define _USE_MATH_DEFINES
#include <cmath>

作为标题。

关于c++ - M_PI 标记为未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com