Floats cast to double

When you declare something as float why (I'm assuming all compilers do this or maybe this is standard behavior in C++) is it cast to double; for me VS issues a warning about precision loss. If you add the f suffix to the variable is it then just a float?

A little off topic but when would you want to use a float over a double?

Thanks
Last edited on
Are you referring to a statement such as float f = 0.1; ?

The type of the literal 0.1 is double, the type of the literal 0.1f is float -- it's just C program syntax.

When you write float f = 0.1;, you're asking the compiler to store a double in an object of type float, so it might warn about precision loss.

You may want to use float over double when you're manipulating a billion numbers at the same time and space becomes a problem.
Why does it store double and not float instead?
The compiler isn't going to change code just because it thinks you meant something other than what you wrote. If you wrote a float on the left and a double on the right, as in float f = 0.1;, why should it replace that with float f = 0.1f; and not with double f = 0.1;?
Last edited on
Because you declared the variable f as a float so it should be a float and not a double? 0.1 is a double...?
Last edited on
Topic archived. No new replies allowed.