CppCoreGuidelines
CppCoreGuidelines copied to clipboard
Guideline for dividing two named integers
What is the recommended way of avoiding truncation of the decimal part of the result in the following example?
int a = 3;
int b = 2;
double c = a / b; // c == 1
Is any of those:
double c1 = static_cast<double>(a) / b;
double c2 = double(a) / b;
double c3 = (a + 0.0) / b;
preferred over the others ?
Having the official recommendation would be useful, as there are still many suggestions to use the C-style casting, i.e.: c=(double)a/b; available.