C是否必须用科学计数法计算出一个数字?

Does C have to calculate out a number in scientific notation?

一个浮点数的最大值是3.40282347E+38。这相当于 3.40282347 * 10^38。我想知道如果我设置一个等于这个的浮点数,它是否必须实际执行幂和乘法以获得要存储的值,或者科学记数法只是实际值?

我正在编写一个学校项目,我们正在尽最大努力对其进行优化,因此这与我相关,尤其是因为它位于嵌套循环结构中。

当您在源代码中使用文字时,这些文字(主要是包含常量折叠的所有文字的表达式)会在编译时求值并转换为 move immediateadd immediatemove special_constant_register

例如:

int a = 10, b = 0xA;

此处 100xA 文字在编译时被评估为相同的值。在您的情况下, double 文字或 float 文字将在编译时进行评估,并将最合适的值分配给变量。

当您编写 3.4e38 时,它会在编译时完全计算。

当你写3.4 * 10^38时,你会得到一个完全出乎意料的结果。这是因为^在C中不是power-of的意思,是异或的意思,而10 ^ 38 == 44。或者它可能根本无法编译,因为它被计算为 (3.4 * 10) ^ 38,并且没有为浮点数定义 xor。或者它已被定义,所以它将是 340 ^ 10.

都是在编译时完成的。以下代码...

#include <float.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
  float foo = 3.40282347E+38;
  float bar = 3.40282347e38;
  printf("foo=%g\nbar=%g\n", foo, bar);
  return 0;
}

在汇编器中产生这个。要让汇编程序使用 -S 标志到 gcc,即,类似于

gcc -S -Wall -O3 -pedantic -std=c11 scientific_notation.c 

这是使用 clang。我会稍微剪下汇编器...

 .section  __TEXT,__text,regular,pure_instructions
  .macosx_version_min 10, 11
  .section  __TEXT,__literal8,8byte_literals
  .align  3
LCPI0_0:
  .quad 5183643170566569984     ## double 3.4028234663852886E+38
  .section  __TEXT,__text,regular,pure_instructions
  .globl  _main
  .align  4, 0x90

...剪断了。

注意这一行 .quad 5183643170566569984。这是一个常数。另请注意,即使我的代码中有两个这些常量,编译器也只需要一个。