Life: research & education/Research

C언어 math.h에서 파이값 가져오는 방법

KMS studio 2021. 5. 15. 13:00

이번주에도 간단하게 C언어 파이값을 비롯해, 자연상수, 자연상수 로그값, 자연소그부터 루트 파이와 2 분의 파이 등 다양한 상수값?들을 알아내는 방법을 알아봅시다.

이 방법은 개인적으로 알면 별거 아닌데, 모르고있다 알게되면 '아...'하게 되는 내용입니다.

참고로 비주얼 스튜디오를 기준으로 설명드리는 겁니다.  gcc에서는 높은 확률로 작동이 안될겁니다.

+ 2주연속 간단한 내용이라 죄송합니다. 다음주에는 NIST에서 권장하는 난수 판독기로 돌아오겠습니다


코드

#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
//이제 여러분은 이 소스에서 math.h에서 지원하는 모든 매크로(define)들을 사용할 수 있습니다.

int main(void) {
	return 0;
}

 

사용가능한 매크로들 (발췌 : mathcorecrt_math_defines.h Copyright (c) Microsoft Corporation. All rights reserved.)

참고로 sqrt(x)는 루트 x를 뜻합니다. e는 자연상수 e고요.

// 이 매크로를 사용하기 위해서 _USE_MATH_DEFINES를 including <math.h> 하기 전에 define하세요
// 자주 사용되는 수학 정의들 (생략)
// since these commonly-defined names are not part of the C or C++ standards
    #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)

원리

긴 설명 안드리겠습니다. 초보자도 헤더파일만 보면 쉽게 알 수 있는 내용이니까요.

간단하게 math.h. 라이브러리만 소개해드리도록 하겠습니다.

 

visual studio의 math.h, corecrt_math_defines.h본문

(발췌 : mathcorecrt_math_defines.h Copyright (c) Microsoft Corporation. All rights reserved.)

더보기

math.h

//
// math.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C Standard Library <math.h> header.  This header consists of two parts:
// <corecrt_math.h> contains the math library; <corecrt_math_defines.h> contains
// the nonstandard but useful constant definitions.  The headers are divided in
// this way for modularity (to support the C++ modules feature).
//
#include <corecrt_math.h>

#ifdef _USE_MATH_DEFINES
    #include <corecrt_math_defines.h>
#endif

corecrt_math_defines.h

//
// corecrt_math_defines.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// Definitions of useful mathematical constants
//
#pragma once

#ifndef _MATH_DEFINES_DEFINED
    #define _MATH_DEFINES_DEFINED
    // Definitions of useful mathematical constants
    //
    // 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 or C++ standards
    #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)
#endif

corecrt_math.h는 math.h의 함수들이 포함되어있는 복잡한 헤더고, 이 글에서 사용되지도 않아서 생략했습니다.


활용

이 방법으로 평소에 일일히 #define으로 지정해 주었던 상수들을 20자리까지, 디테일하게 알아내고, 사용할 수 있습니다.

파이값이나, 자연상수가 사용되는 모든 프로그램에 활용하면 굉장히 유익하죠.

 

또한 루트 파이 값이나 로그2e값 또한 포함하고 있어 이런 값들을 많이 사용하실때 특히 더 유용하실 거라 생각합니다.