Search code examples
cdoubleprecision

No differences between float, double and long double?


I recently noticed that a variable in double or long double have the same précision: 15 digits. I've read that on x86/x64 the long double should have at least 20 digits.

What's wrong? Is there anything wrong?

#include <stdio.h>

int main() {
    long double u = 1.23456789012345678901234567890123456789;
    double v = (double)u;
    float w = (float)u;
    printf("%f\n%lf\n%Lf\n", w, v, u);
    printf("%.20f\n%.20lf\n%.20Lf\n", w, v, u);
}

Here the output:

1.234568
1.234568
1.234568
1.23456788063049316406
1.23456789012345669043
1.23456789012345669043

Solution

  • The C standard does not require that a long double have any extra precision than a double, just that it has at least the same. From this C11 Draft Standard (Annex F):

    F.2 Types

    1. The C floating types match the IEC 60559 formats as follows:
    • The float type matches the IEC 60559 single format.
    • The double type matches the IEC 60559 double format.
    • The long double type matches an IEC 60559 extended format, else a non-IEC 60559 extended format, else the IEC 60559 double format.

      Any non-IEC 60559 extended format used for the long double type shall have more precision than IEC 60559 double and at least the range of IEC 60559 double.

    The key parts of this are those that I have emboldened, in the third sub-bullet point and in the last paragraph.

    Further, the Intel x86/x64 architecture has no intrinsic support for extended-precision floating point operations, so any compiler that does implement them has to do so via 'home-built' libraries (such as that provided by the Intel© C/C++ Compiler).