Floating constants are double by default

suggest change

An unsuffixed floating constant such as 0.1 has type double. The suffix f or F gives it type float, and l or L gives it type long double.

float single = 0.1f;
double normal = 0.1;
long double extended = 0.1L;

Mixing a float with an unsuffixed constant can cause a conversion to double during evaluation:

#include <stdio.h>

int main(void)
{
    float value = 0.1f;

    printf("float approximation:  %.17g\n", (double)value);
    printf("double approximation: %.17g\n", 0.1);
}

On common binary floating-point implementations, 0.1 is not exactly representable. The value stored in float is rounded to that type's precision. If it is compared with the double constant, it is promoted to double, but that conversion cannot recover the discarded precision; the two approximations can therefore compare unequal.

Using the matching suffix avoids an unintended type mismatch, but it does not make decimal fractions exact. For numerical algorithms, choose comparison tolerances from the problem's error bounds instead of applying one arbitrary epsilon to every calculation.

Performance differences between float and double are target-specific. Select a type primarily from the required range, precision, storage, and interface constraints, then measure performance on the target if it matters.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents