473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double comparison problem

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)refere nce_cross.x))
move_dir = 1;
else if (nodey == ((double)refere nce_cross.y))
move_dir = 2;
else
fprintf(stderr, "[%f|%f] - cross(%f|%f)\n" ,nodex, nodey,
double(referenc e_cross.x), double(referenc e_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.00000 0|627.000000)
[527.000000|625. 344833] - cross(527.00000 0|627.000000)
[527.000000|605. 427903] - cross(527.00000 0|627.000000)
[632.289882|627. 000000] - cross(622.00000 0|627.000000)
[639.335527|627. 000000] - cross(622.00000 0|627.000000)
[650.641926|627. 000000] - cross(660.00000 0|627.000000)
[658.736125|627. 000000] - cross(660.00000 0|627.000000)

Thank you

Frank
Jul 22 '05 #1
10 4796
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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #2
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
Jul 22 '05 #3
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
Jul 22 '05 #4
Mr Doppino wrote:
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)refere nce_cross.x))


There are two groups of people in this world.

1) Those who don't fully understand all of the details about performing
== and != on floating points values. They should never use == or != on
floating point values (not being mean or anything, they just shouldn't).

2) Those who do understand all the details about performing == and != on
floating point values. They don't use == and !=, so don't have to be
told not to :)

Chris
Jul 22 '05 #5
In article <cs**********@p luto.srv.dsi.un imi.it>,
Mr Doppino <mr*******@NOSP AMEHEH.it> wrote:
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)refere nce_cross.x))
move_dir = 1;
else if (nodey == ((double)refere nce_cross.y))
move_dir = 2;
else
fprintf(stderr, "[%f|%f] - cross(%f|%f)\n" ,nodex, nodey,
double(referen ce_cross.x), double(referenc e_cross.y));
-------------------------

The problem is that sometimes even though the values of nodex and
reference_cros s.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.00000 0|627.000000)
[527.000000|625. 344833] - cross(527.00000 0|627.000000)
[527.000000|605. 427903] - cross(527.00000 0|627.000000)
[632.289882|627. 000000] - cross(622.00000 0|627.000000)
[639.335527|627. 000000] - cross(622.00000 0|627.000000)
[650.641926|627. 000000] - cross(660.00000 0|627.000000)
[658.736125|627. 000000] - cross(660.00000 0|627.000000)

Thank you

Frank


Sorry, but they're not equal. In fact in any program where floating point
numbers are used attempting to compare for equality *or* inequality is
usually a mistake. Replace your line:

fprintf(stderr, "[%f|%f] - cross(%f|%f)\n" ,nodex, nodey,
double(referenc e_cross.x), double(referenc e_cross.y));

with:

fprintf(stderr, "[%f|%f] - cross(%f|%f) - diff(%e|%e)\n", nodex, nodey,
double(referenc e_cross.x), double(referenc e_cross.y),
nodex - double(referenc e_cross.x),
nodey - double(referenc e_cross.y));

to see what's going on.

Hope this helps,
John Cochran
Jul 22 '05 #6
chris wrote:
There are two groups of people in this world.

1) Those who don't fully understand all of the details about performing == and != on floating points values. They should never use == or != on floating point values (not being mean or anything, they just shouldn't).
2) Those who do understand all the details about performing == and != on floating point values. They don't use == and !=, so don't have to be
told not to :)


I'd say you fall into the first group! After all, comparing floating
point values representing reasonably sized integers is perfectly
valid even using '==' or '!=': you can use floating point values as a
form of bigger integer. You just have to take care of the values
staying in a certain range. Actually, you can also do exact
computations
for other values, i.e. those which can represented exactly. The only
thing to note is that the set of useful values is not closed over the
operations and you need to realize when the result of the operation is
of different kind (e.g. when addition or multiplication overflows,
substraction cancels leading bits, and division is only valid for
certain value). Sure enough, for most of my uses I don't play tricks
with floating point values but it is still possible and if you e.g.
need a 64 bit integer on a platform not supporting some form of
"long long" it can be an appropriate choice to use a 'long double'.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #7
Dietmar Kuehl wrote:
chris wrote:
There are two groups of people in this world.

1) Those who don't fully understand all of the details about


performing
== and != on floating points values. They should never use == or !=


on
floating point values (not being mean or anything, they just


shouldn't).
2) Those who do understand all the details about performing == and !=


on
floating point values. They don't use == and !=, so don't have to be
told not to :)

I'd say you fall into the first group! After all, comparing floating
point values representing reasonably sized integers is perfectly
valid even using '==' or '!=': you can use floating point values as a
form of bigger integer. You just have to take care of the values
staying in a certain range. Actually, you can also do exact
computations
for other values, i.e. those which can represented exactly. The only
thing to note is that the set of useful values is not closed over the
operations and you need to realize when the result of the operation is
of different kind (e.g. when addition or multiplication overflows,
substraction cancels leading bits, and division is only valid for
certain value). Sure enough, for most of my uses I don't play tricks
with floating point values but it is still possible and if you e.g.
need a 64 bit integer on a platform not supporting some form of
"long long" it can be an appropriate choice to use a 'long double'.

OK, I apologise, I was perhaps a little too strong :) There are cases
where == and != can be used safely as long as you stay within some
reasonably tight bounds, as long as are careful :) But like you say you
have to be careful, so I find it easier to scare people off using == and
!= altogether, because I've seen far, far more cases where they were
being used incorrectly, and only a handful where they were being used
correctly, and in those cases the people using them knew they were using
them correctly :)

The idea of using a long double to replace a long long wasn't one I'd
seen before, thats quite a neat idea (although once again requires being
reasonably careful).

Chris
Jul 22 '05 #8

"chris" <ca*@cs.york.ac .uk> wrote in message
news:41******** ******@cs.york. ac.uk...

There are two groups of people in this world.


Actually, there are THREE groups of people in this world:

1) those who can count, and

2) those who can't

:-)
Jul 22 '05 #9
Howard wrote:
Actually, there are THREE groups of people in this world:


There are 10 goups:

- People that knows the binary system.

- Other people.

--
Salu2
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2661
by: sqlvs | last post by:
Good citizens of Gotham, I'm encountering an odd problem when comparing two values converted to type double, as follows (please pardon the per-line script blocks): <%="|" & CDbl(rsBatch("BatchTotal")) & "|" %><br> <%="|" & CDbl(rsBatch("BatchPayments")) & "|" %><br> <%= CDbl(rsBatch("BatchTotal")) = CDbl(rsBatch("BatchPayments")) %><br>...
2
15163
by: Dave | last post by:
Hello all, I would like to solicit suggestions on how to most efficiently accomplish something. I have profiled the project I'm working on and have found that calls to fabs() are taking a very substantial amount of time. fabs() is being used in the following manner: double a, b; a = get_a_value();
6
4679
by: Barry | last post by:
The following code compiled using MSVC++6.0 - double di=0.0; for(;di<=512.0;) { string.Format("%li",int(di)); pDC-> TextOut( left +int(di), 100,string); di += 51.2; }
10
7240
by: chandra.somesh | last post by:
Hi I recently had to write a small code in a competition ,but my code was rejected cause it failed in 1 of test cases. The problm was .....we are given vector of strings....each string consists of either 1 or 2("12122" 0r "2121" so on..)...i had to find the that string where percentage of '1' is minimum.Now the problem and solution both...
6
11346
by: Jesper, DK. | last post by:
A little preamble A year ago I had a very odd bug in my program, in an if clause it picked and executed the else statement even though the if clause should evaluate to true. Normally you know that it is you who are doing something wrong, but not this time. Contacting microsoft and providing them with the code, they informed me that there was a...
44
16285
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b, "Built-in == operator should not be
1
2860
by: Jay Hamilton | last post by:
Hello, I am running into an invalid address alignment error on an HPUX box when I attempt to lookup a value in a STL map. The argument being passed in is a double, which is accessed from a structure; the map's key is also a double. The issue seemed to be occuring in the map's default comparison function so I implemented my own comparison...
18
399
by: brekehan | last post by:
I ran across some code that is initializing a double: double x = 1e-5; Does the standard dictate the compiler to interpret that as 0.00001? or is the above representation error prone? , Chris
13
3127
by: =?Utf-8?B?U3RlZmFuRw==?= | last post by:
With the test I've made on my WinXP/VS2005/C#2.0, I get a precision of 42bit. Is this specified in C# or dependent on what? double dLeft = 2183.23 - 695.37; double dRight = 1487.86; double dFraction = Math.Pow(2, -42); if (Math.Abs(dLeft - dRight) == dFraction) { System.Console.WriteLine("Success"); } // Output: Success
0
7546
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.