What is the behavior of integer division?

CMathC99C89Integer Division

C Problem Overview


For example,

int result;

result = 125/100;

or

result = 43/100;

Will result always be the floor of the division? What is the defined behavior?

C Solutions


Solution 1 - C

> Will result always be the floor of the division? What is the defined behavior?

Not quite. It rounds toward 0, rather than flooring.

> 6.5.5 Multiplicative operators

> 6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

and the corresponding footnote:

> 88) This is often called ‘‘truncation toward zero’’.

Of course two points to note are:

> 3 The usual arithmetic conversions are performed on the operands.

and:

> 5 The result of the / operator is the > quotient from the division of the > first operand by the second; the > result of the % operator is the > remainder. In both operations, if the > value of the second operand is zero, > the behavior is undefined.

[Note: Emphasis mine]

Solution 2 - C

Dirkgently gives an excellent description of integer division in C99, but you should also know that in C89 integer division with a negative operand has an implementation-defined direction.

From the ANSI C draft (3.3.5): > If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

So watch out with negative numbers when you are stuck with a C89 compiler.

It's a fun fact that C99 chose truncation towards zero because that was how FORTRAN did it. See this message on comp.std.c.

Solution 3 - C

Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.

-5 / 2 = -2
 5 / 2 =  2

For unsigned and non-negative signed values, this is the same as floor (rounding towards -Infinity).

Solution 4 - C

Where the result is negative, C truncates towards 0 rather than flooring - I learnt this reading about why Python integer division always floors here: Why Python's Integer Division Floors

Solution 5 - C

>Will result always be the floor of the division?

No. The result varies, but variation happens only for negative values.

> What is the defined behavior?

To make it clear floor rounds towards negative infinity,while integer division rounds towards zero (truncates)

For positive values they are the same

int integerDivisionResultPositive= 125/100;//= 1
double flooringResultPositive= floor(125.0/100.0);//=1.0

For negative value this is different

int integerDivisionResultNegative= -125/100;//=-1
double flooringResultNegative= floor(-125.0/100.0);//=-2.0

Solution 6 - C

I know people have answered your question but in layman terms:

5 / 2 = 2 //since both 5 and 2 are integers and integers division always truncates decimals

5.0 / 2 or 5 / 2.0 or 5.0 /2.0 = 2.5 //here either 5 or 2 or both has decimal hence the quotient you will get will be in decimal.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionT.T.T.View Question on Stackoverflow
Solution 1 - CdirkgentlyView Answer on Stackoverflow
Solution 2 - CschotView Answer on Stackoverflow
Solution 3 - CLeonidView Answer on Stackoverflow
Solution 4 - CGareth WilliamsView Answer on Stackoverflow
Solution 5 - CMohamed El-NakeepView Answer on Stackoverflow
Solution 6 - Cadi1yaView Answer on Stackoverflow