Jakub Zaverka - 1 year ago 62
C++ Question

# Why cannot floating-point promotion work for arithmetics as well?

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.

Converting a `float` to a `double` costs something, and it's likely more expensive than a `short` to `int` conversion (it needs several shifts and bit combining operations). And unlike e.g. `short`, the `float` type is considered something on which the processor can operate directly (just like it can on `int`).

Given the facts obove, why should floating-point promotion happen when it's not necessary? That is, if you're adding two `float`s, why convert them to `double`, add them, and then convert them back to `float`?(1)

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 `double`.

As per [conv.fpprom], this conversion from `float` to `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 `float`s directly, and [expr].12 explicitly allows that. But that very paragraph says

the types are not changed thereby.

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