<float.h> defines platform-specific floating-point limits: DBL_EPSILON (smallest difference from 1.0), DBL_MAX, DBL_MIN, and precision constants. Essential for writing numerically robust code across platforms.
Use DBL_EPSILON as the basis for floating-point comparisons instead of exact equality.
This example demonstrates machine epsilon and shows the limits of floating-point representation.
// compile: gcc -o floatexample floatexample.c // run: ./floatexample // description: query floating-point limits and test epsilon #include <float.h> #include <math.h> #include <stdio.h> int main() { printf("DBL_EPSILON = %.2e\n", DBL_EPSILON); printf("1.0 + DBL_EPSILON != 1.0: %s\n", (1.0 + DBL_EPSILON != 1.0) ? "true" : "false"); printf("1.0 + DBL_EPSILON/2 != 1.0: %s\n", (1.0 + DBL_EPSILON/2 != 1.0) ? "true" : "false"); printf("DBL_MAX = %.6e\n", DBL_MAX); printf("DBL_MIN = %.6e\n", DBL_MIN); printf("DBL_DIG = %d decimal digits\n", DBL_DIG); return 0; }
Double precision:
DBL_EPSILON: smallest x where 1.0 + x != 1.0DBL_MAX, DBL_MIN: largest/smallest representable valuesDBL_DIG: decimal digits of precisionDBL_MANT_DIG: mantissa digitsDBL_MAX_EXP, DBL_MIN_EXP: exponent boundsFloat versions (FLT_* prefix):
FLT_EPSILON, FLT_MAX, FLT_MIN, etc.Long double versions (LDBL_* prefix):
LDBL_EPSILON, LDBL_MAX, LDBL_MIN, etc.