-1

I am trying to understand the phenomenon of overflow and decided to demonstrate the same by executing the piece of code provided below:

#include <bits/stdc++.h>

using namespace std;

int main(){
    int a = 100000;
    int b = 100000;
    cout << a * b << endl; 
    long int c = a * 1LL * b;
    cout << c;
    return 0;
} 

The idea is that simply multiplying a and b (as done in line 6) would result in an integer overflow as 10^10 falls outside the range of the integer data type. To overcome this, the product (a*b) is multiplied with the literal '1LL' (as shown in line 7). The problem is that integer overflow still takes place as the output of the program is:

1410065408
1410065408

It's possible that I am making a super silly mistake somewhere, but I have spent a sufficient amount of time trying to understand where it's going wrong and am yet to find the reason. Hoping for some help/guidance here :)

east1000
  • 1,240
  • 1
  • 10
  • 30
  • 1
    cannot reproduce https://godbolt.org/z/zqWcc1jTq – 463035818_is_not_an_ai Sep 27 '21 at 09:04
  • 3
    Are you sure that `long int` is bigger than `int` on your machine? Does it also happen with `long long int`? – Yksisarvinen Sep 27 '21 at 09:05
  • `long int` is only guaranteed to have a width of not less than 32 bits: https://en.cppreference.com/w/cpp/language/types. This is why the fixed width types exist: https://en.cppreference.com/w/cpp/types/integer – cmannett85 Sep 27 '21 at 09:06
  • @Yksisarvinen, I assumed it would be bigger. Had no idea that the sizes can vary with different machines. Thanks for bringing that to my attention. Changing the data type of c to long long int did make the program work. – Siddharth Mitra Sep 27 '21 at 09:10
  • @cmannett85 I did not know that, guess I should have researched a little more on this. Thanks for the info! – Siddharth Mitra Sep 27 '21 at 09:13
  • Can't reproduce with the code as shown. But I wouldn't expect to be able to. Neither a `long` nor an `int` are not guaranteed to be able to represent 10000*10000. A `long long` can. Forcing at least one operand of each multiplication to be `long long` would therefore work. If you're only multiplying by `1L` (as distinct from `1LL`) then overflow is possible. – Peter Sep 27 '21 at 09:17
  • Does this answer your question? [What does the C++ standard state the size of int, long type to be?](https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – phuclv Sep 27 '21 at 09:20
  • If your on a Windows platform then a `long` is the same size as an `int` - both are 32 bits. Even when compiling x64. (Note however that they are distinct types.) – Bathsheba Sep 27 '21 at 10:32
  • ([Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) – Biffen Sep 27 '21 at 10:41
  • I’m voting to close this question because it is based on an erroneous premise. – Eric Postpischil Sep 27 '21 at 10:54
  • The title mentions a "type cast", but there are no casts in this code. The question is not about a type cast, but about a type **conversion**. A cast is something you write in your source code to tell the compiler to do a conversion. – Pete Becker Sep 27 '21 at 14:32

2 Answers2

1

Type of 1LL is long long int. Its size is at least 8 bytes. Type of c is long int. Its size is at least 4 bytes. Thus you may still have overflow. To avoid this you may use auto:

#include <iostream>
#include <cstdint>

int main() {
  std::uint32_t a = 100'000;
  std::uint32_t b = 100'000;

  auto r1 = a * b;
  auto r2 = static_cast<std::uint64_t>(a) * b;

  std::cout << "r1 = " << r1 << '\n' 
    << "r2 = " << r2 << '\n';
}

Try Online

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
-1

Seems to be environment problem - works fine for me.

cat long_int_ex.cpp; g++ -O2 long_int_ex.cpp ; a.out; g++ --version
#include <bits/stdc++.h>

using namespace std;

int main(){
    int a = 100000;
    int b = 100000;
    cout << a * b << endl; 
    long int c = a * 1LL * b;
    cout << c << endl;
    return 0;
} 
1410065408
10000000000
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.