Reputation: 3287
I want to do the following things:
I use a variable int a
to keep the input from the console, and then I do the following:
int b = a / 16;
When a
is 64 or 32, I get 4 or 2. But if a
is 63, I expect to get 4, but I get 3. Are there any ways in C to get a rounded value?
Edit. More details:
rang 1 to 16 should get 1,
rang 17 to 32 should get 2,
rang 33 to 48 should get 3,
rang 49 to 64 should get 4
Upvotes: 3
Views: 140
Reputation: 10698
When you use the division operator /
with two int
arguments, it will returns an int
representing the truncated result.
You can get a rounded-up division without using floating point numbers, like this :
int a;
int den = 16;
int b = (a + den - 1) / den;
Which will give you what you expect :
a ∈ [0], b = 0 / 16 = 0,
a ∈ [1, 16], b = [16, 31] / 16 = 1,
a ∈ [17, 32], b = [32, 47] / 16 = 2,
a ∈ [33, 48], b = [48, 63] / 16 = 3,
a ∈ [49, 64], b = [64, 79] / 16 = 4,
...
Note that this only work if a
and b
are positives, and beware of the possible overflow of a + den
.
If a + den
is suspected to possibly overflow, then you could use another version of this expression :
int b = (a - 1) / den + 1;
The only downside is that it will return 1
when a = 0
. If that's an issue, you can add something like :
int b = (a - 1) / den + 1 - !a;
Note that you can also handle negative values of a
the same way (round away from zero) :
int b = (a > 0) ? (a - 1) / den + 1 : (a - den + 1) / den;
Upvotes: 4
Reputation: 2721
Since C int division always rounds towards zero, a simple trick you can use to make it round evenly is to add "0.5" to the number before the rounding occurs. For example, if you want to divide by 32, use
x = (a+16)/32;
or more generally, for a/b, where a and b are both positive:
x = (a+b/2)/b;
Unfortunately negative numbers complicate it a bit, because if the result is negative, then you need to subtract 0.5 rather than add it. That's no problem if you already know it will be positive or negative when you're writing the code, but if you to deal with both positive and negative answers it becomes messy. You could do it like this...
x = (a+((a/b>0)?1:-1)*(b/2)) / b;
but by that stage it's starting to get pretty complex - it's probably better to just cast to float and use the round
function.
--
[Edit] as zakinster pointed out, although the question asked for 'round' values, the examples given actually require ceil
. The way to do that with ints would be:
x = (a+b-1)/b;
again, if the answer can be negative this gets complicated:
x = (a+((a/b>0)?1:-1)*(b-1)) / b;
I'll leave my original response for even rounding just in case someone is interested.
Upvotes: 0
Reputation: 2225
Solution
int ans, newNo;
int no = 63; // Given number
int dNo = 16; // Divider
newNo = (no % dNo) ? (dNo - (no % dNo) + no) : no; // Round off number
ans = newNo / dNo;
Optimize solution
ans = (no / dNo) + !!(no % dNo);
Upvotes: 1
Reputation: 214780
The simplest way to get what you want is to use float numbers.
#include <math.h>
#include <stdio.h>
int main(void)
{
double a = 63.0;
double b = 16.0;
printf("%d", (int) round(a / b));
return 0;
}
Carefully pay attention when casting from float numbers to int, and vise versa. For example, round(63/16)
is incorrect. It returns a double, but achieves nothing, since the literals 63 and 16 are integers. The division is done before the value is passed to the function. The correct way is to make sure that one or both operands are of type double: round(63.0 / 16.0)
Upvotes: 0
Reputation: 6606
whatever you are trying is conceptualy not correct but still if you want the same result as you said in your question , you can try for standard function double ceil(double x)
defined in math.h
Rounds x upward, returning the smallest integral value that is not less than x.
Upvotes: 0
Reputation: 122493
Integer division /
in C does not do rouding, instead, it does truncation.
Upvotes: 2