Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
People, i think my VC++ 6.0 debugger has gone crazy.
I have two float arrays:
float c1[2], c2[2];
In the watch window, I have the following:
NAME VALUE
c1[1] 0.285714
c2[1] 0.285714
c1[1] == c2[1] 0 <-***** Isn't that Crazy
c1[1] != c2[1] 1 <-***** Isn't that Crazy
I hope that someone comes with a solution and convince me that i am crazy(because debuggers can never be), and this problem gets solved.
Thanks
[572 byte] By [
madhur] at [2007-11-18 19:16:08]

# 1 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
I think your debugger in Not Crazy!
Probably there is a rounding problem :cool:
Try to obtain the result of c1[1] - c2[1]. I think it should be less than 0.000005
# 2 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
try the comparison after type casting to double(or use double in the declaration). It should solve. this is a C programming issue.
double(c1[1]) == double(c2[1])
should give 1
# 3 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
double(c1[1]) == double(c2[1])
should give 1Why are you sure "it should"?
It depends on "How these both values (c1[1]), c2[1] ) were got".
in case of .....
c2[1] = c2[1];
....
- yes!
Otherwise - :confused:
# 4 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
Subtracting shows the bug, but casting doesn't work.
The values of c1[1] and c2[2] are modified by mouse input, so they keep on changing. I wonder that in some cases i get the correct result , but for others i get this bug. What can be the problem?
c1[1] 1.22857
c2[1] 1.22857
(double)(c1[1]) == (double)(c2[1]) 0
c2[1] - c1[1] -1.19209e-007
(double)(c1[1]) - (double)(c2[1]) 1.1920928955078e-007
c1[1] != c2[1] 1
madhur at 2007-11-11 1:21:05 >

# 5 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
Sorry, madhur.
Your "code" is not a code! Probably, some comments ... :confused:
And it is not clear what means :The values of c1[1] and c2[2] are modified by mouse input, so they keep on changing.
# 6 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
I simply copied that from the watch window. But since it was not showing up properly in the preview post, i thought inserting it as code would preserve the original spacing and tabs. So tell me what can i do - even if i insert spacing manually, they are removed when i preview the post.
c1[1] 1.22857
c2[1] 1.22857
(double)(c1[1]) == (double)(c2[1]) 0
c1[1] != c2[1] 1
c2[1] - c1[1] -1.19209e-007
(double)(c1[1]) - (double)(c2[1]) 1.1920928955078e-007
The values of c1[1] and c2[2] are modified by mouse input, so they keep on changing.
I meant to say that these values (c1[2] and c2[1]) are not set once for all, they keep on updating with cursor position.
madhur at 2007-11-11 1:23:11 >

# 7 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
I meant to say that these values (c1[2] and c2[1]) are not set once for all, they keep on updating with cursor position.And why do you hope that they MUST be the same?
BTW, "cursor position" is a POINT (means two integer values). Why do you convert hese values into float before comparing them?
# 8 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
Actually I am programming in OpenGL. I need to convert the mouse position, which is in screen coordinates, into world coordinates for 3D drawing.
madhur at 2007-11-11 1:25:13 >

# 9 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
Originally posted by madhur
Subtracting shows the bugIt is not a bug. Floating point values should never be compared for equality, since they are rarely exactly equal to each other.
For example, the debugger may show only 7 or 8 digits of precision, but what is in those positions that you don't see? You may be comparing 2.781230000001 to 2.781230000002. These numbers are not equal.
Instead, you should compare the absolute value of the differences of the two floating point numbers. If the absolute value is within some tolerance level, then they can be assumed to be equal.
#include <cmath>
//...
#define TOLERANCE 1.0e-08
double num1, num2;
if (fabs(num1 - num2) <= TOLERANCE )
{
// numbers are "equal"
}
Regards,
Paul McKenzie
# 10 Re: Most Bewildering Problem! Debugger says- 2 things which r equal r not equal
:D
Thanks Paul, that worked.
madhur at 2007-11-11 1:27:11 >
