Site Tools


wiki:h-float

Table of Contents

<float.h>

<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.

Example

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;
}
wiki/h-float.md · Last modified: by 127.0.0.1