Bryan Bryan - 1 year ago 106
C++ Question

No really, when does floating point promotion actually happen?

From this other QUESTION they talk about how Bjarne Stroustrup said that just as integral data-types narrower than an

) are promoted to an
s are promoted to a
. However, unlike widening of integrals narrower than an
, floating point promotion does not happen in the same way, but instead, occurs elsewhere.

I know that if you were to compute
float + double
would be converted to a
before the binary operator(
) is applied. However, this is not floating point promotion according to This is usual arithmetic conversion.

When does floating point promotion actually happen?

Answer Source

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 floats 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);

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);

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download