AlmostEqual
- rounding error insensitive comparison

Math functions
(AFL 2.80)


SYNTAX AlmostEqual( x, y, ulps = 5 )
RETURNS NUMBER
ARRAY
FUNCTION This is a helper function for comparing floating point numbers. It returns True if x and y are equal or almost equal upto defined accurracy (ulps). It is recommended to use this function instead of equality check (==) as it leads to more reliable comparisons and less headache caused by IEEE floating pointacurracy issues.

Parameters:

  • x, y - the numbers or arrays to be compared,
  • ulps stands for "units in last place" and represents maximum relative error of the comparison. Since 32 bit IEEE floating point numbers have accurracy of 7 significant digits, 1 unit in last place(ulp) represents relative error of 0.00001 %. The default value of ulps parameter is 5 which gives roughtly 0.00005% "comparison sensitivity".
Thanks to Bruce Dawson for his fast routine.
EXAMPLE if( 1/3 == 0.3333333 )
{
  
printf("32-bit Floating point IEEE exact equality\n");
}

if( AlmostEqual( 1/3, 0.3333333 ) )
{
  
printf("Numbers are almost equal\n");
}
SEE ALSO

References:

The AlmostEqual function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference