问题
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?
回答1:
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 typedouble
. 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
.
回答2:
The primary (perhaps sole) time that floating point promotions are applied is when passing an argument to a variadic function (e.g., printf
).
In this case, the usual arithmetic conversions don't apply (they're for finding a common type between two operands in an expression).
The relevant part of the standard is [expr.call]/7 (at least as of N4296):
When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg (18.10).
[...]
If the argument has integral or enumeration type that is subject to the integral promotions (4.5), or a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call.
来源:https://stackoverflow.com/questions/30490592/no-really-when-does-floating-point-promotion-actually-happen