判断浮点数溢出的方法

2011-03-19 wcdj

 

方法1:判断是否溢出,一是使用函数判断,二是与最大最小值比较

#include #include #include int main() { // double类型 double dval1 = 1.2; double dval2 = 1.2*pow((double)10,308); double dval3 = 1.2*pow((double)10,309); printf("%d/n",_finite(dval1));// 1 未溢出 printf("%d/n",_finite(dval2));// 1 printf("%d/n",_finite(dval3));// 0 溢出 if (dval1 > DBL_MAX || dval1 < DBL_MIN) printf("overflow/n"); if (dval2 > DBL_MAX || dval2 < DBL_MIN) printf("overflow/n"); if (dval3 > DBL_MAX || dval3 < DBL_MIN) printf("overflow/n"); // float类型 float fval1 = 1.2f; float fval2 = 1.2f*(float)pow((double)10,38); float fval3 = 1.2f*(float)pow((double)10,39); printf("%d/n",_finite(fval1));// 1 未溢出 printf("%d/n",_finite(fval2));// 1 printf("%d/n",_finite(fval3));// 0 溢出 if (fval1 > FLT_MAX || fval1 < FLT_MIN) printf("overflow/n"); if (fval2 > FLT_MAX || fval2 < FLT_MIN) printf("overflow/n"); if (fval3 > FLT_MAX || fval3 < FLT_MIN) printf("overflow/n"); return 0; }

 

#include #include using namespace std; int main() { float f = 0.f; float d = -100.f / f;// -1.#INF000 if (d > FLT_MAX || d < FLT_MIN) cout<<"error"< FLT_MAX || x < FLT_MIN) cout<<"error"<

 

方法2:捕获浮点数溢出异常

#include #include #include #include #include #include int fpieee_handler(_FPIEEE_RECORD *pieee) { if (pieee->Cause.Overflow == 1) {// 处理溢出 return EXCEPTION_CONTINUE_EXECUTION; } else { return EXCEPTION_EXECUTE_HANDLER; } } int main() { __try { unsigned int u; unsigned int control_word; _controlfp_s(&control_word, 0, 0); u = control_word & (~_EM_OVERFLOW); _controlfp_s( &control_word, u, _MCW_EM); // 你的代码 // double类型 double dval3 = 1.2*pow((double)10,309); printf("%d/n",_finite(dval3));// 0 溢出=> 1 if (dval3 > DBL_MAX || dval3 < DBL_MIN) printf("overflow/n"); // float类型 float fval3 = 1.2f*(float)pow((double)10,39); printf("%d/n",_finite(fval3));// 0 溢出=> 1 if (fval3 > FLT_MAX || fval3 < FLT_MIN) printf("overflow/n"); } __except (_fpieee_flt(GetExceptionCode(), GetExceptionInformation(), fpieee_handler)){ } }

 

float.h文件中定义的一些常量:

... #define DBL_DIG 15 /* # of decimal digits of precision */ #define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */ #define DBL_MANT_DIG 53 /* # of bits in mantissa */ #define DBL_MAX 1.7976931348623158e+308 /* max value */ #define DBL_MAX_10_EXP 308 /* max decimal exponent */ #define DBL_MAX_EXP 1024 /* max binary exponent */ #define DBL_MIN 2.2250738585072014e-308 /* min positive value */ #define DBL_MIN_10_EXP (-307) /* min decimal exponent */ #define DBL_MIN_EXP (-1021) /* min binary exponent */ #define _DBL_RADIX 2 /* exponent radix */ #define _DBL_ROUNDS 1 /* addition rounding: near */ #define FLT_DIG 6 /* # of decimal digits of precision */ #define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */ #define FLT_GUARD 0 #define FLT_MANT_DIG 24 /* # of bits in mantissa */ #define FLT_MAX 3.402823466e+38F /* max value */ #define FLT_MAX_10_EXP 38 /* max decimal exponent */ #define FLT_MAX_EXP 128 /* max binary exponent */ #define FLT_MIN 1.175494351e-38F /* min positive value */ #define FLT_MIN_10_EXP (-37) /* min decimal exponent */ #define FLT_MIN_EXP (-125) /* min binary exponent */ #define FLT_NORMALIZE 0 #define FLT_RADIX 2 /* exponent radix */ #define FLT_ROUNDS 1 /* addition rounding: near */ ...

 

参考:
float浮点数的溢出处理
如何判断浮点数计算溢出

 

 

 

 

你可能感兴趣的:(C/C++)