Nice rounding function for C/C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Nice rounding function for C/C++

Post by Saman » Tue Dec 09, 2014 1:11 am

I found a nice C/C++ rounding function said to be from IEEE 754. Hope this will be helpful !

Code: Select all

/*  round_even
 *
 *  Rounds a number to the nearest digit according to the precision provided.
 *  The tie breaking rule known as round half to even is implemented.
 *    
 *  Prototype:
 *    template <typename T>
 *    T round_even(T x, T precision)
 *
 *  Parameters:
 *    x - The number to be rounded.
 *    precision - The increment in which x will be rounded to.
 *
 *  Type:
 *    T must be either a float, a double, or a long double.
 *
 *  Return Value:
 *    The return value is the rounded number.
 *
 *  Example: 
 *    round_even<double>(1.55, 0.1);   // returns 1.6
 *    round_even<double>(1.265, 0.01); // returns 1.26
 */
 
template <typename T>
T round_even(T x, T precision)
{
  T result, integral, fractional;
  // parity_flag - false : true :: odd : even
  // sign_flag - false : true :: negative : positive
  // round_flag - false : true :: down : up
  bool parity_flag, sign_flag, round_flag = false, roundeven_flag = false;
  
  result = x / precision;
  fractional = modf(result, &integral);
 
  integral = fabs(integral);
  parity_flag = fmod(integral, 2) == 1 ? false : true;
  sign_flag = x < 0 ? false : true;
 
  fractional = fabs(fractional);
  fractional *= 10;
  fractional -= 5;
  
  if (fractional >= 1)
    round_flag = true;
  else if (fractional < 0)
    round_flag = false;
  else
    roundeven_flag = true;
    
  if (roundeven_flag)
    if (!parity_flag)
      round_flag = true;
  
  if (!sign_flag)
    round_flag ^= true;
    
  if (round_flag)
    result = ceil(result);
  else
    result = floor(result);
    
  result *= precision;
  
  return result;
}
Post Reply

Return to “C/C++ Programming”