Because of floating point rounding errors, you may never end up with a float, or a double, which is exactly 0.0 ( unless you set it yourself ). That is why i use the following technique:
Code:
if ((val-ROUNDING_ERROR) <= test && (val+ROUNDING_ERROR) >= test)
An alternative is the following:
Code:
if (fabs(val-test) <= ROUNDING_ERROR) // I'm not sure if that way works
Which one you'll choose to use is up to you. For performance sake, i suggest the second one, but for simplicity the first.
Code:
val :: The first number you want to compare
test :: The second number you want to compare
ROUNDING_ERROR :: typical float tolerance is 0.0000001f , for double tolerance i'm not sure
( You can even use your own tolerance by passing in to the function.