如何在C ++中使用PI常量 [英] How to use the PI constant in C++
问题描述
我想在一些C ++程序中使用PI常数和三角函数。我得到 include< math.h>
的三角函数。但是,在这个头文件中似乎没有PI的定义。
如何手动定义PI不能得到PI?
在某些(特别是较老的)平台上(见下面的注释),你可能需要
#define _USE_MATH_DEFINES
,然后包含必要的头文件:
#include< math.h>
,pi的值可通过以下方式访问:
M_PI
> math.h (2014)它定义为:
#define M_PI 3.14159265358979323846 / * pi * /
但检查您的 math.h
更多。 旧 math.h
(2009年)的摘录:
/ *在包含math.h之前定义_USE_MATH_DEFINES以公开常见数学常量的这些宏
*定义。这些被放置在#ifdef
*下,因为这些通常定义的名称不是C / C ++标准的一部分。但是:
* /
ol>
在更新的平台上(至少在我的64位Ubuntu 14.04)我不需要定义 _USE_MATH_DEFINES
在最近的Linux平台上,还有 long double
b
$ b
#define M_PIl 3.141592653589793238462643383279502884L / * pi * /
I want to use the PI constant and trigonometric functions in some C++ program. I get the trigonometric functions with include <math.h>
. However, there doesn't seem to be a definition for PI in this header file.
How can I get PI without defining it manually?
解决方案 On some (especially older) platforms (see the comments below) you might need to
#define _USE_MATH_DEFINES
and then include the necessary header file:
#include <math.h>
and the value of pi can be accessed via:
M_PI
In my math.h
(2014) it is defined as:
# define M_PI 3.14159265358979323846 /* pi */
but check your math.h
for more. An extract from the "old" math.h
(in 2009):
/* Define _USE_MATH_DEFINES before including math.h to expose these macro
* definitions for common math constants. These are placed under an #ifdef
* since these commonly-defined names are not part of the C/C++ standards.
*/
However:
on newer platforms (at least on my 64 bit Ubuntu 14.04) I do not need to define the _USE_MATH_DEFINES
On (recent) Linux platforms there are long double
values too provided as a GNU Extension:
# define M_PIl 3.141592653589793238462643383279502884L /* pi */
这篇关于如何在C ++中使用PI常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!