如何在C ++中使用math.h。我需要Pi的价值 [英] how can I use math.h in C++. I need the value of Pi

查看:324
本文介绍了如何在C ++中使用math.h。我需要Pi的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我解决语法问题。

我需要将math.h用于Pi,但我不明白如何使用它。网上说很多东西就像说使用cmath而有些人说math.h



他们也是在if和else下的statememts中隐含语法的一些错误。



can someone pls help me with the syntax.
I need to use math.h for Pi but I do not understand how to use it. the net says a lot of things like saying use cmath but others say math.h

they are also a few errors concering the syntax in the statememts under if and else.

#include<iostream>
#include<math.h>
using namespace std;

int main(){

    int  iTerm;
    int iSum=0,iDenom=1,iError;
    float M_PI;
/*
cout<<"| Term | Value | Error " ;*/


for(iTerm=0;iTerm<100;iTerm++){
if(iTerm%2==1)
cout<<"|"<<iTerm<<"|" <<iSum= iSum + (4/iDenom)<<"|" <<iError=iSum - M_PI<< "|" endl;
else
cout<<"|"<<iTerm<<"|" <<iSum= iSum - (4/iDenom)<< "|" <<iError=iSum - M_PI<<"|" endl;


iDenom = iDenom + 2;


}
}

推荐答案

这是acos(-1)。 :-)



-SA
It's acos(-1). :-)

—SA


要在c ++中使用常量M_PI,我们必须包含行:

To use the constant M_PI in c++ we must include the lines:
#define _USE_MATH_DEFINES // for C++
#include <cmath>

位于文件顶部。见这里:



https:// msdn .microsoft.com / zh-CN / library / 4hwaceh6.aspx [ ^ ]



现在我们可以这样使用:



at the top of the file. See here:

https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx[^]

So now we can use in this way:

#define _USE_MATH_DEFINES
#include <cmath>
#include<conio.h>
#include <iostream>

using namespace std;

int main()
{
    double myPi = M_PI;

    cout << M_PI << endl;
    cout << myPi << endl;

    _getch();
}


这篇关于如何在C ++中使用math.h。我需要Pi的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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