简体   繁体   English

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

[英]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.我最近注意到doublelong double中的变量具有相同的精度:15 位。 I've read that on x86/x64 the long double should have at least 20 digits.我在 x86/x64 上读过,long double 应该至少有 20 位数字。

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:这里是 output:

1.234568
1.234568
1.234568
1.23456788063049316406
1.23456789012345669043
1.23456789012345669043

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

F.2 Types F.2 类型

  1. The C floating types match the IEC 60559 formats as follows: C 浮动类型符合 IEC 60559 格式,如下所示:
  • The float type matches the IEC 60559 single format. float类型与 IEC 60559 单一格式匹配。
  • The double type matches the IEC 60559 double format. double精度类型与 IEC 60559 双精度格式匹配。
  • The long double type matches an IEC 60559 extended format, else a non-IEC 60559 extended format, else the IEC 60559 double format . long double类型匹配 IEC 60559 扩展格式,否则匹配非 IEC 60559 扩展格式,否则匹配IEC 60559 double 格式

    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.用于 long double 类型的任何非 IEC 60559扩展格式应具有比 IEC 60559 double 更高的精度,并且至少在 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).此外,英特尔 x86/x64 架构没有对扩展精度浮点运算的内在支持,因此任何实现它们的编译都必须通过“自制”库(例如英特尔提供的© C/C++编译器)。

#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 */
};

Might me more what you are after...可能我更多的是你所追求的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM