473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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)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
Jul 22 '05 #1
10 4785
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

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)reference_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**********@pluto.srv.dsi.unimi.it>,
Mr Doppino <mr*******@NOSPAMEHEH.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)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


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(reference_cross.x), double(reference_cross.y));

with:

fprintf(stderr,"[%f|%f] - cross(%f|%f) - diff(%e|%e)\n",nodex, nodey,
double(reference_cross.x), double(reference_cross.y),
nodex - double(reference_cross.x),
nodey - double(reference_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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - 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
Mr Doppino wrote:
Hi people,
I would like to submit a problem about double comparison. Here's the
code:


You may find these links helpful:
http://www.petebecker.com/js200006.html
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Regards,
Sumit
--
Sumit Rajan <su*********@gmail.com>
Jul 22 '05 #11

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

Similar topics

1
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): <%="|" &...
2
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...
6
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
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...
6
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...
44
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,...
1
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...
18
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? ,...
13
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.