Thread: Rounding function

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    Rounding function

    An assignment I'm currently doing required me to round of a float. I didn't know if there was a standard function for it or not, (if there is I couldn't find it) so I wrote my own.

    Code:
    int round(float x){
      int y = (int)x;
      if((x - y) > 0.5f){
        x = (float)++y;
      }
      else{
        x = (float)y;
      }
      return (int)x;
    }
    It works fine but I'm just seeing what people have to say about it. Maybe some tips on improvment.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why are you casting y to a float before assigning it to x? and why are you returning x?

    why not just increment or decrement y and return it?

    and the answer to your standard function question: kinda. ther's ceil(x) and floor(x). you can probably tell what they do from their names...

    edit:
    Code:
    int round(float x)
    {
            int y = (int)x;
            if((x - y) > 0.5f)
            {
                    ++y;
            }
            return y;
    }
    Last edited by major_small; 07-12-2005 at 07:07 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Test with some negative values too.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by major_small
    why are you casting y to a float before assigning it to x? and why are you returning x?

    why not just increment or decrement y and return it?
    heh, I don't know. I whipped it up pretty quickly. Thanks for the input.

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    it's work fine

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't. Read Dave's post.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Well I could test for a negative value and handle it accordingly but my program wont ever use the function with a negative value. If I intended to reuse this function then I would but in this situation it is useless to my program.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Code:
    #include <math.h>
    Code:
    int round(float x) {
        return (int) floor(x + 0.5);
    }
    This should round -1.5 to -1, which is okay depending on what your definition of "up" is. (-1.51 rounds to -2)

    Code:
    int round(float x) {
        return (int) (x - 0.5 + (x > 0.0));
    }
    This should round -1.5 to -2, and 1.5 to 2, which is good if you have this definition of "up".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM