Nice rounding function for C/C++

C, C++, Visual C++, C++.Net Topics

Nice rounding function for C/C++

Postby Saman » Mon Dec 08, 2014 7:41 pm

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;
}
User avatar
Saman
Support Team
Support Team
 
Posts: 816
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Return to C/C++ Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron