Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Comparing floating-point numbers in C

Reference for comparing floats and doubles in C: why == fails, absolute and relative epsilon tests, ULPs, NaN and infinity, and when exact comparison is fine.

Floating-point types store binary approximations. Most decimal fractions, such as 0.1, have no exact binary representation, so arithmetic accumulates tiny errors, and two computations that are equal on paper often differ in the last bits. Comparing them with == then fails.

The usual fix is to treat two numbers as equal when they are close enough. "Close enough" must be chosen for the problem: an absolute tolerance for numbers near zero, a relative one for large numbers. NaN, which is not equal to anything, needs special care.

C
if (fabs(a - b) <= 1e-9) { /* close enough */ }

Try it

C Why == fails and how to compare

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C NaN, infinity and exact cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <math.h>
double fabs(double x);
int isnan(real-floating x);          /* macro */
int isinf(real-floating x);          /* macro */
int isless(real-floating x, real-floating y);   /* no exception on NaN */
Name Description
fabs(x) Absolute value; fabsf and fabsl for float and long double.
isnan(x), isinf(x), isfinite(x) Classification macros, true (non-zero) for NaN, infinity, or a finite number.
DBL_EPSILON (<float.h>) Difference between 1.0 and the next larger double, about 2.2e-16. Useful as a base for tolerances near 1.
FLT_EPSILON The same for float, about 1.2e-7.
nextafter(x, y) The next representable value after x toward y.
isless, isgreater and friends Comparison macros that do not raise the "invalid" exception for NaN.

Returns / behavior: every ordered comparison (<, >, <=, >=, ==) with a NaN operand is false; only != is true.

Errors / undefined behavior: none for comparison. Converting a NaN or an out-of-range value to an integer type is undefined behavior.

Usage notes

  • DBL_EPSILON is not a universal tolerance. It is the spacing of doubles near 1.0. For values near 1000 the spacing is about 1000 times larger, and for sums of many terms the error is larger still. Scale the tolerance by the magnitude of the operands.
  • Combine absolute and relative. A purely relative test fails when one number is 0 (relative difference to zero is always 100%); a purely absolute test is meaningless for 1e15. The example uses both.
  • Choose the tolerance from your problem: measurement precision, number of operations, or required digits. 1e-9 is not magic.
  • Compare with < and >, not ==, in loops: for (x = 0; x != 1.0; x += 0.1) may never end. Use an integer counter and compute x = i * 0.1.
  • NaN handling. x != x is true only for NaN. Test with isnan(). Sorting or qsort comparators that meet NaN may break their ordering rules.
  • ULP comparison (counting representable values between two numbers) is the most principled test, but needs bit-level code; for most programs, the epsilon test is enough.
  • Money should not use floating point: store cents in an integer type.
  • float literals. 0.1f and 0.1 are different values; comparing a float to a double literal usually fails. Use matching types.
  • Excess precision. On some platforms (old x87) intermediate results use more bits than the declared type, and == can give different answers depending on optimization. Modern x86-64 code uses SSE and does not have this issue.
  • Exactly representable values (integers up to 2^53 as a double, and fractions of powers of two such as 0.5, 0.25) compare exactly and safely.