INT_MIN/-1

G

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).
 
E

Eric Sosman

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

Ben Bacarisse

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>
 
K

Keith Thompson

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: <[email protected]>, 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.
 
G

Guest

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

Keith Thompson

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

Peter Nilsson

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

Guest

| (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top