CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Guideline for dividing two named integers

Open DEVoytas opened this issue 5 years ago • 0 comments

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.

DEVoytas avatar Sep 19 '20 11:09 DEVoytas