The type of 0.5 in C++ is double. If you want a float you need to say 0.5f . This is part of the definition of C and C++.
Implied in your question is some desire for automatic type selection based on the number of digits in the value, e.g. we could use float for 0.5 but double for 0.123456789. This is not a tenable approach however, firstly because it is overly complex for compilers written in the 1980’s, and second because it makes it harder to reason about code, e.g. what would be the type of 0.123 / 0.987
?
1
solved Decltype(0.5) x = 5; makes x *Double* rather than *Float*. Why? [duplicate]