No really, when does floating point promotion actually happen?
Asked Answered
A

2

11

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, floats 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?

Affaire answered 27/5, 2015 at 18:40 Comment(4)
read through the comments and answers you'll see the Bjarne's book is in error.Zingaro
possible duplicate of floating-point promotion : stroustrup vs compiler - who is right?Zingaro
@RichardHodges I've read through the comments. Even if that small excerpt was taken out of the book, that does not tell me if floating point promotion was taken out of the language. I still have no idea of when floating point promotion can occur.Affaire
This thread is also relevant: #28098064Rainout
I
17

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

Im answered 27/5, 2015 at 18:57 Comment(2)
Your answer is so detailed and has great examples. I completely get floating point promotion now. Thank you so much!Affaire
Isn't float to long double a "promotion" as opposed to "conversion"?Spa
E
7

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.

Evenson answered 27/5, 2015 at 18:47 Comment(2)
Are you talking about type conversion? I know that type conversion happens if you pass a float to a variadic function that has a double as it's parameter. I don't believe type conversion and floating point promotion are the same thing. If you meant something else, will you please elaborate?Affaire
Oh! You're talking about ellipses. Where the parameter could be any value and any number of values. So that's where floating point promotion can happen. Thank you!Affaire

© 2022 - 2024 — McMap. All rights reserved.