Can anyone explain why this snippet does not produce an underflow exception (on MSVC 2013 and on gcc @ coliru)? The value returned from the average function is lower than DBL_MIN
.
#include <float.h>
#include <iostream>
#include <iomanip>
#include <limits>
const size_t g_testValueCount = 10;
const double g_testValues[g_testValueCount] = { DBL_MIN, 0 };
double unsafeAverage(const double* testValues, size_t testValueCount)
{
double result = 0;
for (size_t testValueIndex = 0; testValueIndex < testValueCount; ++testValueIndex)
{
result += testValues[testValueIndex];
}
return result / testValueCount;
}
int main(int argc, char** argv)
{
std::cout << "DBL_MIN = " << std::setprecision(std::numeric_limits<double>::digits10) << DBL_MIN << std::endl;
try
{
std::cout << " AVG = " << std::setprecision(std::numeric_limits<double>::digits10) << unsafeAverage(g_testValues, g_testValueCount) << std::endl;
}
catch (...)
{
std::cout << "unsafeAverage caught an exception!" << std::endl;
}
return 0;
}