Which is the shortest form of this function(without changing the name of this)?
int NearestInteger(float N)
{
return round(N);
}
You can use std::round function if you have C++11.
Otherwise, you may need to use one of them depends on your requirement std::floor or std::ceil
If you don't have C++11, you can do this:
int NearestInteger(float N) {
return int(floor(N + 0.5f));
}
However, it may work incorrectly for large floats. First of all, if your number does not fit int , any trash can be returned. Second, it may return wrong results for numbers larger than 2^24, because not each integer is exactly representable in single precision float number .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.