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
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.