tags:

views:

168

answers:

3
size = (size_in_bytes + sizeof(int) - 1) / sizeof(int);

This is more of a math questions than real programming question... Is it because C always round down?

+1  A: 

If size_in_bytes is an integer multiple of sizeof(int), like 2 * sizeof(int), it gives you that multiple, because (sizeof(int)-1)/sizeof(int) is less than one. If size_in_bytes is anything other than an integer multiple of sizeof(int), the remainder when divided by sizeof(int) must be at least one. So (sizeof(int) + that remainder - one) is always >= sizeof(int). So it always rounds up.

Kinopiko
Oh no I answered a metashockwave question without realizing it.
Kinopiko
@Kino - I understand your frustrations, but that might be a bit mean. If you're that bothered by it, perhaps a post on Meta is in order.
Chris Lutz
Seriously, I posted this answer without noticing who had asked the question. Otherwise I would not have answered it.
Kinopiko
If it's that big of an issue, you can delete your own answers. I do recommend a Meta question on this issue. There's certainly a good case for it.
Chris Lutz
See: http://meta.stackoverflow.com/questions/27261/what-is-a-good-question-answer-ratio/27280#27280
Jonathan Leffler
Incidentally, I'm not going to post on meta about metashockwave since all his/her questions are perfectly legitimate. If people want to help him/her do his homework, fair enough. But I don't.
Kinopiko
@Chris: I'm not deleting the answer solely because there is a chain of comments here now.
Kinopiko
@Kino - I disagree on the legitimacy of some of his/her questions. Many of them are duplicates of each other (or other questions on the site), a lot of them are poorly worded versions of "I don't know C - tell me what this code is doing wrong!" SO is a place where programmers can get programming help, not a place to get other people to do work you don't know how to do.
Chris Lutz
@Chris: in which case you could bring the issue up on meta.stackoverflow.com. I brought up Rachel's questions there, but metashockwave is not such a bad case in my opinion. For example this question seems reasonable to me. I just don't like that he/she seems to be asking so many questions which must be homework, so I don't want to help him/her.
Kinopiko
+5  A: 

Integer division truncates in C, yes. (i.e. it's round towards zero, not round down.)

By adding "divisor - 1" you make sure that any number which isn't an exact multiple of the divisor gets rounded up. So for example, dividing by 3:

 (0 + 2) / 3 == 0 (0 is an exact multiple)
 (1 + 2) / 3 == 1 (round up)
 (2 + 2) / 3 == 1 (round up)
 (3 + 2) / 3 == 2 (3 is an exact multple)

etc

Jon Skeet
round toward 0 meaning .5 or greater => round up0 to .49999999 round down?
Round towards 0 meaning that [0, 1) goes to 0. For example, 19/5 == 3. It doesn't use midpoint rounding at all.
Jon Skeet
+1  A: 

When all the arguments are int, the / operator does integer division. This means it will divide the top over the bottom, like one would expect of math, but it will just throw away the remainder. Or, in other words, it will always round towards zero.

With integer division, ( sizeof(int) - 1 ) / sizeof(int) will always be less than one. It is, essentially, the largest not-quite-one value you can have. So adding it to the equation will always add not-quite-one to the final answer, which, when rounded down, will be the same as rounding the original equation up.

Reading this answer back, I realize it makes a lot more sense in my head than it does in words.

goldPseudo
A pedantic nitpick: In C89/90 integer division rounded to either 0 or negative infinity (implementation-definen behavior). C++98 is the same (with the added recommendation to round towards 0). And only C99 mandated rounding towards 0 in all cases.
AndreyT