### Question 14.5

What's a good way to check for ``close enough'' floating-point equality?

Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to use an accuracy threshold which is relative to the magnitude of the numbers being compared. Rather than

    	double a, b;
    	...
    	if(a == b)	/* WRONG */

use something like

    	#include <math.h>

    	if(fabs(a - b) <= epsilon * fabs(a))

for some suitably-chosen `epsilon`.

References: Knuth Sec. 4.2.2 pp. 217-8

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback