-2

I am trying to do a division of :-

#include <bits/stdc++.h>
using namespace std;

int main(){
  int A = -2147483648;
  int B = -1;
  int C = A/B;
  // this is not working
  cout<<C<<endl;
 // nor this is working
  cout<<A/B<<endl;
// But this is working
  cout<<-2147483648/-1<<endl; // printing the result 2147483648;
}

I am confused why this happening. Please explain.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Gaurav
  • 1
  • 1
  • 1
  • 1
  • 2
    Please be more specific than "not working". (And remember that overflowing a signed integer has undefined behaviour.) – molbdnilo Oct 19 '22 at 14:04
  • 4
    Dont use ; `#include using namespace std;` Have a look at : https://en.cppreference.com/w/cpp/types/numeric_limits to avoid running into corner cases. Try for -2147483477. (Negative integer numbers of 1 larger range then positive ones). It is part of signed integer arithmetic, so this is not a bug – Pepijn Kramer Oct 19 '22 at 14:04
  • 3
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) - *Never*, ever, include that header. And if you want people to take your question serious, then get rid of it at once, since just the fact that you have that header included clearly shows that you have *no idea what you are doing*. – Jesper Juhl Oct 19 '22 at 14:06
  • 2
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 19 '22 at 14:07
  • 1
    none of this is "working" for me: https://godbolt.org/z/8bdoEEYxe. You should include output and expected output in the question. If it is necessary to change the code to see the effect you refer to then also post that modified code please – 463035818_is_not_an_ai Oct 19 '22 at 14:10
  • Note that `-2147483648` is not a literal negative number, it is the negation of a literal positive number. Since `2147483648` does not fit in `int`, its type is the next wider signed integer type (probably `long long int`). – molbdnilo Oct 19 '22 at 14:16

2 Answers2

0

Assuming the int type is 32-bits and uses two's complement representation, the first two cases exhibit undefined behavior because both -2147483648 and -1 fit in a int but 2147483648 does not.

In the third case, the expression -2147483648/-1 contains the integer literal 2147483648 (before being negated), and has the first type in which the value can fit. In this case, that would be long int. The rest of the calculation keeps the type, so no undefined behavior occurs.

Nelfeal
  • 12,593
  • 1
  • 20
  • 39
  • Okay Yes i just searched the limit of int 2147483648 int can't store this number but i changed the code to :- int A = -2147483648; int B = -1; long int C = A/B; cout< – Gaurav Oct 19 '22 at 14:33
  • @Gaurav The expression `A/B` still has type `int`. Try `static_cast(A)/B`. – Nelfeal Oct 19 '22 at 14:37
0

You can change the data type to long long. long long A = -2147483648; long long B = -1; long long C = A/B; If your you need fractional result, try 'double' instead of 'long long'.

Mahfuz
  • 21
  • 4