rounding question

This is a discussion on rounding question within the C++ Programming forums, part of the General Programming Boards category; is it possible to round to the next highest multiple of four? for example: if i have the values: 1.6, ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    rounding question

    is it possible to round to the next highest multiple of four? for example:

    if i have the values:

    1.6, 2.3, 3.4, 3.9 - all of them need to be 4.

    4.1, 5.6, 6.3 - all of them need to be 8.

    8.1, 9.7, 10 - all of them need to be 12.

    etc.

    can the ceiling function do this? any suggestions?

    keith

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Depending on how many significant digits you need to worry about, off the top of my head I would do something similar to this (although I'm sure there is an easier way!):
    Code:
    x=int(x+3.999999)/4*4;

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    multiple of 4

    you could create a function that does it for you


    Code:
    int RoundBy4( float flValue )
    {
         int iFoursInValue;
         int iValue;
    
         iValue = flValue + 4;
    
         iFoursInValue = iValue / 4;
    
         iValue = iFoursInValue * 4;
    
         return iValue;
    }
    something like this could work .... you probably have to do a little more work to it but it should work....
    zMan

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    16
    I would do this...

    Code:
    #include <math.h>
    
    double d = 2.4;
    int i = (int)ceil(2.4);
    while((++i)%4); // i is now round to highest multiple of 4

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    wow you guys are good. i just can't think like that

    thanks a bunch.

    keith

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    ZMan - yours unfortunately doesn't work for numbers that don't need to be rounded at all. For example, 4.0 should be 4, but yours will say 8. Thats why I added 3.99999 instead of 4.

    Jubbas is probably best if you need to worry about an unlimited amount of numbers after the decimal point, but you'll have to change "while((++i)%4)" to "while((i++)%4)" otherwise something like 3.4 will come out as 8.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Instead of using 3.99999, use an if. If number % 4 == 0, return number.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    But you can't use % with floats though.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    if((int)number % 4 == 0)

    Sheesh... figure it out yourself, you've done typecasting before. Do I really have to spoonfeed you?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    LOL again doesn't work! Try that with 4.1!

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh yeah? well:
    Code:
    if((((double)(int)number) == number) && ((int)number % 4 == 0))
         return number;
    else
         return ((double)((int)(number + 4)/4*4));
    HAH!
    Last edited by Hunter2; 08-13-2003 at 08:49 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    wtf are you thinking? Ever heard of fmod?

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, but I never knew what it was...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21