在一些(特别是较旧的)平台(见下面的注释),你可能需要
#define _USE_MATH_DEFINES
然后包括必要的头文件:
#include
并且pi的值可以通过以下方式访问:
M_PI
在我的math.h(2014)它的定义为:
# define M_PI 3.14159265358979323846 /* pi */
但检查你的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.
*/
然而:
>在较新的平台上(至少在我的64位Ubuntu 14.04)我不需要定义_USE_MATH_DEFINES
>在(最近的)Linux平台上,有一个long double值作为GNU扩展提供:
# define M_PIl 3.141592653589793238462643383279502884L /* pi */