2
unsigned long long a,b,c;
cin>>a>>b>>c;
cout<<(a*b*c);

Input given: 512 294967268 279632277 Output: 5337484673731225600

However, when I do 512 * 294967268 * 279632277 in Python, the output I get is:

42230972821150328832L

Why do I get a different answer?

xennygrimmato
  • 2,646
  • 7
  • 25
  • 47
  • 1
    Overflow obviously. Use `long double` instead. Check out the `sizeof (long long)` and `long double` and you'll see the difference. – decached Nov 18 '13 at 15:28
  • for verification that it is overflow you can try `42230972821150328832%(2**64)` in python and it returns `5337484673731225600L` – Aaron Hayman Apr 16 '19 at 10:31

1 Answers1

6

Overflow in C++ for sure.

Python has datatype of arbitrary size to store the result. So if the value to be stored is big, the size of the datatype is also big automatically.

>> x = 512 * 294967268 * 279632277
>> x
42230972821150328832L
>> type(x)
<type 'long'>
>> x.__sizeof__()
36
>> x = x * x
>> x
1783455065420737763677831948901730484224L
>> type(x)
<type 'long'>
>> x.__sizeof__()
44

So practically there is no limit on the size of the datatype. The available memory defines the theoretical limit.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • What usually happens is that integers are promoted to an arbitrary precision integer type when required. More on that [here](http://stackoverflow.com/questions/4581842/python-integer-ranges). – juanchopanza Nov 03 '13 at 07:48