Skip to content
systemcoding.dev
Fundamentals

float vs double: Precision, Range and Performance

Choose between float and double in C by precision, range, memory, SIMD width and hardware support, with accumulation examples and a decision table.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the C, Rust and C++ logos.

Every C program that does real arithmetic must pick between float and double, and the reasoning "double is bigger, so use double" is half right. A double gives about 16 decimal digits and a range up to 10 to the 308, at twice the memory and, in some places, half the throughput. A float gives about 7 digits and a range up to 10 to the 38, and it is the better choice when the data is large, the hardware has a narrow floating-point unit, or the precision is not needed. This post compares them on precision, range, memory, vector width and hardware, shows what the precision difference does to a simple sum, covers the conversions that quietly turn one into the other, and ends with a decision table. It builds on the earlier post on how floats are stored, and uses GCC 14 on x86-64.

Precision

The two formats differ in how many bits of significand they carry: 24 for float and 53 for double. That translates to a spacing between neighbouring values that is relative to the size of the number, about 1.2 times 10 to the minus 7 for a float and 2.2 times 10 to the minus 16 for a double. In decimal terms, a float has 6 digits that always survive a round trip and a double has 15.

The consequence is visible in a simple loop. Add 0.1 ten times in each type.

sums.cc
#include <stdio.h>

int main(void)
{
    float fs = 0.0f;
    double ds = 0.0;

    for (int i = 0; i < 10; i++) {
        fs += 0.1f;
        ds += 0.1;
    }
    printf("float : %.9g\n", fs);
    printf("double: %.17g\n", ds);

    float big = 16777216.0f;
    printf("2^24 + 1 in float: %.1f\n", big + 1.0f);
    printf("sizeof: %zu and %zu\n", sizeof(float), sizeof(double));
    return 0;
}

The float sum overshoots 1 by a little more than one unit in the last place, and the double sum lands one step below 1. Both are wrong in the last digit, but the float error is about a billion times larger in absolute size. The third line shows the integer limit: 16,777,216 is 2 to the 24, and adding 1 to it in single precision rounds back to the same value, since 16,777,217 needs 25 bits.

Text
float : 1.00000012
double: 0.99999999999999989
2^24 + 1 in float: 16777216.0
sizeof: 4 and 8

The 2 to the 24 limit has practical consequences. A float used as a counter stops incrementing at 16,777,216. A float timestamp in seconds loses whole-second resolution after about 194 days, which is 2 to the 24 seconds, and a float coordinate in a large world loses centimetre precision far from the origin, which is the cause of jitter in games when the player travels far from the origin. A double pushes the limits to 2 to the 53, about 9 times 10 to the 15.

Range

Both formats have more range than most programs use. The largest float is about 3.4 times 10 to the 38, and the smallest positive normal is about 1.2 times 10 to the minus 38. For double the figures are 1.8 times 10 to the 308 and 2.2 times 10 to the minus 308. Overflow in a float happens sooner than you expect from intermediate values: the square of 10 to the 20 is 10 to the 40, which exceeds FLT_MAX and becomes infinity, even though 10 to the 20 itself is fine. A calculation that squares large values, such as a variance or a vector length, may need double for the intermediates or a rescaling step. Underflow is the mirror case: a product of many small probabilities in float goes to zero long before it does in double, which is why such code works in logarithms.

Memory and bandwidth

A float is half the size. For an array of a hundred million values, that is 400 MB against 800 MB, and since many numeric programs are limited by memory bandwidth rather than arithmetic, halving the data roughly doubles the number of values the memory system can deliver per second. A smaller array also fits more elements in each cache line and in the cache as a whole: a 64-byte line holds 16 floats or 8 doubles. For large datasets, such as images, audio, neural network weights and simulation grids, float or an even smaller format is the norm.

SIMD width

Vector units process a fixed number of bits at once, so a narrower type means more lanes. A 128-bit SSE register holds four floats or two doubles, a 256-bit AVX register holds eight floats or four doubles, and a 512-bit AVX-512 register holds sixteen floats or eight doubles. Code that the compiler can vectorise therefore does twice the work per instruction in single precision. The throughput of the individual operations is similar, and the gain comes from the lane count. Division and square root have longer latency in double precision on most x86 cores, according to the vendor's instruction tables.

Hardware support

The hardware decides whether double is even available at full speed.

Platformfloatdouble
x86-64 CPUsfull speedfull speed, half the SIMD lanes
Arm Cortex-Afull speedfull speed
Arm Cortex-M4Fhardware, single precision FPUsoftware emulation, much slower
Arm Cortex-M7hardwarehardware if the optional double-precision FPU is fitted
Cortex-M0 and M3softwaresoftware
GPUs, consumerfull speeda fraction of single-precision throughput, set by the architecture

On a microcontroller with a single-precision FPU such as the Cortex-M4F, a single double operation calls a library routine that can take dozens of cycles, and one stray double constant in a hot loop can change the speed of the program. GPUs are similar: the ratio of double to single throughput is published in each vendor's programming guide, and on consumer cards it is small.

Where double sneaks in

C makes it easy to use double by accident. An unsuffixed literal is a double, so x * 0.5 where x is a float promotes x to double, multiplies in double precision and converts the result back. The arguments to printf are promoted too, and the maths functions come in separate versions: sin takes and returns double, and sinf is the float version.

promote.cc
#include <math.h>
#include <stdio.h>

int main(void)
{
    float x = 2.0f;
    float a = x * 0.5;
    float b = x * 0.5f;
    float c = sqrtf(x);

    printf("%f %f %f\n", a, b, c);
    return 0;
}

The values are the same here, but a computes in double and b in float, and in a hot loop on a platform without a double FPU the difference is large. The call sqrtf stays in single precision, and sqrt(x) would convert up and back. The output, with %f printing six decimals, is:

Text
1.000000 1.000000 1.414214

GCC can warn about the promotion: -Wdouble-promotion flags a float converted to double implicitly, including when passed to printf, and -Wfloat-conversion flags the narrowing from double to float. They are not enabled by -Wall and are useful on embedded projects. Suffix literals with f in float code, and use the f versions of the library functions, or tgmath.h generic macros that pick by argument type.

A further trap on 32-bit x86 using the x87 unit is that intermediate results may be kept in 80-bit registers, so a float computation gives different results depending on spills. On x86-64 with SSE2 this does not happen, and FLT_EVAL_METHOD is 0.

Accuracy versus speed in practice

A common misconception is that using double throughout is the safe default. It is a reasonable default for general code, since the extra precision hides many small errors, but it does not remove them, as the sum above shows: the double result is still not 1. The right question is how much error the result can tolerate. For graphics, audio, and machine learning inference, single precision, or half precision, is ample. For financial sums, neither format is right: use integers or a decimal type. For scientific accumulation over millions of terms, even double can need compensated summation or a wider accumulator. A useful practice is to store data in float and accumulate in double, getting the memory benefit without the worst of the error growth.

The decision table

SituationChooseReason
general-purpose calculation, no strong constraintdoublethe extra digits absorb rounding error, and the cost is small on desktop CPUs
large arrays, images, audio, neural network weightsfloathalf the memory and bandwidth, twice the SIMD lanes
microcontroller with a single-precision FPU onlyfloatdouble is emulated in software
accumulating many termsdouble accumulator, float storagelimits error growth without doubling the data
values beyond 10 to the 38 or squares of large valuesdoubleavoids overflow to infinity
exact decimal quantities such as moneyintegers or a decimal typeneither binary format is exact for decimals
integers above 2 to the 24double or a 64-bit integerfloat cannot hold them exactly

Common mistakes

  • Writing float code with unsuffixed literals and sin, which runs in double precision.

  • Using float for a counter, a timestamp or a world coordinate far from the origin.

  • Assuming double removes rounding error, instead of reducing it.

  • Comparing a float with a double literal, as in x == 0.1, which is almost always false.

  • Printing a float with too few or too many digits, instead of %.9g.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.