<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
Include the header, pick the prefix for your type, then use the macros in checks and comparisons.
Add #include <float.h> at the top of the file.
Choose the prefix: FLT_, DBL_, or LDBL_.
Use *_MAX / *_MIN for range guards and *_EPSILON for near-equality.
Use *_DIG when choosing print precision or float vs double.
Piece
Required?
Role
#include <float.h>
Yes
Provides the limit macros
FLT_* / DBL_*
Yes
Constants for that floating type
<math.h>
No
Needed 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)
Reference
Cheat Sheet
Typical IEEE 754 values (GCC x86-64). Always trust the macros on your machine.
Topic
Detail
Header
#include <float.h> (macros only)
FLT_MAX
~3.4 × 1038
FLT_EPSILON
~1.19 × 10−7
FLT_DIG
6 decimal digits
DBL_MAX
~1.8 × 10308
DBL_EPSILON
~2.22 × 10−16
DBL_DIG
15 decimal digits
FLT_RADIX
Usually 2
Try it
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
Trace
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 ✓
Code
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.
Include float.h. The compiler fills in platform-specific constants.
Print float macros.FLT_MAX, FLT_MIN, FLT_EPSILON, and FLT_DIG describe the 32-bit float type.
Print double macros. The DBL_* set is usually what you want for everyday math.
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;
}
Output
1.0f + FLT_EPSILON different? yes
1.0f + FLT_EPSILON/2 different? no
How It Works
Start at 1.0f. Machine epsilon is defined relative to 1.0.
Add full epsilon.1.0f + FLT_EPSILON is the next representable float above 1.0 → a != b.
Add half epsilon. That bump is too small; it rounds back to 1.0f → a == c.
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;
}
Output
x = 0.30000000000000004
y = 0.29999999999999999
x == y: false
approx_equal: true
How It Works
Binary fractions.0.1 and 0.2 are not exact in binary, so their sum is not exactly 0.3.
Strict equality fails.x == y is false even though both look like 0.3 to humans.
Use fabs + epsilon.fabs(a - b) measures the gap; compare to a multiple of DBL_EPSILON.
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.
Analysis
Performance Notes
Approach
Cost
Best for
float.h macros
Compile-time (zero runtime)
Portable limits and tolerances
float arrays
Half the memory of double
Large data when FLT_DIG is enough
Default double
More precision, often same ALU cost
General calculations
Macros themselves are free. Choosing float vs double is a memory/precision trade-off—not a cost of using float.h.
Remember
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.