lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On May 19, 2014, at 7:37 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

>> I'm not *that* old, but I think the first reaction of some of us was
>> "integer division in C is defined?"
>> 
>> This is clearly the result of intentionally forgetting C99's choice of how
>> to deal with negative numbers.
> 
> I think integer division was well defined in C89. Modulus ('%') is the
> one that was undefined until C99.

Until twenty minutes ago that's what I thought too. But Section 3.3.5 of the 1988 draft ANSI C standard:

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

And 3.3.5 of the 1988 draft ANSI C rationale:

"There was considerable sentiment for giving more portable semantics to division (and hence remainder) by specifying some way of giving less machine dependent results for negative operands. Few Committee members wanted to require this by default, lest existing fast code be gravely slowed. One suggestion was to make 'signed int' a type distinct from plain 'int', [...]"

My head hurts.

Jay