ROS

C++ - Rounding

Posted by Rico's Nerd Cluster on April 18, 2024

llround

llround follows round half away from zero (not bankers rounding)., and returns long long

1
2
3
4
5
6
7
std::llround(2.3)     2
std::llround(2.5)     3
std::llround(2.7)     3

std::llround(-2.3)    -2
std::llround(-2.5)    -3
std::llround(-2.7)    -3

std::round returns double

1
std::round(2.5);   // 3.0 (double)
  • Banker’s rounding is to round .5 to the nearest even integer
1
2
3
2.5 2 ← (2 is even), 
2.6 → 3
3.5 4 ← (4 is even)