Getting the closest multiple of 16 above 18
Hello,
Let's say I have an integer, and I want to know what the closest multiple of 16 abóve that number. Some examples:
integer = 18
closest No. = 32
integer = 86
closest No. = 96
integer = 4
closest No. = 16
Is there a simple formula or function for this?
I found something:
18 : 16 = 1.125. Now if we round that úp, we'd get 2. 2 * 16 = 32
86 : 16 = 5.375. 6 * 16 = 96
Still, how do you round up?
-Stenny
Just divide it normally and round it up. I'm not sure what function is best to used, but if your using integers it will be truncated anyway, so you only need to +1 to the result.
int AboveMultipleOf16 (cont int number)
{
int int_factor_of_16 = number / 16;
int greater_multiple_of_16 = int_factor_of_16 * 16;
return (greater_multiple_of_16 < number) ? (greater_multiple_of_16 + 16) : greater_multiple_of_16;
}
{
int int_factor_of_16 = number / 16;
int greater_multiple_of_16 = int_factor_of_16 * 16;
return (greater_multiple_of_16 < number) ? (greater_multiple_of_16 + 16) : greater_multiple_of_16;
}
don't forget to keep care of the special case where the input number is a multiple of 16.
Using truncating unsigned integer arithmetic: ceil_mult_n = ((value + (n - 1)) / n) * n
Σnigma
Σnigma
Quote:Original post by Tesl
Just divide it normally and round it up. I'm not sure what function is best to used, but if your using integers it will be truncated anyway, so you only need to +1 to the result.
Ah, now that's a nice idea. Using ints and adding 1. Thanks guys!
[Edited by - stenny on December 16, 2006 7:54:05 AM]
i think you could also just add the modulo, that is, the rest of the integer division.
int AboveMultipleOf16 (cont int number)
{
return number + (number % 16);
}
int AboveMultipleOf16 (cont int number)
{
return number + (number % 16);
}
Sure.
(defun closest-mult-16 (n) (* (+ 1 (truncate (/ n 16))) 16))[13]> (closest-mult-16 18)32[14]> (closest-mult-16 86)96[15]> (closest-mult-16 4)16[16]>
Quote:Original post by Anonymous P
Sure.(defun closest-mult-16 (n) (* (+ 1 (truncate (/ n 16))) 16))[13]> (closest-mult-16 18)32[14]> (closest-mult-16 86)96[15]> (closest-mult-16 4)16[16]>
That fails for multiples of 16.
Quote:Original post by EddyCharly
i think you could also just add the modulo, that is, the rest of the integer division.
int AboveMultipleOf16 (cont int number)
{
return number + (number % 16);
}
That's complete nonsense; the modulo result is the remainder, so you're just doubling up the remainder value. This *only* works for multiples of 16 and values congruent to 8 modulo 16.
Quote:Original post by Enigma
Using truncating unsigned integer arithmetic: ceil_mult_n = ((value + (n - 1)) / n) * n
This is correct code, and is a standard idiom, which IIRC is even mentioned in "How to Think Like a Computer Scientist". Enigma has won the thread; you can all go home now.
Quote:No it doesn't.
That fails for multiples of 16.
The requirement:
Quote:Let's say I have an integer, and I want to know what the closest multiple of 16 abóve that number.
The closest multiple of 16 above 16 is not 16. It's 32. :)
Unless you're using a weird definition for 'above', where the closest integer above an integer could be the integer itself...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement