If you want to compare a real value to anything else, you must make sure that your comparison works correctly regardless of any storage problems that might have occurred. Therefore, you must determine "how close to equal is equal for my purposes". Is 3.334 equal to 3.333, or is 3.33 close enough to 3.333 to be considered equal?
Once you have determined what you consider "close enough", you must define a constant to represent that "fudge factor". Usually, in mathematics, this error factor is called EPSILON and that is what we will use.
Note that I capitilized the EPSILON. C programming convention usually uses all caps for defined constants.
At this point, simply define any constants at the beginning of your program after the #include and before the prototypes. These are preprocessor directives and will be discussed (or have been) in more depth at a different time.
#define EPSILON 0.0001 #define PI 3.14159 #define TRUE 1 #define FALSE 0 #define LETTER 'A' etc. Assume you have two variables, x and y. If EITHER x or y is real. Comparison desired Comparison to use x == y fabs(x - y) < EPSILON x <= y x < y + EPSILON x >= y x > y - EPSILONNOTE: The fabs() function is included in the math.h library. It works for real or double numbers. If you need the absolute value of an integer value, use abs() which is in stdlib.h (many implementations seem to have this in stdio.h as well). I don't know why they are in different libraries, except that all the functions in math.h seem to be for doubles. Don't use abs() with reals. You get garbage or 0 which is not very helpful.