Reputation: 23
I have recently started learning C++ and have made this small program
#include <iostream> // for std::cout << / std::cin >> / std::endl; / '\n'
#include <cstdlib> // for EXIT_SUCCESS and EXIT_FAILURE
int input()
{
int imp_num{ 0 };
std::cin >> imp_num;
return imp_num;
}
void output(int result)
{
std::cout << "The dubble of that number is : " << result << '\n';
}
int main()
{
std::cout << "Enter a number: ";
int inp_num{ input() }; // asks the user to enter a number and saves it
int result{ inp_num * 2 }; // calculates the result
output(result); // outputs the result
system("pause"); //prevents the console from terminating
return 0;
}
The problem occurs when the number revived is 10 digits or more. At that point the program just a random number (usually -2) that will always remain the same no matter what I put, and only changes if I recompile the source code.
Enter a number: 23213231231231212312
The dubble of that number is : -2
Press any key to continue . . .
Enter a number: 12311111111111111111111111
The dubble of that number is : -2
Press any key to continue . . .
I recompile the source code
Enter a number: 1231212123133333333333321
The dubble of that number is : 3259
Press any key to continue . . .
Changing all the int
to int64_t
doesn't solve the problem but weirdly the same output of -2 is present here.
Enter a number: 1231212123133333333333321
The dubble of that number is : -2
Press any key to continue . . .
I don't understand why out off all the numbers -2 will appear if an integer overflow is happening. I thought the numbers should circle around.
Upvotes: 2
Views: 115
Reputation: 7726
The value 1231212123133333333333321 given by you is drastically larger than even uint64_t
could hold (overflows). In my case, the maximum range of a uint64_t
(occupies 8-bytes data) datatype is:
0 to +18446744073709551615
To know the limits in your platform, i.e. practically, take a little bit help of C++ library limits
:
#include <limits>
std::cout << std::numeric_limits<uint64_t>::max() << std::endl;
Notice that it may vary on different computer architectures.
Upvotes: 3
Reputation: 2850
int
, which is a signed (usually 32 bit) number can only hold numbers from -2147483648 to 2147483647. When you're trying to input such a big number, std::cin
fails to do so, an instead sets maximum possible value.
Now, you're trying to multiply biggest possible value to hold by 2, it causes an overflow and UB, technically anything can happen in your code, but what seems to be happening with the compiler you're using is the following.
int
is 2 complement, it means that the biggest value has the following bit representation.
01111111111111111111111111111111, or 0x7fffffff in hex
When you're trying to multiply by 2, you're doing a left shit by 1 bit and you get
11111111111111111111111111111110, or 0xfffffffe which represents -2.
Remember though that even if it happens with the compiler and flags/optimization you're using right now, using different compiler and flags can produce different results, as you're causing Undefined Behavior.
Upvotes: -1