I'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
Asked
Active
Viewed 146 times
-2
-
3Give it a try, post your code, and we'll help you make it work. – Jacob Mattison Apr 09 '13 at 18:43
-
Looks like your question is answered here: http://stackoverflow.com/questions/554204/where-is-round-in-c – Frecklefoot Apr 09 '13 at 18:44
-
Does this *way* need to do something specific with `2`? – Drew Dormann Apr 09 '13 at 18:55
3 Answers
2
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1

Jake Smith
- 2,332
- 1
- 30
- 68
-
@Benjamin Lindley's answer seems to be what you are looking for as well. – Jake Smith Apr 09 '13 at 18:57
-
-
1he very well could be. That's why I wrote pseudo code. There are different ways to check for equality with different assumed data types. Good to point that out though. – Jake Smith Apr 09 '13 at 19:00
-3
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items

user2260087
- 1
- 1
-
Downvote for macro, especially in C++ code. Also for using low-level operators instead of clearly arithmetic options. – djechlin Apr 09 '13 at 18:46
-
1