C++11 introduced the std::nearbyint and std::round functions. Both return the "nearest" integer value.
When and where should I prefer one over the other?
I have tested them with the value of 0.5:
Case 1 : Demo for nearbyint
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"nearbyint(0.5) = "<<std::nearbyint(0.5);
}
output: 0
Case 2 : Demo for round
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"round(0.5) = "<<std::round(0.5);
}
output: 1
Why are the outputs different?