Thread: Is this how to round?

  1. #1
    george7378
    Guest

    Is this how to round?

    Hi everyone,

    I was just wondering - is this a valid way to round to the nearest integer in C++?

    Code:
    double round(double x)
    {
    if(x<0){return ceil(x-0.5);}
    else if(x>0){return floor(x+0.5);}
    else {return(x=0);}
    }
    Thanks a lot.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Test your function and see if the answer is right.

  3. #3
    george7378
    Guest
    I tried it and it worked for the numbers I put in, but I didn't know if there was anything I had missed. I guess it's ok!

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Remember to test the infinities and NaN. Cause right now, NaN will give the wrong answer.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's valid, but overkill.
    floor rounds towards minus infinity and ceil rounds towards positive infinity. So they both have the same behaviour either side of zero. This means that you don't need to use both. There's also no reason to have a special case for zero because the floor of zero plus 0.5 is still zero. So all you need is:
    Code:
    double round(double x)
    {
        return floor(x+0.5);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    george7378
    Guest
    Thanks for the help - I found iMalc's suggestion in other places, but people seemed to be saying that it didn't work for negatives. On second thought, it does, doesn't it?

    Thanks!

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by george7378 View Post
    Thanks for the help - I found iMalc's suggestion in other places, but people seemed to be saying that it didn't work for negatives. On second thought, it does, doesn't it?

    Thanks!
    It depends whether you want the result to be a double or an integer. It is common to round by casting to int like this:

    Code:
    double x = ...;
    int rounded = (int)(x + 0.5);
    The above does NOT do the right thing if x is negative, because integer truncation goes toward zero, it's not like floor()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Community Game) Round Robin for C++ [Round 01]
    By phantomotap in forum General Discussions
    Replies: 5
    Last Post: 04-18-2011, 01:07 PM
  2. (Community Game) Planning for Round Robin for C++ [Round 02]
    By phantomotap in forum General Discussions
    Replies: 2
    Last Post: 04-17-2011, 02:39 PM
  3. round .5
    By msshapira in forum C++ Programming
    Replies: 19
    Last Post: 06-23-2009, 06:42 AM
  4. round
    By yes in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2006, 02:11 AM
  5. round and abs in c++
    By asimos in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2002, 02:15 PM