我想在一些 C++ 程序中使用 PI 常数和三角函数。我得到三角函数include <math.h>
。但是,这个头文件中似乎没有PI的定义。
如何在不手动定义的情况下获取 PI?
在某些(尤其是较旧的)平台上(请参阅下面的评论),您可能需要
#define _USE_MATH_DEFINES
然后包含必要的头文件:
#include <math.h>
pi 的值可以通过以下方式访问:
M_PI
In my math.h
(2014)它被定义为:
# define M_PI 3.14159265358979323846 /* pi */
但检查你的math.h
了解更多。摘自“旧”math.h
(在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:
在较新的平台上(至少在我的 64 位 Ubuntu 14.04 上)我不需要定义_USE_MATH_DEFINES
-
在(最近的)Linux 平台上有long double
值也作为 GNU 扩展提供:
# define M_PIl 3.141592653589793238462643383279502884L /* pi */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)