본문 바로가기
PROGRAMMING/C & C++

Math Constants 사용하기

by BLADEBONE 2010. 9. 30.

출처: MSDN




The following symbols are defined for the values of their indicated expressions:


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

  0.707106781186547524401



The "Math Constants" are not defined in Standard C/C++. Therefore, in order to use them, you have to first define _USE_MATH_DEFINES and then include cmath or math.h as below.


If compiling as C++:

#define _USE_MATH_DEFINES

#include <cmath>


If compiling as C:

#define _USE_MATH_DEFINES

#include <math.h>




Visual Studio 2008 기준으로 프로그램에서 수학 상수들을 사용하기 위해서는

위와 같이 math.h 또는 cmath를 include하여야 하며,

이때 include 이전에 "#define _USE_MATH_DEFINES" 를 먼저 선언해 주어야 한다.



반응형

댓글