C float.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is float.h?

<float.h> is a C standard header that defines macros for floating-point limits and precision— how large, how small, and how precise float, double, and long double are on your platform.

Remember
#include <float.h>

FLT_ / DBL_ / LDBL_  → float / double / long double

*_MAX      → largest finite value
*_MIN      → smallest positive normalized
*_EPSILON  → gap at 1.0 (machine epsilon)
*_DIG      → decimal digits that round-trip

values are implementation-defined
use macros — never hard-code limits
UsePrefer
Upper / lower float boundsFLT_MAX / FLT_MIN
Near-equality for doublesDBL_EPSILON
Integer type limitslimits.h

How to Use float.h in C

Include the header, pick the prefix for your type, then use the macros in checks and comparisons.

  1. Add #include <float.h> at the top of the file.
  2. Choose the prefix: FLT_, DBL_, or LDBL_.
  3. Use *_MAX / *_MIN for range guards and *_EPSILON for near-equality.
  4. Use *_DIG when choosing print precision or float vs double.
PieceRequired?Role
#include <float.h>YesProvides the limit macros
FLT_* / DBL_*YesConstants for that floating type
<math.h>NoNeeded for fabs in comparisons

Pseudocode

Pseudocode
include float.h

// range
if |x| is beyond MAX for this type:
    handle overflow / reject

// near-equality
if |a - b| <= EPSILON * scale:
    treat as equal
else:
    treat as different

// precision
if you need more than FLT_DIG digits:
    prefer double (DBL_DIG)

Cheat Sheet

Typical IEEE 754 values (GCC x86-64). Always trust the macros on your machine.

TopicDetail
Header#include <float.h> (macros only)
FLT_MAX~3.4 × 1038
FLT_EPSILON~1.19 × 10−7
FLT_DIG6 decimal digits
DBL_MAX~1.8 × 10308
DBL_EPSILON~2.22 × 10−16
DBL_DIG15 decimal digits
FLT_RADIXUsually 2

Live Preview

Explore typical float.h values and the classic 0.1 + 0.2 comparison (browser simulation of IEEE double).

Simulates typical IEEE 754 macros (same ideas as the C examples below).

Result
FLT_EPSILON ≈ 1.192093e-7

1.0f + FLT_EPSILON     → different from 1.0f
1.0f + FLT_EPSILON/2   → still 1.0f

Use for near-equality near 1.0

Worked Walkthrough — 0.1 + 0.2

Why 0.1 + 0.2 == 0.3 is false, and how DBL_EPSILON helps you compare safely.

Calculation
x = 0.1 + 0.2
  → stored ≈ 0.30000000000000004

y = 0.3
  → stored ≈ 0.29999999999999999

x == y  → false   (bit patterns differ)

|x - y| ≈ 5.55e-17
DBL_EPSILON ≈ 2.22e-16

|x - y| <= DBL_EPSILON * 10  → true
  → treat as approximately equal ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Add -lm when using fabs from math.h. Sample output matches typical GCC on x86-64.

Example 1 — Print Key Limits

Read the most useful macros for float and double on your platform.

C
#include <float.h>
#include <stdio.h>

int main(void) {
    printf("FLT_MAX:     %e\n", (double)FLT_MAX);
    printf("FLT_MIN:     %e\n", (double)FLT_MIN);
    printf("FLT_EPSILON: %e\n", (double)FLT_EPSILON);
    printf("FLT_DIG:     %d\n", FLT_DIG);

    printf("\nDBL_MAX:     %e\n", DBL_MAX);
    printf("DBL_EPSILON: %e\n", DBL_EPSILON);
    printf("DBL_DIG:     %d\n", DBL_DIG);
    printf("FLT_RADIX:   %d\n", FLT_RADIX);

    return 0;
}

How It Works

  1. Include float.h. The compiler fills in platform-specific constants.
  2. Print float macros. FLT_MAX, FLT_MIN, FLT_EPSILON, and FLT_DIG describe the 32-bit float type.
  3. Print double macros. The DBL_* set is usually what you want for everyday math.
  4. Check FLT_RADIX. Almost always 2 (binary floating-point).

Example 2 — See Machine Epsilon

Verify the definition: adding full FLT_EPSILON changes 1.0f; half of it does not.

C
#include <float.h>
#include <stdio.h>

int main(void) {
    float a = 1.0f;
    float b = 1.0f + FLT_EPSILON;
    float c = 1.0f + FLT_EPSILON / 2.0f;

    printf("1.0f + FLT_EPSILON   different? %s\n",
           (a != b) ? "yes" : "no");
    printf("1.0f + FLT_EPSILON/2 different? %s\n",
           (a != c) ? "yes" : "no");

    return 0;
}

How It Works

  1. Start at 1.0f. Machine epsilon is defined relative to 1.0.
  2. Add full epsilon. 1.0f + FLT_EPSILON is the next representable float above 1.0 → a != b.
  3. Add half epsilon. That bump is too small; it rounds back to 1.0f → a == c.
  4. Lesson. Exact == fails for many float results; use a tolerance near *_EPSILON.

Example 3 — Approximate Equality

Compare 0.1 + 0.2 to 0.3 with DBL_EPSILON instead of ==.

C
#include <float.h>
#include <math.h>
#include <stdio.h>

int approx_equal(double a, double b) {
    return fabs(a - b) <= DBL_EPSILON * 10.0;
}

int main(void) {
    double x = 0.1 + 0.2;
    double y = 0.3;

    printf("x = %.17f\n", x);
    printf("y = %.17f\n", y);
    printf("x == y:       %s\n", (x == y) ? "true" : "false");
    printf("approx_equal: %s\n", approx_equal(x, y) ? "true" : "false");

    return 0;
}

How It Works

  1. Binary fractions. 0.1 and 0.2 are not exact in binary, so their sum is not exactly 0.3.
  2. Strict equality fails. x == y is false even though both look like 0.3 to humans.
  3. Use fabs + epsilon. fabs(a - b) measures the gap; compare to a multiple of DBL_EPSILON.
  4. Scale matters. This pattern works near 1.0. For very large values, use a relative tolerance.

Edge Cases & Pitfalls

Check these before relying on floating-point macros in real code.

epsilon

Epsilon is about 1.0

*_EPSILON is the gap at 1.0. For large magnitudes, absolute epsilon is often too small—use a relative tolerance.

min

FLT_MIN is normalized

Subnormals can be smaller than FLT_MIN. C11 adds FLT_TRUE_MIN for the absolute smallest positive float.

platform

Do not hard-code IEEE numbers

Macro values are implementation-defined. Always include float.h and use the names.

long double

long double varies

It may be 80-bit, 128-bit, or the same as double. Read LDBL_* on each target.

Performance Notes

ApproachCostBest for
float.h macrosCompile-time (zero runtime)Portable limits and tolerances
float arraysHalf the memory of doubleLarge data when FLT_DIG is enough
Default doubleMore precision, often same ALU costGeneral calculations

Macros themselves are free. Choosing float vs double is a memory/precision trade-off—not a cost of using float.h.

Key Takeaways

  • Role: portable macros for float/double range and precision.
  • Epsilon: gap at 1.0—use it for near-equality, not as “overall precision.”
  • Digits: FLT_DIG ≈ 6, DBL_DIG ≈ 15.
  • Portable: never hard-code IEEE constants—read the macros.

One line: float.h answers “how big, how small, and how precise are floats here?”—so your code stays portable.

Frequently Asked Questions

float.h defines macros for the limits and precision of float, double, and long double on your platform—maximum value, minimum normalized value, machine epsilon, and decimal digits. Use these macros instead of hard-coded numbers.
FLT_EPSILON is the smallest positive float such that 1.0f + FLT_EPSILON != 1.0f. It is machine epsilon around 1.0—not overall precision. Use DBL_EPSILON for double comparisons.
FLT_MIN is the smallest positive normalized float. Subnormal values can be smaller. C11 adds FLT_TRUE_MIN for the smallest positive value including subnormals.
No. Exact values depend on the compiler and hardware. IEEE 754 is common but not guaranteed. Always use the macros instead of hard-coding numbers like 3.402823e+38.
Default to double for general calculations—it has more precision (DBL_DIG decimal digits). Use float when memory bandwidth matters and about six decimal digits (FLT_DIG) is enough.
float.h covers floating-point types (float, double, long double). limits.h covers integer types (int, long, char). Use the header that matches the type you are working with.

Next: limits.h

Learn integer type limits with INT_MAX, CHAR_BIT, and more.

limits.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful