我知道在 float 后加上 f 可以让编译器知道它是 float 而不是 double 。这对于防止在 float 而不是 double 上操作时进行不必要的转换很重要。然而,在一些由外部公司提供的代码中,我看到了以下示例:
double i = 1.4f;
现在我想知道在这种情况下会发生什么?编译器会默默地忽略“f”吗,它只是与任何东西无关的东西,我可以安全地忽略它?
然后我想知道,如果数字在其他地方被分配给带有 f 后缀的 double ,而不仅仅是在初始化时,那会不会是一种不同的情况,表现不同?
最佳答案
C标准的相关引述:
§6.4.4.2 第 4 段
If suffixed by the letter f or F, it has type float.
第 5 段
Floating constants are converted to internal format as if at translation-time.
第 7 段
The translation-time conversion of floating constants should match the execution-time conversion of character strings by library functions, such as strtod, given matching inputs suitable for both conversions, the same result format, and default execution-time rounding.
假设 IEEE-754 数字 1.4f
产生值:
1.39999997615814208984375
而 1.4
的值为:
1.399999999999999911182158029987476766109466552734375.
关于c++ - 分配给 double 的数字上的 F 后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535770/