I know that double has more precision than float and all that, but during lecture, my professor said 0.5 is a double. Could it be float too?
short int s;
int i;
long int l;
float f;
double d;
l = 2 * s + i * f - 0.5 * d;
I know that double has more precision than float and all that, but during lecture, my professor said 0.5 is a double. Could it be float too?
short int s;
int i;
long int l;
float f;
double d;
l = 2 * s + i * f - 0.5 * d;
According to this SO question, the type of a floating point literal (i.e. a number with a decimal point in it) is by default double
unless it has a suffix of f
:
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.
So, your professor appears to be spot on. In the above expression 0.5
will be treated like a double
by default. I hope that you will get a high grade on the final exam.