gpt4 book ai didi

c++ - 打印非常大的 float

转载 作者:IT老高 更新时间:2023-10-28 23:01:45 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <float.h>

int main()
{
printf("%f\n", FLT_MAX);
}

来自 GNU 的输出:

340282346638528859811704183484516925440.000000

Visual Studio 的输出:

340282346638528860000000000000000000000.000000

C 和 C++ 标准是否允许这两种结果?还是他们要求特定的结果?

请注意,FLT_MAX = 2^128-2^104 = 340282346638528859811704183484516925440

最佳答案

我认为 C99 标准的相关部分是 7.19.6.1 p.13 中的“推荐实践”:

For e, E, f, F, g, and G conversions, if the number of significant decimal digits is at most DECIMAL_DIG, then the result should be correctly rounded. If the number of significant decimal digits is more than DECIMAL_DIG but the source value is exactly representable with DECIMAL_DIG digits, then the result should be an exact representation with trailing zeros. Otherwise, the source value is bounded by two adjacent decimal strings L < U, both having DECIMAL_DIG significant digits; the value of the resultant decimal string D should satisfy L <= D <= U, with the extra stipulation that the error should have a correct sign for the current rounding direction.

我的印象是,这允许在这种情况下打印的内容有一些余地;所以我的结论是VS和GCC在这里都是合规的。

关于c++ - 打印非常大的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com