Performs a fuzzy comparison of two numeric values.

Category: | Mathematical |

In floating point arithmetic,
the value of a sum sometimes depends on the order in which the numbers
are added. One approximate bound for the floating point error in the
computation of a sum of n numbers,
x1 through xn is expressed
by the following formula:

n * machine_precision * sum (abs(x1) + ... + abs(xn))

To compare sums of n floating point numbers with the COMPFUZZ function,
you can therefore use n as
the fuzz value and the sum of the absolute values as the scale factor,
as shown in the following DATA step:

data _null_; x1 = -1/3; x2 = 22/7; x3 = -1234567891; x4 = 1234567890; /* Add the numbers in two different orders. */ sum1 = x1 + x2 + x3 + x4; sum2 = x4 + x3 + x2 + x1; diff = abs (sum1 - sum2); put sum1= / sum2= / diff=; /* Using only a fuzz value gives the wrong result. The fuzz value */ /* is 8 because there are four numbers in each sum, for a total of */ /* eight numbers. */ compfuzz = compfuzz (sum1, sum2, 8); put "fuzz only (wrong): " compfuzz=; /* Using a fuzz factor and a scale value gives the correct result. */ scale = abs(x1) + abs(x2) + abs(x3) + abs(x4); compfuzz = compfuzz (sum1, sum2, 8, scale); put "fuzz and scale (correct): " compfuzz=; run;

Copyright © SAS Institute Inc. All rights reserved.