coddding coddding - 15 days ago 6
C++ Question

c++ Can I Initialize a variable of type float with a double?

Haven't really been able to find much about this. Though compiler doesn't seem to complain when I initialize a variable of type

float
with a
double
e.g.

double a = 1.0;
float b = a;


It works fine, but I am still curious if it is OK to do this, and/or is it safe?

(I am on a 32 bit system running windows)

Answer

The size of the created variable is different. "float = 4 bytes" and "double = 8 bytes". Hence, what is important here is the range of values are different for both types. If you use small values as 1.0 it should not effect the workflow.