c++ - 分配给 double 的数字上的 F 后缀

标签 c++ c

我知道在 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/

相关文章:

c++ - 如何在 Windows 中编译 scintilla?

C++ OOP 无法正确存储和显示内容

C 从参数字符串中解析 double

多个进程可以共享其堆内存吗?

c++ - 使用 fread 读入 int 缓冲区

c - pthread_exit 问题返回结构

C++ 模板结构 'has no member named' 错误

c++ - 是否有像 eclipse cdt 这样的轻型 linux c++ IDE+Visual 调试器?

c++ - 错误 C2593 : 'operator =' is ambiguous For string template

c - c w/h 终端命令 : ./process arg | 中的管道./进程参数