表达式中的隐式类型转换 int 到 double
当我在代码中使用命名常量时,我一直在尝试减少隐式类型转换。例如,
const double foo = 5;
我不会使用,
const double foo = 5.0;
而是使用,这样就不需要进行类型转换。但是,在我执行类似操作的表达式中...
const double halfFoo = foo / 2;
等等。 2 是否被计算为整数并且是否隐式转换?我应该使用 2.0 吗?
I've been trying to reduce implicit type conversions when I use named constants in my code. For example rather than using
const double foo = 5;
I would use
const double foo = 5.0;
so that a type conversion doesn't need to take place. However, in expressions where I do something like this...
const double halfFoo = foo / 2;
etc. Is that 2 evaluated as an integer and is it implicitly converted? Should I use a 2.0 instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
2
隐式转换为双精度型,因为foo
是双精度型。您必须小心,因为如果foo
是整数,则会执行整数除法,然后结果将存储在halfFoo
中。我认为无论您打算将浮点文字用作浮点值,还是始终使用浮点文字(例如
2.0
或2.
)是一种很好的做法。它更加一致并可以帮助您找到可能与此类事件一起出现的有害错误。The
2
is implicitly converted to a double becausefoo
is a double. You do have to be careful because iffoo
was, say, an integer, integer division would be performed and then the result would be stored inhalfFoo
.I think it is good practice to always use floating-point literals (e.g.
2.0
or2.
wherever you intend for them to be used as floating-point values. It's more consistent and can help you to find pernicious bugs that can crop up with this sort of thing.这称为类型强制。维基百科对此有很好的介绍:
对于C 和C++,整型(即长整型、整型、短整型、字符型)表达式的值是表达式中最大的整型类型。我不确定,但我想象涉及浮点数的表达式会发生类似的情况(假设浮点值比整数类型“更大”)。
This is known as Type Coercion. Wikipedia has a nice bit about it:
In the case of C and C++, the value of an expression of integral types (i.e. longs, integers, shorts, chars) is the largest integral type in the expression. I'm not sure, but I imagine something similar happens (assuming floating point values are "larger" than integer types) with expressions involving floating point numbers.
严格来说,你想要实现的目标似乎适得其反。
通常,人们会努力减少 C 程序中显式类型转换的数量,并且通常会减少源代码中的所有类型依赖性。好的 C 代码应该尽可能与类型无关。这通常意味着最好避免使用任何尽可能频繁地阐明特定类型的显式语法元素。 做更好,
这样做比这样
因为后者是多余的。 C语言的隐式类型转换规则将确保前者正确工作。关于比较也可以这样说。这
比前者更好
,因为前者更独立于类型。
在这种情况下,隐式类型转换是一件非常好的事情,而不是一件坏事。它可以帮助您编写通用的与类型无关的代码。你为什么要试图避开他们?
确实,在某些情况下,您别无选择,只能显式表达类型(例如使用
2.0
而不是2
等)。但通常情况下,只有当确实有必要时,人们才会这样做。我无法理解为什么有人会在没有真正需要的情况下这样做。Strictly speaking, what you are trying to achieve seems to be counterproductive.
Normally, one would strive to reduce the number of explicit type conversions in a C program and, generally, to reduce all and any type dependencies in the source code. Good C code should be as type-independent as possible. That generally means that it is a good idea to avoid any explicit syntactical elements that spell out specific types as often as possible. It is better to do
than
because the latter is redundant. The implicit type conversion rules of C language will make sure that the former works correctly. The same can be said about comparisons. This
is better than
because, again, the former is more type-independent.
Implicit type conversion in this case is a very good thing, not a bad thing. It helps you to write generic type-independent code. Why are you trying to avoid them?
It is true that in some cases you have no other choice but to express the type explicitly (like use
2.0
instead of2
and so on). But normally one would do it only when one really has to. Why someone would do it without a real need is beyond me.