gawk: Comparing FP Values
1
1 15.4.1.2 Be Careful Comparing Values
1 ....................................
1
1 Because the underlying representation can be a little bit off from the
1 exact value, comparing floating-point values to see if they are exactly
1 equal is generally a bad idea. Here is an example where it does not
1 work like you would expect:
1
1 $ gawk 'BEGIN { print (0.1 + 12.2 == 12.3) }'
1 -| 0
1
1 The general wisdom when comparing floating-point values is to see if
1 they are within some small range of each other (called a "delta", or
1 "tolerance"). You have to decide how small a delta is important to you.
1 Code to do this looks something like the following:
1
1 delta = 0.00001 # for example
1 difference = abs(a) - abs(b) # subtract the two values
1 if (difference < delta)
1 # all ok
1 else
1 # not ok
1
1 (We assume that you have a simple absolute value function named 'abs()'
1 defined elsewhere in your program.)
1