For a single literal constant, this does not matter. In the context of the initializer, a constant of any numeric type will be implicitly converted to the type of the object being initialized. This is guaranteed by the locale. So, all:
float x = 0; float x = 0.0; float x = 0.0f; float x = 0.0L; // converted from long double to float
are equally valid and lead to the fact that the same value is stored in x .
A literal constant in a more complex expression may have unexpected results.
In most cases, each expression is evaluated on its own, regardless of the context in which it appears. Any implicit conversion is applied after evaluating the subexpression.
So if you write:
float x = 1 / 2;
expression 1 / 2 will be evaluated as int , which will give 0 , which is then converted to float. It will set float. It will set x to 0.0f , not to 0.5f`.
I think you should be safe using unsuffixed floating point constants (which are of type double ).
By the way, you can use double instead of float in your program. double , as I mentioned, is a type of an undefined floating point constant and in a sense can be considered a default floating point type. It usually has a greater range and accuracy than float , and usually there are not many differences in performance.
Keith thompson
source share