条件语句中的 0 与 0.0
之间有区别吗?
double dandouble = 5.23493; //or some other random number
if (dandouble < 0.0)
dandouble = 3.5;
和
double dandouble = 5.23493; //or some other random number
if (dandouble < 0)
dandouble = 3.5;
或者它们会产生相同的结果吗?
Is there a difference between:
double dandouble = 5.23493; //or some other random number
if (dandouble < 0.0)
dandouble = 3.5;
and
double dandouble = 5.23493; //or some other random number
if (dandouble < 0)
dandouble = 3.5;
Or will they turn out to result the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译器必须发出 Opcodes.Clt IL 指令 进行比较。 CLI 规范规定了指令可接受的参数,不允许使用 double 和 int。意识到这些规则后,编译器会提升参数以获得有效的组合,double 和 double 是第一个匹配项。它有足够的智能来识别 int 参数是一个文字。因此不会发出任何 IL 来进行转换,它直接发出 Opcodes.Ldc_R8 为 0.0
没有区别。
The compiler must emit the Opcodes.Clt IL instruction to make the comparison. The CLI spec dictates the acceptable arguments for the instruction, double and int are not permitted. Aware of those rules, the compiler promotes an argument to get to a valid combination, double and double are the first match. It has enough smarts to recognize that the int argument is a literal. So doesn't emit any IL to make the conversion, it directly emits an Opcodes.Ldc_R8 for 0.0
No difference.
根据上述测试,我认为 C# 选择损失最小的转换。 (不偏向左侧或右侧)
所以回答你的问题,不。没有什么区别。
Given the above test, I'd say C# opts for the least lossy conversion. (Not a preference for left or right side)
So to answer your question, no. There isn't a difference.
没有区别。他们是一样的。
No difference. They are the same.