C++/coding skills

[C++] 쓰레기값 거르기(아주 작은값) numeric_limits

화이팅입미다 2020. 12. 4. 16:38

http://https://docs.microsoft.com/ko-kr/cpp/standard-library/numeric-limits-class?view=msvc-160#epsilon

 

 

numeric_limits 클래스

구문

template <class Type>
    class numeric_limits

 

epsilon

함수는 1과 데이터 형식에 대해 나타낼 수 있는 1보다 큰 가장 작은 값 사이의 차이를 반환

 

예제

// numeric_limits_epsilon.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for float objects is: "
        << numeric_limits<float>::epsilon( ) << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for double objects is: "
        << numeric_limits<double>::epsilon( ) << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1" << endl
        << "for long double objects is: "
        << numeric_limits<long double>::epsilon( ) << endl;
}

 

output

The difference between 1 and the smallest value greater than 1
for float objects is: 1.19209e-007
The difference between 1 and the smallest value greater than 1
for double objects is: 2.22045e-016
The difference between 1 and the smallest value greater than 1
for long double objects is: 2.22045e-016

 

 

사용 예시

float f_tmp;
if(fabsf(f_tmp) < std::numeric_limits<float>::epsilon()) return;

이런식으로 쓰레기값을 거를 수 있다