Skip to content

Latest commit

 

History

History
57 lines (32 loc) · 2.98 KB

File metadata and controls

57 lines (32 loc) · 2.98 KB

 

 

 

 

 

 

RoundOff is a math code snippet to round a double to the nearest integer.

 


//From http://www.richelbilderbeek.nl/CppRoundOff.htm int RoundOff(const double x) {   return (x > 0.0     ? static_cast<int>(x + 0.5)     : static_cast<int>(x - 0.5)); }

 

 

 

 

 

 


//From http://www.richelbilderbeek.nl/CppRoundOff.htm int RoundOff(const double x) {   return (x > 0.0     ? static_cast<int>(x + 0.5)     : static_cast<int>(x - 0.5)); } #include <cassert> int main() {   assert(RoundOff(-2.4)==-2);   assert(RoundOff(-1.6)==-2);   assert(RoundOff(-1.5)==-2);   assert(RoundOff(-1.4)==-1);   assert(RoundOff(-0.6)==-1);   assert(RoundOff(-0.5)==-1);   assert(RoundOff(-0.4)== 0);   assert(RoundOff(-0.1)== 0);   assert(RoundOff( 0.1)==0);   assert(RoundOff( 0.4)==0);   assert(RoundOff( 0.5)==1);   assert(RoundOff( 0.6)==1);   assert(RoundOff( 1.4)==1);   assert(RoundOff( 1.5)==2);   assert(RoundOff( 1.6)==2);   assert(RoundOff( 2.4)==2); }