C++ 0 和 0.0 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1384434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ difference between 0 and 0.0
提问by Rob
Is there a difference between 0 and 0.0 in C++? Which should you use for initializing a double?
C++ 中的 0 和 0.0 之间有区别吗?您应该使用哪个来初始化双精度值?
Thanks
谢谢
回答by Rob
A literal 0
is considered to be an int
literal; literal 0.0
is a double
literal. When assigning to a double
, either will work (since the int
can be cast in a widening conversion); however, casting 0.0
to an int
is a narrowing conversion, and must be done explicitly; i.e. (int)0.0
.
文字0
被认为是int
文字;文字0.0
是double
文字。分配给 a 时double
,两者都可以工作(因为int
可以在扩大转换中进行转换);但是,转换0.0
为 anint
是一种缩小转换,必须显式完成;即(int)0.0
。
回答by Nosredna
I try to keep my constants type-consistent. 0 for ints. 0.0f or 0.f for float, and 0.0 for double.
我尽量保持我的常量类型一致。0 表示整数。浮点数为 0.0f 或 0.f,双精度数为 0.0。
To me, the most important reason to do this is so the compiler and the programmer see the same thing.
对我来说,这样做的最重要原因是编译器和程序员看到相同的东西。
If I do this...
如果我这样做...
float t=0.5f;
float r;
r= 1 * t;
...should r be assigned 0 or .5? There's no hesitation if I do this instead...
...应该将 r 指定为 0 还是 .5?如果我这样做,我会毫不犹豫...
float t=0.5f;
float r;
r= 1.0f * t;
回答by Jim Dennis
One appears to be an integer literal, the other a floating point literal. It really doesn't matter to the compiler whether you initialize floats or doubles with integer literals. In any event the literal will be compiled into some internal representation.
一个似乎是整数文字,另一个是浮点文字。对于编译器来说,是否使用整数文字初始化浮点数或双精度数实际上并不重要。在任何情况下,文字都会被编译成某种内部表示。
I would tend to suggest 0.0 in order to make your intention (to other programmers) explicitly clear.
我倾向于建议 0.0 以明确您的意图(对其他程序员)。
回答by Jim Dennis
Here is the Visual-C++ disassembly for:
这是 Visual-C++ 反汇编:
int main() {
double x = 0;
//
double y = 0.0;
//
double z = x * y;
return 0;
}
Disassembly
拆卸
int main() {
00007FF758A32230 push rbp
00007FF758A32232 push rdi
00007FF758A32233 sub rsp,128h
00007FF758A3223A mov rbp,rsp
00007FF758A3223D mov rdi,rsp
00007FF758A32240 mov ecx,4Ah
00007FF758A32245 mov eax,0CCCCCCCCh
00007FF758A3224A rep stos dword ptr [rdi]
double x = 0;
00007FF758A3224C xorps xmm0,xmm0
00007FF758A3224F movsd mmword ptr [x],xmm0
//
double y = 0.0;
00007FF758A32254 xorps xmm0,xmm0
00007FF758A32257 movsd mmword ptr [y],xmm0
//
double z = x * y;
00007FF758A3225C movsd xmm0,mmword ptr [x]
00007FF758A32261 mulsd xmm0,mmword ptr [y]
00007FF758A32266 movsd mmword ptr [z],xmm0
return 0;
00007FF758A3226B xor eax,eax
}
00007FF758A3226D lea rsp,[rbp+128h]
00007FF758A32274 pop rdi
00007FF758A32275 pop rbp
00007FF758A32276 ret
They produced the same assembly. Visual-C++ uses the method of XOR-ing the XMM register with it'self. Had you first loaded the integer 0 then moved it into the XMM register it would have used an extra instruction. Given our hypothesis on how the 0.0
may be loaded as a literal, as well as the extra useless instruction load loading an integer 0 then moving it into a floating-point register, neither is optimal so it seems as though it really doesn't matter because the compiler writer we must assume this optimization has been know for a long time because it is kind of obvious. If 100% portability is required, then it is more portable to write an inline function that manually uses the XOR technique.
他们生产了相同的组件。Visual-C++ 使用 XMM 寄存器与其自身进行异或的方法。如果您首先加载整数 0,然后将其移动到 XMM 寄存器中,它将使用额外的指令。考虑到我们关于如何0.0
将 加载为文字的假设,以及额外无用的指令加载加载一个整数 0 然后将它移动到一个浮点寄存器中,两者都不是最佳的,所以看起来好像真的无关紧要,因为编译器编写者我们必须假设这种优化已经知道很长时间了,因为它有点明显。如果需要 100% 的可移植性,那么编写一个手动使用 XOR 技术的内联函数会更具有可移植性。