Topic: 基础问题,请教! (比较float是不是要设置误差)

  Print this page

1.基础问题,请教! (比较float是不是要设置误差) Copy to clipboard
Posted by: liufancjsdn
Posted on: 2003-04-02 11:35

float型的数据比较,是不是也要设置一个误差,然后再比较
如 : if ( abs(f1 - f2) <= xxx );
而不能直接 if ( f1 == f2 )

2.Re:基础问题,请教! [Re: liufancjsdn] Copy to clipboard
Posted by: diystar
Posted on: 2003-04-02 14:28

好像无此必要

3.Re:基础问题,请教! [Re: liufancjsdn] Copy to clipboard
Posted by: Julian_zhu
Posted on: 2003-04-02 17:36

没有必要!!!SmileSmile

4.Re:基础问题,请教! [Re: liufancjsdn] Copy to clipboard
Posted by: liufancjsdn
Posted on: 2003-04-02 17:38

没有必要吗?

float f1 = 1.0/3 ;
float f2 = 1.0 ;

if (f1*3 == f2) 为false

5.Re:基础问题,请教! [Re: liufancjsdn] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-02 18:37

liufancjsdn wrote:
float型的数据比较,是不是也要设置一个误差,然后再比较
如 : if ( abs(f1 - f2) <= xxx );
而不能直接 if ( f1 == f2 )

这是个老问题了,怎么还有疑问。(还有同胞却认为不必。)无论是float还是double,都不能用==表示数学上的相等。如果不是金融或银行等要求很高的情况(这要专门处理),在你的具体应用的合理范围内用 if ( abs(f1 - f2) <= xxx );比较。

6.Re:基础问题,请教! [Re: liufancjsdn] Copy to clipboard
Posted by: floater
Posted on: 2003-04-03 00:37

You need to safe guard agains machine round-off errors. These errors are invisible and ignorable. But when they are accumulated, they could eat up your result. One example is the weather forecasting system, you know how good it is on everydays television, Big Smile.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923