views:

140

answers:

4

i.e. -

int result;

result = 125/100;

or

result = 43/100;

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

If you could point me in the right direction, i would appreciate it.

Thank You.

+2  A: 

Yes, the result is always floor of the division for positive integers. It will round towards smallest absolute value.
-5 / 2 = -2
5 / 2 = 2

Leonid
Truncation, *not* floor.
dan04
@dan04: yep floor would be valid only for positive integers :)
Leonid
+9  A: 

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

Yes, integer quotient of the two operands.

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]

dirkgently
...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
It is neither flooring nor ceiling, it is truncation of fractional part, it is conceptually different!
Lorenzo
@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.
Martin York
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
Is this behaviour in C89 as well? I seem to remember modulus is not as rigorously defined.
Philip Potter
@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.
David Thornley
+2  A: 

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

Gareth Williams
@Gareth Williams: I agree with the comment wondering whether having (neg % pos) go negative is ever useful? On a related note, I wonder if the required arithmetically-incorrect behavior in some cases of "unsignedvar > signedvar" is ever useful? I can understand the rationale for not requiring always-correct behavior; I see no rationale for requiring wrong behavior.
supercat
+1 for an excellent reference on why flooring is the correct behavior for integer division (contrary to C's definition, which is broken and almost-never useful).
R..
+1  A: 

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.

schot