C++ Compiler Strange Behavior

Hello All,

I see a very strange behavior with the C++ Compiler. Below is my sample code. When I debug this code, I did not get any error whereas when I execute, the "IF" condition fails and the error is thrown. As per the value read from Hardware the value of OV when comparing should be 16.50646050 and temp + OVDIFF should be 11.0. I could not understand why the IF condition fails. A strange thing happens when the comparision is changed the following way. i.e if((temp + OVDIFF) < OV ). In this case the IF condition pass and I am not getting any error.

Can someone explain to me the root cause of this strange behavior? How does C++ compiler handle this comparision?

-----------------------------------------------------------------------------
//Sample Code

//LSB = 26.45556/0xffff = 0.0064604542;
//OVDIFF = 1.0;

double temp = 10.0;
double OV = -1.0;

OV = Hardware_Read(); // Hardware_Read() returns 2554
OV = OV * LSB;
OV = OV + LSB

if(OV >= (temp + OVDIFF))
{
// Take some action
}
else
{
// Error
}

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

Thanks in Advance
Karthik
A few cout statements at the appropriate places should show you what is going on;


1. if 0XFFFF is decimal 65535 then 26.45556/65535 is 0.000403686 which is ten times smaller than the figure you quoted.

2. if Hardware_read() function returns 2554 (two thousand, five hundred and fifty four) - then
OV = OV * LSB; = 2554 * 0.000403686 = something like 1.03101 (to five decimal places)

3. OV = OV + LSB = 1.03101 + 0.000403686 = 1.03142 (to 5 decimal places)

4. (temp + OVDIFF) = 10.0 + 1.0 = 11.0

so your if test is if ( 1.03142 >= 11) which is obviously not true.
Sorry !! That was a typo. It is 0x0FFF and not 0xFFFF. So it is 26.45556/4095 which is 0.0064604542
making that minor alteration, the if test should be true now.
Topic archived. No new replies allowed.