본문 바로가기

C, C++, C#/Tip

C++ double Infinity, -nan(ind) 예외 처리 방법

반응형

안녕하세요. 이번 포스팅은 C++ double의 Infinity, -nan(ind) 예외 처리 포스팅입니다.

 

제 스스로 메모를 위한 포스팅입니다.

 

실수값인지 체크하는 함수입니다.

        #include <cmath>

        //! double 값의 쓰레기값 여부를 확인
        //return 	- ture : 실수값
        //			- flase : 쓰레기값
        bool IsTrashValue(double x)
        {
            int i = _fpclass(x);
            if (
            i == _FPCLASS_SNAN 		|| //Signaling NaN";          
            i == _FPCLASS_QNAN 		|| //Quiet NaN";                  
            i == _FPCLASS_NINF 		|| //Negative infinity (-INF)";   
            //i == _FPCLASS_NN 		|| //Negative normalized non-zero"
            i == _FPCLASS_ND 		|| //Negative denormalized";      
            //i == _FPCLASS_NZ 		|| //Negative zero (-0)";         
            //i == _FPCLASS_PZ 		|| //Positive 0 (+0)";            
            i == _FPCLASS_PD 		|| //Positive denormalized";      
            //i == _FPCLASS_PN 		|| //Positive normalized non-zero"
            i == _FPCLASS_PINF 		|| //Positive infinity (+INF)";     
            )
            {
            	return false;
            }
            return true;
        }

 

 

숫자인지 확인하는 소스입니다.

    bool IsNumber(double x) 
    {
        // This looks like it should always be true, 
        // but it's false if x is a NaN.
        return (x == x); 
    }

 

실수 허용 최대값을 확인합니다.

    bool IsFiniteNumber(double x) 
    {
        return (x <= DBL_MAX && x >= -DBL_MAX); 
    }    

 

 

 

 

 

출처

https://www.johndcook.com/blog/IEEE_exceptions_in_cpp/

 

반응형