Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 基础问题,请教! (比较float是不是要设置误差)
liufancjsdn





发贴: 55
积分: 0
于 2003-04-02 11:35 user profilesend a private message to usersend email to liufancjsdnsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
float型的数据比较,是不是也要设置一个误差,然后再比较
如 : if ( abs(f1 - f2) <= xxx );
而不能直接 if ( f1 == f2 )


why edited on 2003-06-22 00:17


作者 Re:基础问题,请教! [Re:liufancjsdn]
diystar





发贴: 57
积分: 20
于 2003-04-02 14:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好像无此必要


作者 Re:基础问题,请教! [Re:liufancjsdn]
Julian_zhu



发贴: 0
积分: 0
于 2003-04-02 17:36 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没有必要!!!SmileSmile


作者 Re:基础问题,请教! [Re:liufancjsdn]
liufancjsdn





发贴: 55
积分: 0
于 2003-04-02 17:38 user profilesend a private message to usersend email to liufancjsdnsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没有必要吗?

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

if (f1*3 == f2) 为false




作者 Re:基础问题,请教! [Re:liufancjsdn]
jiangns3000



发贴: 0
积分: 0
于 2003-04-02 18:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liufancjsdn wrote:
float型的数据比较,是不是也要设置一个误差,然后再比较
如 : if ( abs(f1 - f2) <= xxx );
而不能直接 if ( f1 == f2 )

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



作者 Re:基础问题,请教! [Re:liufancjsdn]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-04-03 00:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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