ISO/ IEC JTC1/SC22/WG14 N617

From peren!uunet!ARL.MIL!gwyn Tue Sep 23 17:10 PDT 1997
Return-Path: <peren!uunet!ARL.MIL!gwyn>
Received: by cobra. (5.x/SMI-SVR4)
	id AA08300; Tue, 23 Sep 1997 17:10:05 -0700
	id QAA22471; Tue, 23 Sep 1997 16:43:42 -0700
	(peer crosschecked as: [128.63.58.14])
	id QQdihk06252; Tue, 23 Sep 1997 18:08:49 -0400 (EDT)
>Received: by  (SMI-8.6/SMI-SVR4)
	id QAA22471; Tue, 23 Sep 1997 16:43:42 -0700
>Received: from smokey.arl.mil by relay5.UU.NET with SMTP 
	(peer crosschecked as: [128.63.58.14])
	id QQdihk06252; Tue, 23 Sep 1997 18:08:49 -0400 (EDT)
Date:     Tue, 23 Sep 97 18:04:52 EDT
From: "Douglas A. Gwyn (IST)" <peren!uunet!ARL.MIL!gwyn>
To: john c benito <uunet!peren!cobra!jb>
Subject:  Re:  Rationale for Signed integer division
Message-Id:  <[email protected]>
Content-Type: text
Content-Length: 6482
Status: RO

                   Document Number:  WG14 N617/X3J11 96-081


                        C9X Revision Proposal
                        =====================

Title: Specify truncation for signed integer division
Author: Douglas A. Gwyn
Author Affiliation: United States Army Research Laboratory
Postal Address: 801-L Cashew Court, Bel Air, MD 21014, USA
E-mail Address: [email protected]
Telephone Number: +1-410-278-8945
Fax Number: +1-410-278-2934
Sponsor: X3J11
Date: 1996-09-13
Document History:  Proposal from Tom MacDonald was approved,
        but consensus was not reached on the proper wording.
        The present author submitted Document Number WG14
        N613/X3J11 96-077 as an attempt to reach consensus.
        The present paper is intended to replace that
        document, to incorporate a suggestion made by the
        review subgroup: the 6.3.5 wording was streamlined.
Proposal Category:
   __ Editorial change/non-normative contribution
   __ Correction
   __ New feature
   __ Addition to obsolescent feature list
   __ Addition to Future Directions
   x_ Other (please specify)  tighten up semantics
Area of Standard Affected:
   __ Environment
   x_ Language
   x_ Preprocessor
   x_ Library
      __ Macro/typedef/tag name
      x_ Function
      __ Header
   x_ Other (please specify)  run-time semantics
Prior Art: Fortran
Target Audience: all C programmers, porters of code from
        Fortran to C
Related Documents (if any): ANSI X3.9-1978, ISO 1539-1980
        (programming language FORTRAN), ANSI X3.198-1992 &
        ISO/IEC 1539:1992 (Fortran 90)
Proposal Attached: x_ Yes __ No, but what's your interest?
Abstract: Currently, the C standard (C89) permits some
        flexibility in the implementation of signed integer
        division; when the division is inexact and one of
        the operands is negative, the result may be rounded
        up or rounded down, in an implementation-defined
        manner.  (The behavior of the % operator is required
        to be consistent with that of the / operator.)  This
        differs from Fortran, which requires that the result
        must definitely be truncated toward zero.  Fortran-
        like behavior is permitted by C89, but it is not
        required.

        The origin of this practice seems to have been a
        desire to map C's division directly to the "natural"
        behavior of the target instruction set, whatever it
        may be, without requiring extra code overhead that
        might be necessary to check for special cases and
        enforce a particular behavior.  However, the
        argument that Fortran programmers are unpleasantly
        surprised by this aspect of C and that there would
        be negligible impact on code efficiency was accepted
        by WG14, who agreed to require Fortran-like behavior
        in C9x.  Previous attempts to devise wording for this
        change did not attain consensus; the present proposal
        takes a somewhat more radical approach, which avoids
        previous ambiguities.

Proposal: The intent is to require signed integer division
        where at least one operand is negative to "truncate
        toward zero".

        Change in 6.3.5 Multiplicative operators, Semantics,
        from:
                When integers are divided and the division is
                inexact, if both operands are positive the
                result of the / operator is the largest integer
                less than the algebraic quotient and the result
                of the % operator is positive.  If either
                operand is negative, whether the 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.
        to:
                When integers are divided, the result of the /
                operator is the algebraic quotient with any
                fractional part discarded.  [FOOTNOTE: This is
                often called ``truncation toward zero''.]
        (Note that the relation between % and / is preserved.)

        Change in 7.10.6.2 The div function, Description from:
                If the division is inexact, the resulting
                quotient is the integer of lesser magnitude
                that is the nearest to the algebraic quotient.
        to:
                The returned quotient is the algebraic quotient
                with any fractional part discarded.

        In the Rationale document, replace the first paragraph
        of 6.3.5 Multiplicative operators with:
                In C89, division of integers involving
                negative operands could round upward or
                downward, in an implementation-defined manner;
                the intent was to avoid incurring overhead in
                run-time code to check for special cases and
                enforce specific behavior.  However, in Fortran
                the result would always truncate toward zero,
                and the overhead seems to be acceptable to the
                numeric programming community.  Therefore, C9x
                now requires similar behavior, which should
                facilitate porting of code from Fortran to C.
                The table in subsection 7.10.6.2 of this
                document illustrates the required semantics.

        In the Rationale document, change the first sentence
        of 7.10.6.2 The div function from:
                div and ldiv provide a well-specified semantics
                for signed integral division and remainder
                operations.
        to:
                Because C89 had implementation-defined
                semantics for division of signed integers when
                negative operands were involved, div and ldiv
                were invented to provide well-specified
                semantics for signed integral division and
                remainder operations.

        In the Rationale document, add a new paragraph at the
        end of 7.10.6.2 The div function:
                Now that C9x requires similar semantics for
                the division operator, the main reason for new
                programs to use div or ldiv is to
                simultaneously obtain quotient and remainder.