floating-point promotion : stroustrup vs compiler - who is right?
Asked Answered
C

2

36

In section 10.5.1 of Stroustrup's new book "The C++ Programming Language - Fourth Edition" he says, that before an arithmetic operation is performed, integral promotion is used to create ints out of shorter integer types, and similarly, floating-point promotion is used to create doubles out of floats.

I confirmed the first claim with the following code:

#include <iostream>
#include <typeinfo>

int main()
{
    short a;
    short b;
    std::cout << typeid(a + b).name() << std::endl;
}

This outputs "int" with vc++ and "i" with gcc.

But testing it with floats instead of shorts, the output is still "float" or "f":

#include <iostream>
#include <typeinfo>

int main()
{
    float a;
    float b;
    std::cout << typeid(a + b).name() << std::endl;
}

According to Stroustrup there are no exceptions to the floating-point promotion-rule, so I expected "double" or "d" as output.

Is the mentioned section about promotions wrong or somehow unclear? And is there any difference in C++98 and C++11 regarding type promotions?

Conall answered 20/6, 2013 at 13:53 Comment(9)
I believe the promotion from float to double only occurs in case of mixed arithmethic; that is when adding a float and a double together the float is first promoted before the addition is carried out. Are you sure it is not the case ?Rosierosily
int is the "minimum level" for integral arithmetic in C++, and float is the "minimum level" for floating-point arithmetic.Toolis
I just cited the book. He explicitly says that floats get promoted to doubles. Also he does nor mentioned "mixed arithmetic", only "before an arithmetic operation".Conall
reformulation: good vs compiler - who is right?Chiccory
The rule used to be as Stroustrup described. It changed quite some time ago, but the corresponding text in the book was not updated.Dutcher
It is worth to mention there is nothing like this in Third Edtion, ch. In "C.6.3 Usual Arithmetic Conversions" These conversions are performed on the operands of a binary operator to bring them to a common type, which is then used as the type of the result: ... – Otherwise, if either operand is d o u b l e , the other is converted to d o u b l e . – Otherwise, if either operand is f l o a t , the other is converted to f l o a t . – Otherwise, integral promotions (§C.6.1) are performed on both operands.Swatch
I don't know the book but maybe he's referring to a case unrelated to C++ like x87 fpuRegalado
In the meantime Stroustrup has removed the sentence, about the floating-point promotion, from the book. Please refer errata on his webpage.Teacake
From the errata: pg 267: s/Similarly, floating-point promotion is used to create doubles out of floats// (for those unfamiliar with that syntax, it means that sentence should be removed)Spoony
B
30

I don't know what exactly Stroustrup's book says, but according to the standard, floats will not be converted to doubles in this case. Before applying most arithmetic binary operators, the usual arithmetic conversions described in 5p9 are applied:

  • If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
  • If either operand is of type long double, the other shall be converted to long double.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.
  • Otherwise, the integral promotions (4.5) shall be performed on both operands. [...]

The integral promotions are what causes two shorts to be converted to ints. But two floats will not be converted to doubles according to these rules. If you add a float to a double, the float will be converted to a double.

The above is from C++11. C++03 contains the same rules, except for the one referring to scoped enumerations.

Billings answered 20/6, 2013 at 14:2 Comment(5)
This seems to match OP's experience. I'd say the Standard is right. Maybe a typo in the book? +1.Depredation
4.6 Floating point promotion [conv.fpprom] 1 A prvalue of type float can be converted to a prvalue of type double . The value is unchanged. 2 This conversion is called floating point promotion . comes from this standard - is it a different one?Denver
So there is no default floating-type promotion? Stroustrup says that the purpose of the promotions is to bring the operands to the "natural" size for arithmetic operations. The "natural" floating-point type is double so it seems like a bug in the book.Conall
@fiscblog: That just defines what a "floating point promotion" is. It doesn't say when it is performed. When adding two floats, floating point promotion is not performed. The cases when it is performed are described elsewhere in the standard.Billings
@kiba: I can't judge the book without seeing exactly what it says. As fiscblog said, there is a thing called "floating point promotion" that converts floats to doubles. But it is not performed for arithmetic operations.Billings
T
6

In the meantime Stroustrup seems to recognised the refer sentence is not correct or at least misleading. He has removed the sentence, about the floating-point promotion, from section 10.5.1.

Please see errata of 3rd printing of 4th edition on Stroustrup's web page:

pg 267: s/Similarly, floating-point promotion is used to create doubles out of floats//

(Remark: The expression s/regexp/replacement/ is similar to sed unix tool semantics. It searches for the pattern regexp and replaces it with replacement. Nothing in our case.)

Teacake answered 20/9, 2014 at 18:57 Comment(3)
This should be a comment not an answer.Loram
@EWit: Tom8128 hasn't earned the commenting privilege.Spoony
@Tom8128: I've +1 you because you're answer is right, and I've added a comment to the discussion under the question to mention the errata. However, I suggest that you modify your question to include a link to the errata and a relevant quote of it, and a small explanation. As it is, the answer is short and needs some expansion for it to be great. In the future, when you've earned the commenting privilege, things like this should be comments. That's why I suggest you expand this answer: to turn it into a proper answer.Spoony

© 2022 - 2024 — McMap. All rights reserved.