ZetCode

C floating-point types

last modified November 14, 2026

Floating-point types store numbers with a fractional part and a wide range of magnitudes. C provides three of them, float, double and long double, which typically follow the IEEE 754 binary formats. This tutorial covers their sizes and precision, why most decimal fractions cannot be stored exactly, how to compare such values, the sign-exponent-mantissa layout, special values, rounding, the limits published in float.h, and the way small errors accumulate in long calculations.

The three floating-point types

The types differ in storage size and in the number of significant binary digits, called the mantissa, that they can represent. The first program prints the sizes on the current platform and the constants that describe the precision.

float_types.c
#include <stdio.h>
#include <float.h>

int main(void) {

    float f = 3.1415926535f;
    double d = 3.141592653589793;
    long double ld = 3.14159265358979323846L;

    printf("sizeof(float) = %zu\n", sizeof f);
    printf("sizeof(double) = %zu\n", sizeof d);
    printf("sizeof(long double) = %zu\n", sizeof ld);

    printf("float:       %.15f\n", f);
    printf("double:      %.15f\n", d);
    printf("long double: %.21Lf\n", ld);

    printf("mantissa bits: float %d, double %d, long double %d\n",
           FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG);

    return 0;
}

The literal suffix chooses the type: f makes a float, L a long double, and an unsuffixed literal with a decimal point is a double. The float value is stored with 24 mantissa bits, so it agrees with the decimal literal only to about seven significant digits; the remaining digits printed by %.15f are the exact binary value, not extra accuracy.

$ gcc -std=c17 -Wall -Wextra -o float_types float_types.c
$ ./float_types
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
float:       3.141592741012573
double:      3.141592653589793
long double: 3.141592653589793238513
mantissa bits: float 24, double 53, long double 64

Decimal fractions are not exact

A binary floating-point value is a sum of powers of two, so fractions such as one half or one quarter are exact while one tenth is not: 0.1 in binary is a repeating fraction, just as 1/3 is in decimal. The next program adds 0.1 and 0.2 and compares the result with 0.3.

decimal_inexact.c
#include <stdio.h>

int main(void) {

    double a = 0.1;
    double b = 0.2;
    double c = 0.3;

    printf("0.1 + 0.2 = %.17f\n", a + b);
    printf("0.3       = %.17f\n", c);
    printf("a + b == c: %d\n", a + b == c);

    float f = 0.1f;

    printf("0.1f as double = %.17f\n", (double) f);

    return 0;
}

Each literal is rounded to the nearest representable double when it is compiled, and the sum of the two rounded values is rounded again. The result differs from the stored 0.3 by one unit in the last place, so the equality test is false. %.17f is used because 17 significant decimal digits are enough to recover any double exactly; with the default six digits both numbers would print as 0.300000 and hide the difference.

$ gcc -std=c17 -Wall -Wextra -o decimal_inexact decimal_inexact.c
$ ./decimal_inexact
0.1 + 0.2 = 0.30000000000000004
0.3       = 0.29999999999999999
a + b == c: 0
0.1f as double = 0.10000000149011612

Comparing floating-point values

Because of that rounding, == is rarely the right test for computed values. A common approach accepts two numbers as equal when their difference is smaller than a tolerance, which for double is often taken from DBL_EPSILON.

epsilon.c
#include <stdio.h>
#include <float.h>
#include <math.h>

int main(void) {

    double a = 0.1 + 0.2;
    double b = 0.3;

    printf("fabs(a - b) = %.17f\n", fabs(a - b));
    printf("DBL_EPSILON = %.17f\n", DBL_EPSILON);
    printf("fabs(a - b) < DBL_EPSILON: %d\n", fabs(a - b) < DBL_EPSILON);

    return 0;
}

DBL_EPSILON is the distance between 1.0 and the next representable double, about 2.2e-16. The difference here is smaller, so the test accepts the values as equal. A fixed absolute tolerance is meaningful only near 1.0; for large magnitudes the gap between neighbouring values is bigger, and a relative test that divides the difference by the magnitude is more reliable. Exact comparison remains correct for values that were not computed, such as a stored constant compared with itself.

$ gcc -std=c17 -Wall -Wextra -o epsilon epsilon.c -lm
$ ./epsilon
fabs(a - b) = 0.00000000000000006
DBL_EPSILON = 0.00000000000000022
fabs(a - b) < DBL_EPSILON: 1

Inside a float

An IEEE 754 single-precision value is 32 bits: one sign bit, eight exponent bits and 23 stored mantissa bits. The next program copies the bytes of a float into an integer and prints the three fields and the binary pattern.

float_representation.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>

static void print_float_bits(float f) {

    uint32_t bits;
    memcpy(&bits, &f, sizeof bits);

    uint32_t sign = bits >> 31;
    uint32_t exponent = (bits >> 23) & 0xFF;
    uint32_t mantissa = bits & 0x7FFFFF;

    printf("value: %g\n", f);
    printf("sign: %u\n", sign);
    printf("exponent: 0x%02X (biased %u, actual %d)\n",
           exponent, exponent, (int) exponent - 127);
    printf("mantissa: 0x%06X\n", mantissa);
    printf("binary: ");

    for (int i = 31; i >= 0; i--) {

        printf("%u", (bits >> i) & 1);

        if (i == 31 || i == 23) {
            printf(" ");
        }
    }

    printf("\n");
}

int main(void) {

    print_float_bits(1.0f);
    printf("\n");
    print_float_bits(0.1f);
    printf("\n");
    print_float_bits(-3.5f);

    return 0;
}

memcpy is the portable way to inspect the representation: casting the address of a float to uint32_t * and dereferencing it would break the strict aliasing rules. A normal number has a biased exponent between 1 and 254 and represents the value (-1)sign × 2exponent-127 × 1.mantissa. For 1.0 the exponent is 127, so the scale is 20 and the mantissa is zero; the implicit leading 1 is not stored.

$ gcc -std=c17 -Wall -Wextra -o float_representation float_representation.c
$ ./float_representation
value: 1
sign: 0
exponent: 0x7F (biased 127, actual 0)
mantissa: 0x000000
binary: 0 01111111 00000000000000000000000

value: 0.1
sign: 0
exponent: 0x7B (biased 123, actual -4)
mantissa: 0x4CCCCD
binary: 0 01111011 10011001100110011001101

value: -3.5
sign: 1
exponent: 0x80 (biased 128, actual 1)
mantissa: 0x600000
binary: 1 10000000 11000000000000000000000

Infinity, NaN and signed zero

Some bit patterns are reserved for values that are not ordinary numbers: exponent 255 with a zero mantissa is infinity, exponent 255 with a non-zero mantissa is NaN, "not a number", and exponent 0 with a zero mantissa is zero, whose sign is stored separately. The header math.h provides the constants and the classification macros.

special_values.c
#include <stdio.h>
#include <math.h>

int main(void) {

    double inf = INFINITY;
    double nan = NAN;
    double zero = 0.0;
    double neg_zero = -0.0;

    printf("inf: %f\n", inf);
    printf("nan: %f\n", nan);
    printf("zero: %f\n", zero);
    printf("neg_zero: %f\n", neg_zero);
    printf("zero == neg_zero: %d\n", zero == neg_zero);
    printf("signbit(neg_zero): %d\n", signbit(neg_zero) != 0);
    printf("inf == inf: %d\n", inf == inf);
    printf("nan == nan: %d\n", nan == nan);
    printf("isinf(inf): %d\n", isinf(inf));
    printf("isnan(nan): %d\n", isnan(nan));

    return 0;
}

Infinity compares equal to itself, while NaN compares equal to nothing, not even to another NaN; that unequal-to-itself property is the portable way to recognize it without isnan. Positive and negative zero compare equal, and only signbit distinguishes them, which matters for division and for functions such as log. The classification macros return a non-zero value when the test succeeds, so they are used directly in conditions.

$ gcc -std=c17 -Wall -Wextra -o special_values special_values.c -lm
$ ./special_values
inf: inf
nan: nan
zero: 0.000000
neg_zero: -0.000000
zero == neg_zero: 1
signbit(neg_zero): 1
inf == inf: 1
nan == nan: 0
isinf(inf): 1
isnan(nan): 1

Rounding

Every operation that cannot be represented exactly is rounded, and the default mode rounds to the nearest representable value with ties going to the even digit. The next program shows the default mode, the tie-breaking rule and the difference between computing one third as float and as double.

rounding.c
#include <stdio.h>
#include <fenv.h>

int main(void) {

    printf("default mode is FE_TONEAREST: %d\n",
           fegetround() == FE_TONEAREST);

    printf("%.0f %.0f %.0f %.0f\n", 0.5, 1.5, 2.5, 3.5);

    float third = 1.0f / 3.0f;

    printf("1/3 as float:  %.20f\n", third);
    printf("1/3 as double: %.20f\n", 1.0 / 3.0);

    return 0;
}

The four values round to 0, 2, 2 and 4: ties go to the nearest even integer rather than always upward. The one-third values show how the same mathematical result is stored with different numbers of significant bits, which is why a calculation carried out in float and then printed with many digits looks wrong but is in fact the closest float to one third.

$ gcc -std=c17 -Wall -Wextra -o rounding rounding.c -lm
$ ./rounding
default mode is FE_TONEAREST: 1
0 2 2 4
1/3 as float:  0.33333334326744079590
1/3 as double: 0.33333333333333331483

Limits from float.h

The float.h header publishes the range and precision of each type. The program prints the largest and smallest positive normal value, the machine epsilon and the number of decimal digits that survive a round trip through the type.

limits.c
#include <stdio.h>
#include <float.h>

int main(void) {

    printf("FLT_MAX:     %e\n", FLT_MAX);
    printf("FLT_MIN:     %e\n", FLT_MIN);
    printf("FLT_EPSILON: %e\n", FLT_EPSILON);
    printf("FLT_DIG:     %d\n", FLT_DIG);

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

    return 0;
}

FLT_MIN is the smallest positive normal value, not zero and not the smallest representable value. FLT_DIG is 6 and DBL_DIG is 15, which is why float is not suitable for values that need more than about six significant decimal digits and why double is the default choice for ordinary calculations.

$ gcc -std=c17 -Wall -Wextra -o limits limits.c
$ ./limits
FLT_MAX:     3.402823e+38
FLT_MIN:     1.175494e-38
FLT_EPSILON: 1.192093e-07
FLT_DIG:     6
DBL_MAX:     1.797693e+308
DBL_MIN:     2.225074e-308
DBL_EPSILON: 2.220446e-16
DBL_DIG:     15

Denormal numbers

Values smaller than FLT_MIN are stored in a special form with an exponent field of zero, called denormal or subnormal. They keep the sign and the mantissa but lose the implicit leading 1, so they have fewer significant bits. The next program halves FLT_MIN and classifies the result.

denormals.c
#include <stdio.h>
#include <float.h>
#include <math.h>

int main(void) {

    float normal = FLT_MIN;
    float denormal = FLT_MIN / 2.0f;

    printf("FLT_MIN:     %e\n", normal);
    printf("FLT_MIN / 2: %e\n", denormal);
    printf("isnormal(FLT_MIN): %d\n", isnormal(normal));
    printf("isnormal(FLT_MIN / 2): %d\n", isnormal(denormal));
    printf("fpclassify(FLT_MIN / 2) == FP_SUBNORMAL: %d\n",
           fpclassify(denormal) == FP_SUBNORMAL);

    return 0;
}

The halved value is still representable, but no longer normal: it lies between zero and FLT_MIN and is stored with a reduced mantissa. As values get closer to zero, fewer bits remain available, so precision decreases gradually until the number rounds to zero altogether. Some processors can be configured to flush such values to zero for speed, which changes the result of calculations that depend on them.

$ gcc -std=c17 -Wall -Wextra -o denormals denormals.c -lm
$ ./denormals
FLT_MIN:     1.175494e-38
FLT_MIN / 2: 5.877472e-39
isnormal(FLT_MIN): 1
isnormal(FLT_MIN / 2): 0
fpclassify(FLT_MIN / 2) == FP_SUBNORMAL: 1

Error accumulation

Each rounded operation introduces a small error, and in a long sum the errors can add up to a visible bias. The next program adds 0.01 ten thousand times in float, repeats the calculation with the Kahan compensated summation algorithm, and finishes with a subtraction of nearly equal large values.

error_accumulation.c
#include <stdio.h>

int main(void) {

    float sum = 0.0f;

    for (int i = 0; i < 10000; i++) {
        sum += 0.01f;
    }

    float kahan = 0.0f;
    float c = 0.0f;

    for (int i = 0; i < 10000; i++) {

        float y = 0.01f - c;
        float t = kahan + y;

        c = (t - kahan) - y;
        kahan = t;
    }

    printf("naive sum: %.10f\n", sum);
    printf("kahan sum: %.10f\n", kahan);

    float x = 1e8f;
    float y = x + 1.0f;

    printf("(1e8 + 1) - 1e8 = %.1f\n", y - x);

    return 0;
}

The naive sum ends at 100.00295 instead of 100, an error of about 3e-5, because the running total has more significant digits than a float can hold once it grows past 64, and each addition loses the low bits of 0.01. Kahan summation keeps those lost bits in the compensation variable c and adds them back, which recovers the exact result here. The last calculation shows absorption: adding 1 to 1e8 changes nothing at all, since the difference is below the spacing of float at that magnitude.

$ gcc -std=c17 -Wall -Wextra -o error_accumulation error_accumulation.c
$ ./error_accumulation
naive sum: 100.0029525757
kahan sum: 100.0000000000
(1e8 + 1) - 1e8 = 0.0

Guidelines

Most floating-point problems are avoided by choosing the type deliberately and by treating equality with care. The rules below summarise the behaviour shown in the examples.

Source

cppreference: arithmetic types
cppreference: common mathematical functions
cppreference: limits of floating-point types
Wikipedia: IEEE 754

This tutorial covered floating-point types in C: the sizes and precision of float, double and long double, the inexactness of decimal fractions, comparison with an epsilon, the IEEE 754 layout, infinity and NaN, rounding, the limits in float.h, denormal values and error accumulation. Integer types and their ranges are covered in C variable, and formatted output of these values in C printf.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C tutorials.