INT_MIN/-1

Discussion in 'C Programming' started by phil-news-nospam@ipal.net, Oct 26, 2009.

  1. Guest

    Is the behavior of INT_MIN/-1 defined?

    6.5.5.6 specifies a result consistency for a/b if a/b is representable.
    But if it is not representable (INT_MIN/-1 is such a case for int) the
    standard is silent. There are other places where the standard says the
    result is undefined. But for this it doesn't say so, or define it. Am
    I to take that as undefined, anyway?

    The behavior I see on x86, when the compiler is not optimizing away the
    calculation from run time, is a floating point exception (which probably
    really means an arithmetic exception to the CPU architecture).

    --
    -----------------------------------------------------------------------------
    | Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
    | (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
    -----------------------------------------------------------------------------
    , Oct 26, 2009
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > Is the behavior of INT_MIN/-1 defined?


    On systems where INT_MIN + INT_MAX == 0 (that is, on
    all ones' complement and signed magnitude systems, and possibly
    on some two's complement systems), INT_MIN/-1 == INT_MAX. On
    two's complement machines where INT_MIN + INT_MAX == -1, the
    behavior is undefined. So I guess you could say that it's
    implementation-defined whether the behavior is defined or
    undefined.

    > 6.5.5.6 specifies a result consistency for a/b if a/b is representable.
    > But if it is not representable (INT_MIN/-1 is such a case for int) the
    > standard is silent. There are other places where the standard says the
    > result is undefined. But for this it doesn't say so, or define it. Am
    > I to take that as undefined, anyway?


    Yes: 6.5p5 says so explicitly.

    For the more general question see 4p2. Behavior is undefined
    when it violates a non-constraint "shall," when the Standard says
    so explicitly, or when the Standard provides no definition -- and
    all three of these are "equally undefined."

    > The behavior I see on x86, when the compiler is not optimizing away the
    > calculation from run time, is a floating point exception (which probably
    > really means an arithmetic exception to the CPU architecture).


    That's not so bad; it might easily have been worse. Do
    you know what a Gawdawful mess it makes when you sneeze a
    noseful of demons all over your keyboard? I cleaned it as
    best I could, but I think there are a few still in there, and
    they're responsible for my spelling erors.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 26, 2009
    #2
    1. Advertising

  3. writes:

    > Is the behavior of INT_MIN/-1 defined?
    >
    > 6.5.5.6 specifies a result consistency for a/b if a/b is representable.
    > But if it is not representable (INT_MIN/-1 is such a case for int) the
    > standard is silent. There are other places where the standard says the
    > result is undefined. But for this it doesn't say so, or define it. Am
    > I to take that as undefined, anyway?


    Yes, it is undefined. In fact, 6.5 paragraph 5 says so explicitly:

    5 If an exceptional condition occurs during the evaluation of an
    expression (that is, if the result is not mathematically defined
    or not in the range of representable values for its type), the
    behavior is undefined.

    <snip>
    --
    Ben.
    Ben Bacarisse, Oct 26, 2009
    #3
  4. writes:
    > Is the behavior of INT_MIN/-1 defined?

    [snip]

    As others have already mentioned, the behavior of INT_MIN/-1 is
    undefined if the result cannot be represented in an int (as is the
    case for most modern systems).

    Also, as Larry Jones posted here last month (Fri 2009-09-04,
    Message-ID: <>, Subject: Re: in
    standard C it is impossible to write a correct program. Why?):

    At the last meeting, the committee voted to add words to 6.5.5
    to clarify that the behavior of a%b is undefined if a/b is not
    representable.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 26, 2009
    #4
  5. Guest

    On Mon, 26 Oct 2009 08:30:50 -0700 Keith Thompson <> wrote:

    | At the last meeting, the committee voted to add words to 6.5.5
    | to clarify that the behavior of a%b is undefined if a/b is not
    | representable.

    There's no more value to a%b when b==1 than in a/b. Of course, a%-1 should
    be 0 always. But in the case of a/-1 not be representable, who knows what
    result a particular divider design my give for a/-1. At least a/1 is a bit
    more clean cut since if a is representable, a/1 is, too.

    --
    -----------------------------------------------------------------------------
    | Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
    | (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
    -----------------------------------------------------------------------------
    , Oct 26, 2009
    #5
  6. writes:
    > On Mon, 26 Oct 2009 08:30:50 -0700 Keith Thompson <> wrote:
    >
    > | At the last meeting, the committee voted to add words to 6.5.5
    > | to clarify that the behavior of a%b is undefined if a/b is not
    > | representable.
    >
    > There's no more value to a%b when b==1 than in a/b. Of course, a%-1 should
    > be 0 always. But in the case of a/-1 not be representable, who knows what
    > result a particular divider design my give for a/-1. At least a/1 is a bit
    > more clean cut since if a is representable, a/1 is, too.


    The case where b==1 is not the issue; both a/b and a%b are well
    defined in that case.

    The case in question is, for example, a==INT MIN, b==-1. The point
    of the recent committe vote is that, even though INT_MIN % -1 has a
    mathematically well defined result (namely 0), computing that result
    on typical hardware is likely to involve computing INT_MIN / -1 as
    well, which invokes UB. (This assumes 2's-complement; for symmetric
    signed integer representations, the issue doesn't come up.)

    With a special case for INT_MIN % -1, implementations would be
    required to compute it correctly, which means that they'd have
    to perform a special run-time check whenever the values of the
    operands of "%" aren't known at compile time.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 26, 2009
    #6
  7. wrote:
    > 6.5.5.6 specifies a result consistency for a/b if a/b
    > is representable.


    The consistency being...

    "If the quotient a/b is representable, the expression
    (a/b)*b + a%b shall equal a."

    My C89 draft has the same clause, yet C89 is not as rigid
    as C90 regarding division. Note that INT_MIN/2 is always
    representable, however 'the expression' may overflow if
    division rounds towards negative infinity.

    --
    Peter
    Peter Nilsson, Oct 26, 2009
    #7
  8. Guest

    On Mon, 26 Oct 2009 14:16:10 -0700 (PDT) Peter Nilsson <> wrote:
    | wrote:
    |> 6.5.5.6 specifies a result consistency for a/b if a/b
    |> is representable.
    |
    | The consistency being...
    |
    | "If the quotient a/b is representable, the expression
    | (a/b)*b + a%b shall equal a."
    |
    | My C89 draft has the same clause, yet C89 is not as rigid
    | as C90 regarding division. Note that INT_MIN/2 is always
    | representable, however 'the expression' may overflow if
    | division rounds towards negative infinity.

    But does a mathematical consistency requirement have to actually be doable
    in the language for the specification to be understood?

    I am in fact shaking out division/modulus toward negative infinity right
    now, in the form of macros (that do use GNU extensions):

    http://phil.ipal.org/c/mod_ni.h
    http://phil.ipal.org/c/quo_ni.h

    I've actually needed these macros in a few places, already. One example
    is date calculations when time (not necessarily time_t) values go negative.

    --
    -----------------------------------------------------------------------------
    | Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
    | (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
    -----------------------------------------------------------------------------
    , Oct 26, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M Welinder

    C99: Is "INT_MIN % -1" well defined?

    M Welinder, Aug 5, 2004, in forum: C Programming
    Replies:
    6
    Views:
    348
    Dik T. Winter
    Aug 7, 2004
  2. Jordan Abel

    INT_MIN as decimal

    Jordan Abel, Mar 8, 2006, in forum: C Programming
    Replies:
    15
    Views:
    656
    Peter Nilsson
    Mar 9, 2006
  3. negate INT_MIN

    , Feb 10, 2007, in forum: C Programming
    Replies:
    2
    Views:
    409
    ┬Ča\\/b
    Feb 10, 2007
  4. INT_MIN and compiler diagnostic

    , Feb 28, 2007, in forum: C Programming
    Replies:
    41
    Views:
    1,185
    Chris Torek
    Mar 26, 2007
  5. viza

    (unsigned)-INT_MIN

    viza, Jul 21, 2008, in forum: C Programming
    Replies:
    22
    Views:
    1,013
    Keith Thompson
    Jul 26, 2008
Loading...

Share This Page