双重比较问题 [英] Double comparison problem

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

问题描述

大家好,

我想提交一个关于双重比较的问题。这是代码:


struct Cross_point {

int cross_id;

int x,y;

int street_oriz,street_vert;

};


------------------- -------------

double nodex,nodey;

Cross_point reference_cross;


* **

//这里有对nodex,nodey和reference_cross的赋值

***


if(nodex = =((double)reference_cross.x))

move_dir = 1;

else if(nodey ==((double)reference_cross.y))

move_dir = 2;

else

fprintf(stderr," [%f |%f] - cross(%f |%f)\ n" ,nodex,nodey,

double(reference_cross.x),double(reference_cross.y));

-------------- -----------


问题是有时即使nodex的值和

reference_cross.x或nodey和reference_cross .y和我收到的

错误消息一样。印刷显示价值相等。


这是我得到的输出:


[527.000000 | 601.232221] - 交叉(527.000000) | 627.000000)

[527.000000 | 625.344833] - cross(527.000000 | 627.000000)

[527.000000 | 605.427903] - cross(527.000000 | 627.000000)

[632.289882 | 627.000000] - 交叉(622.000000 | 627.000000)

[639.335527 | 627.000000] - 交叉(622.000000 | 627.000000)

[650.641926 | 627.000000] - 交叉(660.000000) | 627.000000)

[658.736125 | 627.000000] - 交叉(660.000000 | 627.000000)


谢谢


Frank

解决方案

Doppino先生写道:

问题是有时即使nodex的值和
reference_cross .x OR nodey和reference_cross.y是相同的我得到
的错误信息。印刷显示价值相等。




你呢,现在?也许那是因为你没有调查什么

浮点数是什么,并没有意识到浮动

点数值通常不会相等,即使它们是

应包含相同的值。因此,为了检测漂浮的

点值是否相等,你会使用某种形式的epsilon。


打印显示值相等的事实;是因为在打印例程中难以舍入以获得预期的结果。价值

典型案例。例如,用户希望看到0.1。甚至

虽然这个值无法使用浮动的

积分来表示,其基数为2.

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发&咨询


Dietmar Kuehl写道:


打印显示值相等的事实是因为在打印程序中难以舍入以获得预期的。在典型情况下的价值观。例如,用户希望看到0.1。甚至
虽然这个值无法用一个基数为2的浮动点来准确表示。




那没关系,但是并没有解释OP看到的是什么。

601.233331和627.00与此解释相比有很大不同

适用。


话虽如此:我对此行为没有任何解释。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Karl Heinz Buchegger写道:


Dietmar Kuehl写道:


打印显示值相等的事实是因为在打印程序中难以舍入以获得预期的。在典型情况下的价值观。例如,用户希望看到0.1。甚至
虽然这个值不能用一个基数为2的漂浮
点来准确表示。



那没关系,但不是解释OP正在看到的内容。
601.233331和627.00与此解释相同。

话虽如此:我对此行为没有任何解释。




Appologies。你太对了。我误读了OP的帖子。

y分量的差异不是问题。

问题在x组件中应该是相等的但是

不是因为浮点问题。

正是你所描述的。


-

Karl Heinz Buchegger
kb******@gascad.at


Hi people,
I would like to submit a problem about double comparison. Here''s the code:

struct Cross_point {
int cross_id;
int x, y;
int street_oriz, street_vert;
};

--------------------------------
double nodex, nodey;
Cross_point reference_cross;

***
// Here there are assignments to nodex, nodey, and reference_cross
***

if (nodex == ((double)reference_cross.x))
move_dir = 1;
else if (nodey == ((double)reference_cross.y))
move_dir = 2;
else
fprintf(stderr,"[%f|%f] - cross(%f|%f)\n",nodex, nodey,
double(reference_cross.x), double(reference_cross.y));
-------------------------

The problem is that sometimes even though the values of nodex and
reference_cross.x OR nodey and reference_cross.y are the same I get the
error message. The printing show the values are equal.

Here''s the output i get:

[527.000000|601.232221] - cross(527.000000|627.000000)
[527.000000|625.344833] - cross(527.000000|627.000000)
[527.000000|605.427903] - cross(527.000000|627.000000)
[632.289882|627.000000] - cross(622.000000|627.000000)
[639.335527|627.000000] - cross(622.000000|627.000000)
[650.641926|627.000000] - cross(660.000000|627.000000)
[658.736125|627.000000] - cross(660.000000|627.000000)

Thank you

Frank

解决方案

Mr Doppino wrote:

The problem is that sometimes even though the values of nodex and
reference_cross.x OR nodey and reference_cross.y are the same I get the error message. The printing show the values are equal.



Do you, now? Maybe that is because you failed to investigate what
floating point is all about and failed to realize that floating
point values typically do not compare equal even though they
should contain the same value. Thus, to detect whether floating
point values are equal, you would use some form of epsilon.

The fact that "printing show the values are equal" is due to
tricky rounding in printing routines to get "expected" values
in typical cases. For example, users want to see "0.1" even
though this value cannot be represented exactly using a floating
point with a base of 2.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


Dietmar Kuehl wrote:


The fact that "printing show the values are equal" is due to
tricky rounding in printing routines to get "expected" values
in typical cases. For example, users want to see "0.1" even
though this value cannot be represented exactly using a floating
point with a base of 2.



That''s all right, but doesn''t explain what the OP is seeing.
601.233331 and 627.00 are far to different that this explanation
applies.

Having said that: I have no explanation for that behaviour.

--
Karl Heinz Buchegger
kb******@gascad.at


Karl Heinz Buchegger wrote:


Dietmar Kuehl wrote:


The fact that "printing show the values are equal" is due to
tricky rounding in printing routines to get "expected" values
in typical cases. For example, users want to see "0.1" even
though this value cannot be represented exactly using a floating
point with a base of 2.



That''s all right, but doesn''t explain what the OP is seeing.
601.233331 and 627.00 are far to different that this explanation
applies.

Having said that: I have no explanation for that behaviour.



Appologies. You are quite right. I misread the OP''s post.
The difference in the y component isn''t the problem. The
problem is in the x component which should be equal but
aren''t because of floating point problems.
Exactly what you describe.

--
Karl Heinz Buchegger
kb******@gascad.at


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

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