What alternative can I use if I need to have floating point numbers?

What Paul says above about picturing float values as being a bit fuzzy is a very good way to think about it.

For an example of why this (often) doesn't matter, say we had a float "x" and we wanted to choose different algorithms based on whether or not x was less than 1. Perhaps one algorithm was known to work better for x<1 and the other work better for x>1. In a case like this however, where x represents a continuous real world variable, it's a reasonable assumption that both algorithms are about the same when x=1. So realistically, it probably doesn't matter which algorithm we choose when x is very close to 1.0.

So in many cases like the above, the "fuzziness" of floats simply doesn't matter. And if it does matter, then it probably means that a float (at least of the precision you are using) is not appropriate for the problem at hand.

If you want some concrete examples of how we deal with this fuzziness in practice, the answer is that we usually use a "tolerance" constant that is appropriate for the precision we are using. For example:

#define toln 1e-6 // Thanks AWOL.

In the following examples I'll assume we are comparing "float x" with 1.0.

For testing equality we would typically use:

if ( fabs(x-1.0) < toln ) ...When testing inequality (for example less than) you could use either,

if (x < 1.0) ...or

if (x <= 1.0)...To be honest, it really shouldn't matter which of the above two that you use, at least if a float is truly appropriate for the problem at hand. In either case we don't really care what happens for values very close to 1.0. We simply accept that for calculations that would theoretically give 0.9999999 to 1.0000001, that it could go either way.

If for some reason we absolutely must have x=1 excluded, then use something like:

if (x + toln < 1.0) ...If for some reason we absolutely must have x=1 included, then use something like:

if (x <= 1.0 + toln) ...BTW. The choice for toln could depend upon just how many chained calculations (and how many opportunities there are for rounding errors) before you get to your comparison. But typically it would be somewhere about

1E-6 for single precision and somewhere about

1e-13 for double precision.