表达式中的隐式类型转换 int 到 double

发布于 2024-08-12 15:12:30 字数 302 浏览 7 评论 0原文

当我在代码中使用命名常量时,我​​一直在尝试减少隐式类型转换。例如,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-08-19 15:12:30

2 隐式转换为双精度型,因为 foo 是双精度型。您必须小心,因为如果 foo 是整数,则会执行整数除法,然后结果将存储在 halfFoo 中。

我认为无论您打算将浮点文字用作浮点值,还是始终使用浮点文字(例如 2.02.)是一种很好的做法。它更加一致并可以帮助您找到可能与此类事件一起出现的有害错误。

The 2 is implicitly converted to a double because foo is a double. You do have to be careful because if foo was, say, an integer, integer division would be performed and then the result would be stored in halfFoo.

I think it is good practice to always use floating-point literals (e.g. 2.0 or 2. 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.

神爱温柔 2024-08-19 15:12:30

这称为类型强制。维基百科对此有很好的介绍:

隐式类型转换,也称为强制转换,是编译器自动进行的类型转换。有些语言允许甚至要求编译器提供强制转换。

在混合类型表达式中,可以在运行时根据需要将一种或多种子类型的数据转换为超类型,以便程序正确运行。

...

应谨慎使用此行为,因为可能会产生意想不到的后果。当浮点表示转换为整数表示时,数据可能会丢失,因为浮点值的小数部分将被截断(向下舍入)。相反,从整数表示转换为浮点表示也可能会丢失精度,因为浮点类型可能无法准确表示整数(例如,float 可能是 IEEE 754 单精度类型,它无法表示整数)整数 16777217 正好,而 32 位整数类型可以)。这可能会导致一些情况,例如将相同的整数值存储到整数类型和实数类型的两个变量中,如果比较相等则返回 false。

对于C 和C++,整型(即长整型、整型、短整型、字符型)表达式的值是表达式中最大的整型类型。我不确定,但我想象涉及浮点数的表达式会发生类似的情况(假设浮点值比整数类型“更大”)。

This is known as Type Coercion. Wikipedia has a nice bit about it:

Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. Some languages allow, or even require, compilers to provide coercion.

In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly.

...

This behavior should be used with caution, as unintended consequences can arise. Data can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded down). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly (for example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to situations such as storing the same integer value into two variables of type integer and type real which return false if compared for equality.

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.

冧九 2024-08-19 15:12:30

严格来说,你想要实现的目标似乎适得其反。

通常,人们会努力减少 C 程序中显式类型转换的数量,并且通常会减少源代码中的所有类型依赖性。好的 C 代码应该尽可能与类型无关。这通常意味着最好避免使用任何尽可能频繁地阐明特定类型的显式语法元素。 做更好,

const double foo = 5; /* better */

这样做比这样

const double foo = 5.0; /* worse */

因为后者是多余的。 C语言的隐式类型转换规则将确保前者正确工作。关于比较也可以这样说。这

if (foo > 0)

比前者更好

if (foo > 0.0)

,因为前者更独立于类型。

在这种情况下,隐式类型转换是一件非常好的事情,而不是一件坏事。它可以帮助您编写通用的与类型无关的代码。你为什么要试图避开他们?

确实,在某些情况下,您别无选择,只能显式表达类型(例如使用 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

const double foo = 5; /* better */

than

const double foo = 5.0; /* worse */

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

if (foo > 0)

is better than

if (foo > 0.0)

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 of 2 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文