반응형

cpp numerics library

수치 라이브러리


일반 수학 함수들은 <cmath> 헤더에 선언되어 있고 std::sin, std::sqrt 등으로 사용할 수 있다. 네임스페이스는 std를 사용.

복소수는 <complex> 헤더, 알고리즘은 <numeric> 헤더를 사용한다.


++Common mathematical functions

+abs(value)    ; 절대값. 데이터 타입은 int, long, long long, float을 지원한다.

타입을 명시한 함수도 있다. fabs(float), labs(long) , llabs(long long) (C++11)


+div(x, y)    ; 몫과 나머지를 구한다.

std::div_t div(int x, int y) ; 데이터 타입은 long, long long도 지원. ldiv(), lldiv() 사용.

struct div_t { int quot; int rem; }    ; 리턴값인 div_t 타입은 몫과 나머지 필드가 있다.

itoa 함수 구현 예제

std::string itoa(int n, int base) {

   std::string buf ;

   std::div_t dv{} ; 

   dv.quot = n ;

   do {

      dv = std::div(dv.quot, base) ;

      buf += "0123456789abcdef"[std::abs(dv.rem)] ;

   } while (dv.quot) ;

   if (n<0) buf +='-' ;

   return { buf.rbegin(), buf.rend() } ;

}


+큰값, 작은값

max(a,b), fmax(a,b) 등. max는 int, long, long long, fmax는 float, double, long double

min(a,b), fmin(a,b) 등


+오차

fdim(a,b) ; a-b 값이 0보다 크면 a-b값을 리턴. 그렇지 않으면 0리턴. (dim 은 없다. fdim 사용 )


+지수, 로그

exp(arg)    ; 자연상수 e의 arg승수 값. e^arg.  타입은 float, double, long double

exp2(arg)    ; 2^arg  (C++11)

log (arg)   ;   ln(arg) 자연로그

log10(arg)  ; 밑이 10인 로그

pow(x, y)    ; x^y 

sqrt(x)    ; root(x)


+삼각함수

sin, cos, tan, asin, acos, atan

const double pi = std::acos(-1) ;

std::sin( pi/6 )    --> 0.5

std::sin( pi/2 )    --> 1

2 * std::atan( INFINITY )    --> 3.14159 (pi)


atan2(y, x)    ; atan ( y/x ),  -pi~pi 범위의 리턴값


hyperbolic 삼각함수 ; sinh, cosh, tanh, asinh, acosh, atanh 

sinh(arg) ;   ( e^arg - e^-arg ) / 2

cosh(arg) ;  ( e^arg + e^-arg ) / 2

tanh(arg) ;  sinh / cosh


+ 가까운 정수

ceil ; 올림

floor ; 내림

round ; 반올림 (C++11)

float round(float arg) ; double round (double arg) ; ... long double 등.











'Develop > C&CPP' 카테고리의 다른 글

libcurl HTTP POST send 모듈/curl 커맨드 방식도 지원  (0) 2018.07.13
if or 비교시 어떤 식의 성공으로 진입했는지 구분  (0) 2018.07.13
Preprocessor  (0) 2018.06.20
deque  (0) 2018.06.13
stack / queue  (0) 2018.06.12

+ Recent posts