标签 cmath 下的文章

C++: cmath里的M_PI


1.摘要

项目开发中挺多需要pi这个东西的,例如弧度与角度的转换,然后自己还要去定义一个宏来使用,一整还冲突。其实cmath.h的头文件里就有关于pi的宏,还有pi/2、pi/4等

2.使用方法

  • #define _USE_MATH_DEFINES
  • #include <cmath>

3.补充说明

cmath.h中会包含corect_math_defines.h的相关定义。

  • #define M_E 2.71828182845904523536 // e
  • #define M_LOG2E 1.44269504088896340736 // log2(e)
  • #define M_LOG10E 0.434294481903251827651 // log10(e)
  • #define M_LN2 0.693147180559945309417 // ln(2)
  • #define M_LN10 2.30258509299404568402 // ln(10)
  • #define M_PI 3.14159265358979323846 // pi
  • #define M_PI_2 1.57079632679489661923 // pi/2
  • #define M_PI_4 0.785398163397448309616 // pi/4
  • #define M_1_PI 0.318309886183790671538 // 1/pi
  • #define M_2_PI 0.636619772367581343076 // 2/pi
  • #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
  • #define M_SQRT2 1.41421356237309504880 // sqrt(2)
  • #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)

4.注意事项

在头部添加使用2中的使用方法还要注意一下位置,位置不一样也会报找不到的错。
屏幕截图 2022-07-01 143923.png
这样处理才能使用成功。和奇怪,具体也不知道为啥,如果把iostream放在第一位就不能用了。