Subject: Re: Castings...
To: Neil A. Carson <neil@causality.com>
From: Alan Barrett <apb@iafrica.com>
List: tech-kern
Date: 10/13/1997 00:08:52
> Are there any defined standards for this stuff?

If, as I think is the case, we are talking about the integer / and %
operators in C, then the C standard says that, if either operand is
negative the result is implementation-defined.  The implementation must
choose one of two behaviours, and must document its choice.  The choice
is essentially between whether

    the result of (-12 / 5) is -3
    and the result of (-12 % 5) is 3
    and the result of ((-12/5)*5 + (-12%5)) is -12

or
    
    the result of (-12 / 5) is -2
    and the result of (-12 % 5) is -2
    and the result of ((-12/5)*5 + (-12%5)) is -12

The relevant part of the C standard (ANSI/ISO 9899-1990) is the last
paragraph of section 6.3.5:

" When integers are divided and the division is inexact, if both
" operands are positive the result if the / operator is the largest
" integer less than the algebraic quotient and the result of the %
" operator is positive.  If either operand is negative, whethe rthe
" result of the / operator is the largest integer less than or equal to
" the algebraic quotient or the smallest integer greater than or equal
" to 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.

--apb (Alan Barrett)