-2

As we all know, the smallest integer is different depending on the compiler, so I have this question: What's the result of (int)-2147483648 divided by (int)-1 you get when you use various compilers? For example, VC6.0, VS2010 etc.?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
funnyfan
  • 25
  • 1
  • 2
    out of curiosity: why would you try _dividing_? Yes, it's the same in this case, but I wouldn't have ever thought of doing negation with division instead of multiplication. – stefan Dec 28 '12 at 14:03
  • Multiplication or division doesn't matter. The result is implementation defined if the result of an arithmetic operation on signed ints isn't representable. An implementation can define the result or raise a signal. (At least in C99, cf §6.3.1.3.) You'll need to look in Microsoft's compiler docs. – Mat Dec 28 '12 at 14:05
  • 1
    @Mat it is not implementation defined. It is undefined behavior: `INT_MIN / -1` is undefined behavior because of `6.5.5p5` in c99. – ouah Dec 28 '12 at 14:13
  • By the way, I cannot understand why this question has been closed. If some people wrongly assume this division is implementation-defined I think the question has to be reopened. – ouah Dec 28 '12 at 14:16
  • @ouah: I don't see that spelled out in 6.5.5/5, only division by zero (but I did indeed think that specific case was undefined...). And agree with you, I don't see why this was closed. (There's probably a dup out there though.) – Mat Dec 28 '12 at 14:20
  • @Mat sorry 6.5p5, not 6.5.5p5. – ouah Dec 28 '12 at 14:21
  • 2
    Related question: [Why it is different between -2147483648 and (int)-2147483648](http://stackoverflow.com/questions/12620753/why-it-is-different-between-2147483648-and-int-2147483648) – Bo Persson Dec 28 '12 at 14:47
  • 2
    Why do you need to know this? The behavior is undefined, so you should never try in the first place, and compilers are not required to give consistent results. – Raymond Chen Dec 28 '12 at 14:52
  • On x86/x86_64, you commonly get a floating point exception when you do that. – Daniel Fischer Dec 28 '12 at 16:46

1 Answers1

1

First let's assume we are in a 32-bit system with two's complement representation where INT_MIN value is the same as -INT_MAX - 1.

This expression:

(int) -2147483648 / (int) -1

is equivalent to

(int) -2147483648 / -1

as -1 is already of type int.

In a 32-bit two's complement system where INT_MAX is 2147483647, the value 2147483648 is of type long1) as it cannot be represented in an int. The value of -2147483648 is also of type long.

The long value -2147483648 can be represented in an int, and the value after the integer conversion of (int) -2147483648 is INT_MIN.

The original expression is then equivalent (under our assumptions) to:

INT_MIN / -1

and this expression is equivalent to INT_MAX + 1 which is not representable in an int. Indeed int type range goes from INT_MIN to INT_MAX. The expression is an integer overflow and integer overflow invokes undefined behavior in C.

(C99, 6.5p5) "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."


1) We implicitly assume LONG_MAX is > INT_MAX, otherwise the value 2147483648 is of type long long.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    6.3.1.3(1): "When a value with integer type is converted to another integer type other than `_Bool`, if the value can be represented by the new type, it is unchanged." _Usually_, the value of `(int) -2147483648` is not implementation-defined [beyond the implementation-definedness of integer type sizes and representations] and results in `INT_MIN`. Only if the width of `int` is less than 32 bits, or the representation is not two's complement, or the value with sign-bit 1 and all value bits 0 is a trap representation can it be something else than `-2147483648`. – Daniel Fischer Dec 28 '12 at 16:56
  • @DanielFischer gosh, I missed the point in the second part of my answer. Of course `-2147483648` is representable in an `int` and it is indeed `INT_MIN`, I don't know what I was thinking about. Thank you, I'm editing this. – ouah Dec 28 '12 at 17:29