浮点问题用C [英] Floating point issue in C

查看:108
本文介绍了浮点问题用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1839422/strange-output-in-comparision-of-float-with-float-literal\">strange在浮动的比较输出浮动文字


 浮动= 0.7;
如果(一个或下; 0.7);

为什么这里的前pression评估为真?


解决方案

浮点数限制了precision。 0.7最有可能不能准确地重新presented,所以在该值可能是0.6999999999982左右的浮子。此相比,双0.7(这是更precise:0.6999999999999999999999999384)将表明,它是以下

检查了这一点:<一href=\"http://docs.sun.com/source/806-3568/ncg_goldberg.html\">http://docs.sun.com/source/806-3568/ncg_goldberg.html

Possible Duplicate:
strange output in comparision of float with float literal

float a = 0.7;
if (a < 0.7) ;

Why does the expression here evaluate to true?

解决方案

Floating point numbers have limited precision. 0.7 most likely can't be exactly represented, so the value in a might be 0.6999999999982 or so in a float. This compared to a double 0.7 (which is more precise: 0.6999999999999999999999999384) will show that it is less.

Check this out: http://docs.sun.com/source/806-3568/ncg_goldberg.html

这篇关于浮点问题用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆