TruthOrDare - 1 year ago 78

C++ Question

I'm currently learning C++ thought C++ Primer. In chapter 4.11 on Type Conversion, I noticed that the chapter rarely talks about the implicit conversion from integral to floating point types, but really focused mostly on conversion within the integral types. Thus, I am not really sure about the rules of conversion from integral type to floating point type.

My question is from the chapter's example:

`int ival = 3.541 + 3`

The chapter mentioned that 3 is converted to a

`double`

The question stems from the fact that for most integral types, they are mostly promoted to

`int`

`int`

`int`

`float`

`float`

`double`

`float`

`double`

Thanks a bunch!

Answer Source

`3.541`

is a double. So that's what the other argument needs to be converted to.

Suffixes are used to denote the precision of a literal, `3.541f`

(or F) would be a float, `3.541L`

(or l) would be a long double. The default (no suffix) is double. Source http://en.cppreference.com/w/cpp/language/floating_literal .

3.541 does not fit into any of those lengths, as it's a repeating fraction in binary. The real value will be the floating point value as close to 3.541 as possible (like 3.540999999999999925393012745189480483531951904296875 in case of a double).