What happens when you set an int or double value to less than the minimum? I need my program to recognize when a number is smaller then the minimum, and then set the value to equal the minimum instead. This behavior is not the default...

#include <iostream>

using namespace std;

int main(){
	int a = -1;
		cout<< "1. a is " << a <<endl;
		a = -12;
		cout<< "2. a is " << a <<endl;
		a = -123;
		cout<< "3. a is " << a <<endl;
		a = -1234;
		cout<< "4. a is " << a <<endl;
		a = -12345;
		cout<< "5. a is " << a <<endl;
		a = -123456;
		cout<< "6. a is " << a <<endl;
		a = -1234567;
		cout<< "7. a is " << a <<endl;
		a = -12345678;
		cout<< "8. a is " << a <<endl;
		a = -123456789;
		cout<< "9. a is " << a <<endl;
		a = -1234567890;
		cout<< "10. a is " << a <<endl;
		a = -12345678901;
		cout<< "11. a is " << a <<endl;
		a = -123456789012;
		cout<< "12. a is " << a <<endl;
		a = -1234567890123;
		cout<< "13. a is " << a <<endl;
		a = -12345678901234;
		cout<< "14. a is " << a <<endl;

	int b = 2147483648;
		cout<< "\n\nb is " << b <<endl;
		cout<< "b-1 is " << b-1 <<endl;
		cout<< "b-1+1 is " << b-1+1 <<endl;
		cout<< "b+1 is " << b+1 <<endl;
	b = b-1;
		cout<< "b-1+1 instrinsic is " << b+1 <<endl;
return 0;
}

which gives warnings:
"
intminimum.cpp: In function ‘int main()’:
intminimum.cpp:26: warning: overflow in implicit constant conversion
intminimum.cpp:28: warning: overflow in implicit constant conversion
intminimum.cpp:30: warning: overflow in implicit constant conversion
intminimum.cpp:32: warning: overflow in implicit constant conversion
"

which outputs:
"
1. a is -1
2. a is -12
3. a is -123
4. a is -1234
5. a is -12345
6. a is -123456
7. a is -1234567
8. a is -12345678
9. a is -123456789
10. a is -1234567890
11. a is 539222987
12. a is 1097262572
13. a is -1912276171
14. a is -1942892530


b is -2147483648
b-1 is 2147483647
b-1+1 is -2147483648
b+1 is -2147483647
b-1+1 instrinsic is -2147483648
"

What happens when you set an int or double value to less than the minimum?

Bad Things™.

I need my program to recognize when a number is smaller then the minimum, and then set the value to equal the minimum instead.

Or you could just not let your value go below the minimum. These overflow/undeflow calculations aren't terribly difficult. It's certainly a lot better than invoking undefined behavior and hoping for the best.

It really up to the compiler to decide what to do on overflow. I don't think there is a standard mechanism to handle overflow. But a lot of compilers, wraps around the values.


And it seems like you need a variable that can hold the proper range, promote the variable to the next level and you need not to worry about it. If all the regular data types cannot hold the range that you need, then look into a arbitrary precision library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.