由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - double's equality
相关主题
inline functions in C++在看the effective C++language
A tech question (转载)static variable in template header
inline C function in VC++ 2005 express基础问题:在header里面define function
有什么办法可以查每行代码用的时间?inline到底能省多少时间?
interview question: make all class member functions virtual (转载)请问C++返回值和返回引用区别
[合集] Inline member function in C++C++方法全都内联有什么坏处?
请教问题请教C++里面这个表达
[合集] three worst thing about C++(题目)c++中的inline 函数是做什么的?
相关话题的讨论汇总
话题: std话题: abs话题: epsilon话题: double话题: equality
进入Programming版参与讨论
1 (共1页)
h*****f
发帖数: 248
1
C++ FAQ建议用以下的code來test whether 2 doubles are equal
#include /* for std::abs(double) */
inline bool isEqual(double x, double y)
{
const double epsilon = /* some small number such as 1e-5 */;
return std::abs(x - y) <= epsilon * std::abs(x);
// see Knuth section 4.2.2 pages 217-218
}
想问为什么要用:
return std::abs(x - y) <= epsilon * std::abs(x);
而不是用:
return std::abs(x - y) <= epsilon;
Knuth section 4.2.2提到
std::abs(x - y) <= epsilon * std::abs(x) || std::abs(x - y) <= epsilon * std
::abs(y)
沒有
std::abs(x - y) <= epsilon * std::abs(x) && std::abs(x - y) <= epsilon * std
::abs(y)
strong.
可是我沒看懂....
t****t
发帖数: 6806
2
这不是挺明白的吗? 绝对误差和相对误差的关系. 哪儿不明白呢?

【在 h*****f 的大作中提到】
: C++ FAQ建议用以下的code來test whether 2 doubles are equal
: #include /* for std::abs(double) */
: inline bool isEqual(double x, double y)
: {
: const double epsilon = /* some small number such as 1e-5 */;
: return std::abs(x - y) <= epsilon * std::abs(x);
: // see Knuth section 4.2.2 pages 217-218
: }
: 想问为什么要用:
: return std::abs(x - y) <= epsilon * std::abs(x);

a****l
发帖数: 8211
3
其实我觉得这个faq写的不完全对。两个浮点数相同的的话就是说二进制数是相同,这是
没有任何疑问的。不能直接比较的原因是计算有误差,就是说数学上应该相同的计算结
果计算机出来的可能是不同的,数学上应该是不同的计算结果计算机出来的可能是相同
的二进制数。所以这是一个“应用”的问题,而不是一个“语言”的问题。if (x == y)
本身没有任何问题而且是完全合理的,关键是使用的人要知道这到底是什么意思。两个
浮点数能否比较不应该是c语言应该考虑的问题,而是数值计算应该考虑的问题,而且数
值计算本身就是一个非常复杂的学科,是否相等是里面一个非常非常简单的入门问题.
http://www.cygnus-software.com/papers/comparingfloats/comparing
举个例子,如果要计算a/(b-c),怎么做?
float sample(float a, float b, float c)
{ if ( b==c ) {oops..} else return a/(b-c);}
当然还有其他的问题,但是这是数值计算里面讨论的了.

【在 h*****f 的大作中提到】
: C++ FAQ建议用以下的code來test whether 2 doubles are equal
: #include /* for std::abs(double) */
: inline bool isEqual(double x, double y)
: {
: const double epsilon = /* some small number such as 1e-5 */;
: return std::abs(x - y) <= epsilon * std::abs(x);
: // see Knuth section 4.2.2 pages 217-218
: }
: 想问为什么要用:
: return std::abs(x - y) <= epsilon * std::abs(x);

h*****f
发帖数: 248
4
Obviously, I wasn't awake.
Thanks for pointing out! :)

【在 t****t 的大作中提到】
: 这不是挺明白的吗? 绝对误差和相对误差的关系. 哪儿不明白呢?
1 (共1页)
进入Programming版参与讨论
相关主题
c++中的inline 函数是做什么的?interview question: make all class member functions virtual (转载)
C编译器为何允许一个函数在某个路径不返回值?[合集] Inline member function in C++
override operator[] inline请教问题
C++11的lambda不会破坏可读性吗?[合集] three worst thing about C++(题目)
inline functions in C++在看the effective C++language
A tech question (转载)static variable in template header
inline C function in VC++ 2005 express基础问题:在header里面define function
有什么办法可以查每行代码用的时间?inline到底能省多少时间?
相关话题的讨论汇总
话题: std话题: abs话题: epsilon话题: double话题: equality