I have this simple test:
double h;
...
// code that assigns h its initial value, used below
...
if ((h>0) && (h<1)){
//branch 1 -some computati
It might be a precision issue. H might not be exactly 1, but something very near to it. Could you post some more information on what you're trying to do, for instance, where does the value of H come from, and where does it go?
Okay, you've posted the code. You are calculating h by a series of arithmetic operations from what looks like fairly arbitrary numbers. This means you're going to get a very close approximation to the ideal value of h, but not quite the right one.
This means that you need to do approximate comparisons. Testing (h == 1.0) will succeed only by accident; try (fabs(h - 1.0) < 1e-10) or something like that (using a const double instead of a magic number for tolerance). Make the appropriate changes for other comparisons.
Always allow for rounding errors when comparing floating point values. Rather than testing for equality, something like this is usually what you want:
if (abs(x - y) < epsilon) ...
where epsilon is some suitably small value, like 0.00001.
There are several reasons why floating point math isn't accurate. First, not all values can be represented exactly (0.1 for example, can not be represented in binary floating point numbers, in the same way that 1/3 can't be represented in decimals)
Another is that floating point only uses a fixed number of significant digits (which "float" relative to the decimal point, hence the name). So on large numbers, small fractions effectively get truncated away. Never assume that floating point computations return an accurate result.
Check the actual value of h by printing it out with maximum precision. You'll probably find that it is actually slightly less than 1.0.
I ran the following code as a test
#include <iostream>
int main()
{
double h = 1.0;
if((h>0) && (h<1))
{
std::cout << "first branch" << std::endl;
}
else
{
std::cout << "second branch" << std::endl;
}
}
and the output was "first branch" (using g++ 4.3.2 on Ubuntu 8.10), but Indeera mentioned in a comment that the same code running on Windows XP compiled with VS2005 gives the output "second branch" (thanks, Indeera).
You might change your code to compare the differences between h
and 0.0 and h
and 1.0 to some small delta value.
double allowedDelta = 0.000001;
if( ((h - 0.0) > allowedDelta) && ((1.0 - h) > allowedDelta) )
... // h is between 0.000001 and 0.9999990
Note that "(h - 0.0)" can be replaced with "h" in this special case. I'm leaving it the way it is for illustrative value.
Also note that if you were only making one comparison you'd need to compare the delta to the absolute value of the difference between h and some constant. Since you're checking a range here, the two comparisons ANDed together make a special case where you can bypass the use of abs
. If h is a negative value or some positive value greater than 1.0 it will be out of range and fail one of the two tests above.