I have read a bit about floating-point promotion. I know that it doesn't apply on binary arithmetic operations, only on e.g. overload resolution. But why?
The C++ standard guarantees that double must be at least as precise as float [basic.fundamental.8] and the floating point promotion is required to keep the value unchanged [conv.fpprom]. Yet this question makes it very clear that it does not happen. Stroustrup, 4th edition has the subject even errata-ed (here, Chapter 10, p. 267).
However, I cannot see any reason why the promotion cannot be done in usual arithmetic conversions [expr.10], even if all prerequisites are met. Is there any?
The latest C++14 working draft can be found here, the final version is purchase-only.
float to a
double costs something, and it's likely more expensive than a
int conversion (it needs several shifts and bit combining operations). And unlike e.g.
float type is considered something on which the processor can operate directly (just like it can on
Given the facts obove, why should floating-point promotion happen when it's not necessary? That is, if you're adding two
floats, why convert them to
double, add them, and then convert them back to
Note that a floating-point promotion will indeed happen when you're adding mixed arguments (e.g. a
float + double), by the very ruling in C++14 [expr] you're referring to.
- (10.3) Otherwise, if either operand is
double, the other shall be converted to
As per [conv.fpprom], this conversion from
double is carried out by floating point promotion.
(1) Of course, it is perfectly possible this will happen internally if the processor cannot operate on
floats directly, and [expr].12 explicitly allows that. But that very paragraph says
the types are not changed thereby.