Getting the closest multiple of 16 above 18

Started by
15 comments, last by Fred304 17 years, 4 months ago
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
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
Advertisement
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.
"Leave it to the computer programmers to shorten the "Year 2000 Millennium Bug" to "Y2K." Isn't that what caused this problem in the first place?"
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;
}
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
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]
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
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);
}
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:
That fails for multiples of 16.
No it doesn't.

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