Next: , Previous: , Up: Inexactness of computations   [Contents][Index] Be Careful Comparing Values

Because the underlying representation can be a little bit off from the exact value, comparing floating-point values to see if they are exactly equal is generally a bad idea. Here is an example where it does not work like you would expect:

$ gawk 'BEGIN { print (0.1 + 12.2 == 12.3) }'
-| 0

The general wisdom when comparing floating-point values is to see if they are within some small range of each other (called a delta, or tolerance). You have to decide how small a delta is important to you. Code to do this looks something like the following:

delta = 0.00001                 # for example
difference = abs(a) - abs(b)    # subtract the two values
if (difference < delta)
    # all ok
    # not ok

(We assume that you have a simple absolute value function named abs() defined elsewhere in your program.)