如何在C ++中使用PI常量我想在一些C ++程序中使用PI常量和三角函數(shù)。我得到了三角函數(shù)include <math.h>。但是,此頭文件中似乎沒有PI的定義。如何在不手動定義PI的情況下獲取PI?
3 回答

富國滬深
TA貢獻1790條經(jīng)驗 獲得超9個贊
在某些(特別是較舊的)平臺上(請參閱下面的評論),您可能需要這樣做
#define _USE_MATH_DEFINES
然后包含必要的頭文件:
#include <math.h>
pi的值可以通過以下方式訪問:
M_PI
在我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. */
然而:
在較新的平臺上(至少在我的64位Ubuntu 14.04上)我不需要定義
_USE_MATH_DEFINES
在(最近的)Linux平臺上,還有
long double
作為GNU擴展提供的值:# define M_PIl 3.141592653589793238462643383279502884L /* pi */
- 3 回答
- 0 關注
- 1037 瀏覽
添加回答
舉報
0/150
提交
取消