Reputation: 707
I am trying to represent some large numbers in c++. In the below code if tried only to print s
the compiler would not complain. But If try to make some multiplications and storing it in t
the compiler would say integer overflow in expression...
I tried to make it unsigned long long t
but again the compiler complains. Is there any way in doing this multiplication without having any overflow?
int main ()
{
long long int s = 320718425168;
long long int t = 4684688*68461; //4684688*68461 = 320718425168
return 0;
}
Upvotes: 2
Views: 746
Reputation: 60969
The literals used as the factors of the products are of type int
, which cannot represent the product.
Cast one of the factors to long long
first.
long long t = (long long)4684688 * 68461;
Or use the corresponding literal suffix ll
or LL
to change the literals type. I.e.
long long t = 4684688LL * 68461;
Demo.
Upvotes: 7