gpt4 book ai didi

C++ : Implicit type conversion

转载 作者:太空狗 更新时间:2023-10-29 19:49:11 27 4
gpt4 key购买 nike

我对隐式类型转换有点困惑。给定以下程序

   float x = 4.23423451;
double y = 4.23423451;

float z = 101.9876;

float res1 = x * z;
float res2 = y * z;

std::cout << "res1 & res2 " << res1 << " & " << res2 << std::endl;
std::cout << "equality " << (res1 == res2) << std::endl;

输出是

   res1 & res2 431.839  & 431.839
equality 1

我的问题是“对于 x、y 和 z (x = y) 的任何值以及任何编译器,相等性是否始终为真?”

res2 = y * z;

变量“y”会被类型转换为 float 还是变量“z”会被类型转换为 double?

最佳答案

参见 my comments .

This is well-defined. The intermediate expression for z will undergo widening into double, so y * z will be a double expression. An implicit narrowing conversion will then convert it to float for storing in res2. This same narrowing applies to res1.

这反射(reflect)在 C++11 标准的 §5¶9 表达式 [expr] 中。

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

...

  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.

...

然而,这并不能确定等式是否成立。

That being said, res1 need not necessarily be equivalent to res2 -- it is highly dependent on the precision of float and double in the environment. The two literals could potentially not even be equal -- 4.23423451f not need even be equivalent to 4.23423451. You can not be sure that static_cast<double>(static_cast<float>(4.23423451)) will be equal to 4.23423451.

参见§5.17¶3 赋值和复合赋值运算符 [expr.ass]

If the left operand is not of class type, the expression is implicitly converted (Clause 4) to the cv-unqualified type of the left operand.

§4 标准转化 [conv] 说明如下:

Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

...

  • Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.

§4.6 浮点提升 [conv.fpprom] 中所述,

  1. A prvalue of type float can be converted to a prvalue of type double. The value is unchanged.
  2. This conversion is called floating point promotion.

...和§4.8 float 转换 [conv.double],

  1. A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.

  2. The conversions allowed as floating point promotions are excluded from the set of floating point conversions.

这里的问题是,在多种情况下,我们的转换不是提升,而是缩小到可能精度较低的类型( doublefloat )。

本质上,任何时候你转换doublefloat ,您可能会失去精度。

关于C++ : Implicit type conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12396132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com