coddding coddding - 1 year ago 117
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

with a

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download