float、double 和 long double 之间没有区别吗?

No differences between float, double and long double?

我最近注意到 doublelong double 中的变量具有相同的精度:15 位。我在 x86/x64 上读到,长双精度数至少应有 20 位数字。

怎么了?有什么问题吗?

#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);
}

此处输出:

1.234568
1.234568
1.234568
1.23456788063049316406
1.23456789012345669043
1.23456789012345669043
#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);
    printf("%.40Lf\n",u);  /* print out the long double not limiting it to 20 places */
};

请告诉我更多你想要的...

C 标准不要求 long doubledouble 具有任何额外的精度,只是它至少具有相同的精度。从此C11 Draft Standard(附件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.

本文的关键部分是我在第三个子项目符号点和最后一段中鼓励的部分。

此外,Intel x86/x64 架构没有内部 对扩展精度浮点运算的支持,因此 支持的任何编译器 实现它们必须通过 'home-built' 库(例如 Intel© C/C++ 编译器提供的库)。