-2

I am facing an overflow problem for this case and getting output as 7392445620511834112 , I wanted to multiply 2 large values of b and c and want to store that value.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll b=1000000000000000000;
    ll c=2000000000000000000;
    ll out=b*c;
    cout<<out;
}
  • [The GNU MP Bignum Library](https://gmplib.org/)? – MikeCAT May 20 '21 at 11:50
  • 1000000000000000000 times 2000000000000000000 is larger than 10^18. – MikeCAT May 20 '21 at 11:51
  • Actually 2000000000000000000 itself is larger than 10^18. What does "it is provided" mean? – MikeCAT May 20 '21 at 11:51
  • use but can i store that much larger values? – Rahul Rangi May 20 '21 at 11:52
  • I don't see a question in your post. What are you asking? – eerorika May 20 '21 at 11:52
  • 4
    See also: [c++ - Why should I not #include ? - Stack Overflow](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [c++ - Why is "using namespace std;" considered bad practice? - Stack Overflow](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – MikeCAT May 20 '21 at 11:53
  • Also, don't abuse macros like that. It's entirely unnecessary. You can use a type alias, if you really want to write `ll`. – eerorika May 20 '21 at 11:54
  • 2
    And don't use type-aliases (or worse, macros) for common standard types. Such macros doesn't make your code easier to read or faster. In fact, most of the shown code shows signs of bad habits commonly taught on so-called "competition" sites. Don't use such sites to learn anything, that's not what they're for. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes. That's how you learn C++ and programming in general. – Some programmer dude May 20 '21 at 11:56
  • It is not possible to store values in c++ that are larger than 10^18. Look up the [range of long long int in c++](https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm) – risingStark May 20 '21 at 12:00
  • You'll need to come up with a strategy to handle numbers bigger than the biggest number that will fit in a `long long`. – Eljay May 20 '21 at 12:04
  • If you can't use a "bignum" library (which I assume competition sites doesn't allow), then you need to think back to basic math in school, and how you used to solve arbitrary multiplications using pen and paper. The same technique can be used in your program. – Some programmer dude May 20 '21 at 12:06
  • Anybody riding on 10¹⁸ all the time: That is *not* the maximum for long long (assuming 64 bits), maximum is *roughly* nine times greater. Both values are still in range, only result is outside (not considering missing `LL` suffix). – Aconcagua May 20 '21 at 12:18
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Yeheshuah May 21 '21 at 02:54

1 Answers1

0

If you change from long long to unsigned long long you could get a little closer. If you don't care about absolute precision you could use a floating point type instead here even just float is enough to store the output in a way that cout displays it correctly but with bigger or less round numbers or when processing the output in other ways you may want to use double or long double for more size and precision. And don't listen to all the people telling you you are doing it wrong they spend all their day arguing about those kind of things on stack exchange. How you are doing it is perfectly fine otherwise it would not compile.