Thread: Calculate nearest number?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Cool Calculate nearest number?

    How to do this?

    Example: I have a couple of numbers: 16, 32, 48, 64, 80, 96 and so on, they increase by 16 each time.

    How can I change a random number to the nearest of these numbers?

    In this example the random number is 22 and 16 is the nearest number, so my answer is 16. A couple of more examples: 33=32, 61=64, 17=16

    How to do this in code in a simple way?
    You cantīt teach an old dog new tricks.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    int i = 39;

    i = (i+8)&-16;

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int NearestNumber(int Number)
    {
       return (((int)((Number + 8) / 16)) * 16);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Do you want the random number to be one of 16,32,48 etc... ? Then it's easier to generate it this way:
    Code:
    int Number = (rand() % RangeOfRandomNumbers) * 16;
    RangeOfRandomNumbers is how many 16-tuples to choose from, ie: 5 gives 0,16,32,48,64.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Magos
    Do you want the random number to be one of 16,32,48 etc... ? Then it's easier to generate it this way:
    Code:
    int Number = (rand() % RangeOfRandomNumbers) * 16;
    RangeOfRandomNumbers is how many 16-tuples to choose from, ie: 5 gives 0,16,32,48,64.
    No, I don't need random numbers, thanks anyway :-)
    You cantīt teach an old dog new tricks.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Monster
    int i = 39;

    i = (i+8)&-16;
    This code seems nice for gaps of 16.

    Can I use this code idea even if the gap between numbers is something else then 16? Example:

    20, 40, 60, 80

    or

    3, 6, 9, 12

    You cantīt teach an old dog new tricks.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by electrolove
    This code seems nice for gaps of 16.

    Can I use this code idea even if the gap between numbers is something else then 16? Example:

    20, 40, 60, 80

    or

    3, 6, 9, 12

    Ah, now I get what Monster was doing. He was cutting the last 4 bits in the number making it a 16 tuple. -16 is the same bitmask as 240.
    Sorry, it only works for numbers like 2,4,8,16,32,64 etc...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Magos
    Ah, now I get what Monster was doing. He was cutting the last 4 bits in the number making it a 16 tuple. -16 is the same bitmask as 240.
    Sorry, it only works for numbers like 2,4,8,16,32,64 etc...
    Yea, I realize now that my example of a gap of 16 was not good. The thing I need is a function that takes the gap as parameter 1 and the value to be calculated as parameter 2. In that way I can use any gap to calculate the random number, that is what I need.

    Anyone?
    You cantīt teach an old dog new tricks.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just modify my function above to:
    Code:
    int NearestNumber(int Number, int Gap)
    {
       return (((int)((Number + (int)(Gap / 2)) / Gap)) * Gap);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Magos
    Just modify my function above to:
    Code:
    int NearestNumber(int Number, int Gap)
    {
       return (((int)((Number + (int)(Gap / 2)) / Gap)) * Gap);
    }
    I have tested the code now and it does exactly the thing I want (as far as I know)

    Thanks Magos
    You cantīt teach an old dog new tricks.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Hey in simple lingo....u cd even try this ...

    Divide the random no. by 16.....ok get the remainder now if that is gr8r than 8 then the nearest no. is 16*(quotient+1) otherwise the no. is just (16*quotient).....give it a try and let me know if it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM