Ubuntu
默認定義在math.h文件當中,只要#include <math.h>
就可以使用,
注意僅僅
#include <cmath>
是仍然不包含這些常量宏定義的。
Windows
默認定義在math.h文件的#include <corecrt_math_defines.h>
當中,需要不僅#include <math.h>
還需要預先進行宏定義#define _USE_MATH_DEFINES
注意僅僅
#include <cmath>
是仍然不包含這些常量宏定義的。
常用宏定義常量
- M_E=自然對數e
- M_LOG2E=log2(e)
- M_LOG10E=log10(e)
- M_LN2=ln(2)
- M_LN10=ln(10)
- M_PI=pi =3.14159265358979323846 共計21位數字
- M_PI_2=pi/2
- M_PI_4=pi/4
- M_1_PI=1/pi
- M_2_PI=2/pi
- M_2_SQRTPI=2/sqrt(pi)
- M_SQRT2=sqrt(2)
- M_SQRT1_2=1/sqrt(2)
Ubuntu 還在math.h
擴展定義了以下128bit的常量,用於long double
- M_El
- M_LOG2El
- M_LOG10EL
- M_PIl
- …
簡而言之就是在上述所有宏定義後面加了一個小寫字母l