Bernard - 1 year ago 59
C++ Question

# Division with negative dividend, but rounded towards negative infinity?

Consider the following code (in C++11):

``````int a = -11, b = 3;
int c = a / b;
// now c == -3
``````

C++11 specification says that division with a negative dividend is rounded toward zero.

It is quite useful for there to be a operator or function to do division with rounding toward negative infinity (e.g. for consistency with positive dividends when iterating a range), so is there a function or operator in the standard library that does what I want? Or perhaps a compiler-defined function/intrinsic that does it in modern compilers?

I could write my own, such as the following (works only for positive divisors):

``````int div_neg(int dividend, int divisor){
if(dividend >= 0) return dividend / divisor;
else return (dividend - divisor + 1) / divisor;
}
``````

But it would not be as descriptive of my intent, and possibly not be as optimized a standard library function or compiler intrinsic (if it exists).

I'm not aware of any intrinsics for it. I would simply apply a correction to standard division retrospectively.

``````int div_floor(int a, int b)
{
int res = a / b;
int rem = a % b;
// Correct division result downwards if up-rounding happened,
// (for non-zero remainder of sign different than the divisor).
int corr = (rem != 0 && ((rem < 0) != (b < 0)));
return res - corr;
}
``````

Note it also works for pre-C99 and pre-C++11, i.e. without standarization of rounding division towards zero.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download