Thread: Rounding a float to the nearest half

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Question Rounding a float to the nearest half

    Hi again. I struck a bit of a problem with my program. I needed to take a number and then round it to the neerest half. After quite a while I finaly managed to hack it together but I was wondering if there is a cleaner way to do it?

    Here is my bodged way of doing it:

    Code:
        
        float weight;
        float remainder;
        int whole;   
    
        weight = 11.24;
        
        whole = weight / 1;
        remainder = weight - whole;
        if (remainder < 0.5) 
            if (remainder >= 0.25)
                remainder = 0.5;
            else
                remainder = 0.0;
        else
            if (remainder >= 0.75)
                remainder = 1.0;
            else
                remainder = 0.5;
        
        weight = whole + remainder;
    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by kzar
    Hi again. I struck a bit of a problem with my program. I needed to take a number and then round it to the neerest half.
    You could consider this:

    1. Multiply by 2.0
    2. Round to nearest integer value.
    3. Divide by 2.0

    Regards,

    Dave

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by Dave Evans
    You could consider this:

    1. Multiply by 2.0
    2. Round to nearest integer value.
    3. Divide by 2.0

    Regards,

    Dave
    Thanks that works great.

    Code:
        float weight;
    
        weight = 11.75;
        weight = round(weight * 2.0) / 2.0;
    Last edited by kzar; 04-01-2005 at 11:00 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    don't forget that when converting from float ot int, the computer just chops off the decimal places - in effect rounding towards zero. To counter this, add 0.5 if its positive and subtract 0.5 if its negative before going from float to int.

    To check this try putting in 11.8 and it will go to 11.5 if haven't done it.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by FlyingDutchMan
    don't forget that when converting from float ot int, the computer just chops off the decimal places - in effect rounding towards zero. To counter this, add 0.5 if its positive and subtract 0.5 if its negative before going from float to int.

    To check this try putting in 11.8 and it will go to 11.5 if haven't done it.

    He said that this works:
    Quote Originally Posted by kzar
    Code:
       weight = round(weight * 2.0) / 2.0;
    Since he said it worked, I would assume he is using the C99 Standard library function round() or an equivalent home-grown round() function that does its job. If you have GNU gcc you can try it yourself. (Put in 11.8 and it goes to 12.0)

    The library function round() doesn't covert float to int. It returns a double whose value is the rounded integer value of its argument.

    Regards,

    Dave
    Last edited by Dave Evans; 04-01-2005 at 10:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM