This only applies to floating point literals .
This is the same as saying that any integer that you write in code is always treated as a (signed) int . Once you assign this to a variable, you will get the type of the variable.
However, when using stand-alone literals in a calculation, you get the literal type for this calculation, potentially causing implicit type conversions:
float f = 3.141; // f is of type float, even though the literal was double auto d = f * 2.0; // d will be of type double because of the literal 2.0 auto f2 = f * 2.0f; // f2 will be of type float again
The calculation in the second line includes two different types: The type of the variable f is float . Although it was built from a double literal, the type of the variable is what counts. The literal type 2.0 , on the other hand, is double and therefore triggers an implicit conversion for computation. Therefore, the actual multiplication is performed as a multiplication by two double s.
If you want a particular value to have a specific type, use the appropriate literal.
source share