首页 > 其他 > 详细

浮点数比较

时间:2014-08-22 16:32:59      阅读:361      评论:0      收藏:0      [点我收藏+]
参照MSDN定义:
/* Compile options needed: none. Value of c is printed with a decimal
point precision of 10 and 6 (printf rounded value by default) to
show the difference
*/
#include 
// Define your own tolerance
const double EPSILON = 1.00e-07;
const float   FLT_EPSILON  = 1.192092896e-07F;
const double  DBL_EPSILON  = 2.2204460492503131e-016;

#define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON)))

int main()
{
float a, b, c;
a = 1.345f;
b = 1.123f;
c = a + b;
// if (FLOAT_EQ(c, 2.468))   // Remove comment for correct result
if (c == 2.468)              // Comment this line for correct result
printf("They are equal.\n");
else
printf("They are not equal! The value of c is %13.10f,or %f",c,c);
}


浮点数比较

原文:http://my.oschina.net/u/572632/blog/305603

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!