Categories
c c89 c99 integer-division math

What is the behavior of integer division?

263

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?

6

  • 8

    Summary: signed integer division truncates towards zero. For non-negative results, this is the same as floor (round towards -Infinity). (Beware that C89 doesn’t guarantee this, see answers.)

    Nov 7, 2017 at 4:19


  • 39

    Everyone keeps saying “truncate toward zero” or “ceiling” or “floor” like the code is making a deliberate decision on which technique to use. If the code could talk it would say "I just throw the dam fraction part in the trash and move on with life"

    Oct 8, 2018 at 3:09


  • 7

    @TimothyL.J.Stewart The “code” is making a deliberate decision. As per the specification, integer division is meant to be T(runcation)-division. Because of this, the modulo/remainder operator is implented differently than if it were in another language, say, Python or Ruby. See this for a list of different ways languages do the modulo operator and this paper that lists out at least five of the common ways programming languages decide to do div/modulo.

    – 13steinj

    Oct 23, 2018 at 21:22

  • 4

    @13steinj I’m speaking colloquially per the comments it was turning into a “it’s truncate toward zero… no it’s floor… no if its negative its ceiling…” sometimes technicalities do not propagate into the future with human memory like we wish, but knowing intuitively that the “fraction part is tossed away” you can derive the technical points. Technicalities are a heavy burden, but intuition is light and refreshing as the wind, I’ll carry those far and wide and when necessary I’ll know where to start. Like that paper you linked, thank you.

    Oct 24, 2018 at 0:50

  • I answered here with the emphasis on the Euclidean division (inter-play between integer division and modulus operator).

    May 18, 2019 at 17:25

219

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:

  1. 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]

9

  • 26

    …unless, of course, you’re dividing a negative number by a positive (or v.v.), in which case it’ll be the ceiling.

    – Will A

    Aug 30, 2010 at 17:45


  • 81

    It is neither flooring nor ceiling, it is truncation of fractional part, it is conceptually different!

    – lornova

    Aug 30, 2010 at 17:48


  • 43

    @Will A: No. It is defined as truncation towards zero. Calling it anything else will just add to confusion so please refrain from doing so.

    Aug 30, 2010 at 17:49

  • 49

    At least from a mathematical perspective, truncation towards zero is equivalent to “if > 0 then floor else ceiling.” I think just calling it truncation is simpler than calling it floor/ceiling, but either is valid. Regardless, Will A’s point is valid: Dirkgently’s answer is partially incorrect, since he stated that the OP is right about the result being the floor of the division.

    – Brian

    Aug 30, 2010 at 18:17

  • 9

    @Philip Potter: I don’t think it was defined in C89, and it isn’t in the 1998 C++ standard. In those, of course (a / b) * b + a % b == a had to be satisfied, and the absolute value of a % b had to be less than a, but whether a % b was negative for negative a or b was not specified.

    Aug 30, 2010 at 21:28

50

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.

3

  • 3

    And C99 draft N1256 foreword paragraph 5 mentions reliable integer division as a new language feature. Amazing *-*.

    Sep 11, 2016 at 8:24

  • Truncation is how most common CPU hardware (e.g. x86) behaves, so it would be crazy to make a different choice. IDK which came first, Fortran semantics or hardware behaviour, but it’s not a coincidence that those are the same, too.

    Nov 7, 2017 at 4:23

  • 3

    @PeterCordes: Most common CPU hardware could do floored division by most constants faster than they could do truncating division. IMHO, it would have been better for the Standard to say that expr1 / expr2 and expr1 % expr2 must be consistent with each other when both instances of expr1 combine the same objects in the same way, and likewise for expr2, but the choice of truncating versus floored division is otherwise Unspecified. That would have allowed more efficient code generation without breaking much compatibility (and implementations could document specific behavior if inclined)

    – supercat

    Jun 30, 2018 at 20:05

34

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).

1

  • 9

    @dan04: yep floor would be valid only for positive integers 🙂

    – Leonid

    Aug 30, 2010 at 17:46