Floats cast to double

Jul 12, 2013 at 9:52pm
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 Jul 12, 2013 at 9:52pm
Jul 12, 2013 at 10:00pm
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.
Jul 12, 2013 at 10:02pm
Why does it store double and not float instead?
Jul 12, 2013 at 10:36pm
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 Jul 12, 2013 at 10:37pm
Jul 12, 2013 at 10:40pm
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 Jul 12, 2013 at 10:42pm
Topic archived. No new replies allowed.