I had/have some confusion regarding rounding a float/double variable to the nearest integer. While googling, I came across this cppreference page for rounding. On that page, it says:
float round ( float arg ); // (1)
float roundf( float arg ); // (2)
double round ( double arg ); // (3)
long double round ( long double arg ); // (4)
double round ( IntegralType arg ); // (5)
Here, I don't understand why is the return value type float, double, or long double? For example, if I have a variable float x and 2.5 <= x < 3.5 then its rounded value is 3. That can be represented using just an int, right? So, why is the return value type of std::round not an int (or long it for long double)?
On that page, it also says:
1-5) Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
So, what does exactly "integer value to arg (in floating-point format)" mean?
To understand this std::round, I did one small experiment:
int main(int argc, char const *argv[]) {
float var = 3.14;
int rounded_var_int = std::round(var);
float rounded_var_float = std::round(var);
std::cout << rounded_var_int << ", " << rounded_var_float << std::endl; // 3, 3
return 0;
}
However, this made me even more confused because everything ran just fine.
So, the last question:
In the above code, int rounded_var_int = std::round(var) is just fine or do I need to explicitly cast it to int e.g. int rounded_var_int = static_cast<int>(std::round(var)) to avoid any potential bug(s)?