What is a correct way to convert double to float in c++. Is the conversion implicit?
Question 1: Consider double d = 5.0; and float f;
Which one is correct?
f = d;f = (float)d;f = static_cast<float>(d);
Question 2: Now consider we have
char *buffer = readAllBuffer();
double *d = (double*)(buffer + offset);
float f;
Which one is now correct?
f = d[0];f = (float)d[0];f = static_cast<float>(d[0]);
Thanks in advance!
doublecan store more than afloatcan so can you loose precision or the value completelydoublevalue in either case and want to convert to afloatvalue. In other words, how you obtain thedoubleis independent of converting it tofloat.fvalue.d.