I am fairly new to programming and want to understand how to detect and prevent numeric overflows in a program. I am wanting to learn how to:
1.) detect when an overflow will happen 2.) prevent it from happening and return the correct value when not overflowing and finally, 3.) how to return something to my test method to test the program.
for my add_numbers method, would it be best to run IF/ELSE statements and check for the max integer? or is there a better way to do this? To prevent the overflow would I just end the program or rely on the wraparound?
How can I use my test function to test for this?
#include <iostream>
#include <limits>
template <typename T>
T add_numbers(T const& start, T const& increment, unsigned long int const& steps)
{
T result = start;
for (unsigned long int i = 0; i < steps; ++i)
{
result += increment;
}
return result;
}
template <typename T>
void test_overflow()
{
// I don't change here (start)
const unsigned long int steps = 5;
const T increment = std::numeric_limits<T>::max() / steps;
const T start = 0;
std::cout << "Overflow Test of Type = " << typeid(T).name() << std::endl;
// I don't change the above block of code (end)
std::cout << "\tAdding Numbers Without Overflow (" << +start << ", " << +increment << ", " << steps << ") = ";
T result = add_numbers<T>(start, increment, steps);
std::cout << +result << std::endl;
std::cout << "\tAdding Numbers With Overflow (" << +start << ", " << +increment << ", " << (steps + 1) << ") = ";
result = add_numbers<T>(start, increment, steps + 1);
std::cout << +result << std::endl;
}
void do_overflow_tests(const std::string& star_line)
{
std::cout << std::endl << star_line << std::endl;
std::cout << "*** Running Overflow Tests ***" << std::endl;
std::cout << star_line << std::endl;
test_overflow<char>();
test_overflow<wchar_t>();
test_overflow<short int>();
test_overflow<int>();
test_overflow<long>();
test_overflow<long long>();
// unsigned integers
test_overflow<unsigned char>();
test_overflow<unsigned short int>();
test_overflow<unsigned int>();
test_overflow<unsigned long>();
test_overflow<unsigned long long>();
// real numbers
test_overflow<float>();
test_overflow<double>();
test_overflow<long double>();
}
int main()
{
const std::string star_line = std::string(50, '*');
std::cout << "Starting Numeric Underflow / Overflow Tests!" << std::endl;
do_overflow_tests(star_line);
do_underflow_tests(star_line);
std::cout << std::endl << "All Numeric Underflow / Overflow Tests Complete!" << std::endl;
return 0;
}