没有,浮点提升实际发生在什么时候? [英] No really, when does floating point promotion actually happen?
问题描述
从这个其他问题他们谈论Bjarne Stroustrup如何表示,比 int
(例如 short
)窄的整数数据类型被提升为 int
, float
s被提升为 double
。然而,不像扩展比 int
更窄的积分, 浮点升级 不会以相同的方式发生,我知道如果你计算 float + double
二元运算符(
会被转换为 +
)被应用之前,> float double
。但是,根据 浮点推广 -coercion /rel =nofollow noreferrer> Learncpp.com 。这是 惯常的算术转换 。
何时浮点提升实际发生? b
$ b
类型的价值 float
可以转换为 double
类型的值。
$ b
这个转换称为浮点提升。
$ / b
链接问题的答案是正确的。这个升级不应该在添加两个 float
时自动发生,因为通常的算术转换不会提升浮点操作数。
<将 float
作为操作数传递给省略号时会发生浮点升级 ,如 printf
。这就是为什么%f
格式说明符会打印一个 float
或者一个 double
:如果你传递了一个 float
,这个函数实际上会收到一个 double
,这是促销的结果。 p>
浮点数提升的存在在重载解析中也很重要,因为整数升级和浮点数升级隐式转换排名比积分转换,浮点转换和浮点积分转换。
例子1:
void f(double);
void f(long double);
f(0.0f);
这个函数调用 void f(double)
升级到 double
优于转换为 long double
。相反,考虑这可能是令人惊讶的例子2:
void f(long double);
void f(int);
f(0.0f);
这是不明确的。从 float
到 long double
的转换并不比 float
到 int
,因为它们都不是促销。
示例3:
struct S {
operator float();
操作符int();
};
double d = S();
调用运算符float
,然后提升结果 float
值改为 double
来初始化 d
p>
From this other QUESTION they talk about how Bjarne Stroustrup said that just as integral data-types narrower than an int
(e.g. short
) are promoted to an int
, float
s are promoted to a double
. However, unlike widening of integrals narrower than an int
, floating point promotion does not happen in the same way, but instead, occurs elsewhere.
I know that if you were to compute float + double
the float
would be converted to a double
before the binary operator(+
) is applied. However, this is not floating point promotion according to Learncpp.com. This is usual arithmetic conversion.
When does floating point promotion actually happen?
解决方案 There is such a thing as "floating point promotion" of float
to double
per [conv.fpprom].
A prvalue of type float
can be converted to a prvalue of type double
. The value is unchanged.
This conversion is called floating point promotion.
The answers to the linked question are correct. This promotion should not occur automatically when adding two float
s since the usual arithmetic conversions do not promote floating-point operands.
Floating point promotion does occur when passing a float
as an operand to an ellipsis, like in printf
. That's why the %f
format specifier prints either a float
or a double
: if you pass a float
, the function actually receives a double
, the result of promotion.
The existence of the floating point promotion is also important in overload resolution, because integral promotions and floating point promotions have better implicit conversion rank than integral conversions, floating point conversions, and floating-integral conversions.
Example 1:
void f(double);
void f(long double);
f(0.0f);
This calls void f(double)
since the promotion to double
is better than the conversion to long double
. In contrast, consider this perhaps surprising example 2:
void f(long double);
void f(int);
f(0.0f);
This is ambiguous. The conversion from float
to long double
is no better than the conversion from float
to int
since they are both not promotions.
Example 3:
struct S {
operator float();
operator int();
};
double d = S();
This calls operator float
and then promotes the resulting float
value to double
to initialize d
.
这篇关于没有,浮点提升实际发生在什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!