Search code examples
c++numeric-limits

Why doesn't numeric_limits<T>::min() return the smallest value?


When I run this code:

#include <limits>
#include <cstdio>

#define T double

int main()
{
    static const T val = std::numeric_limits<T>::min();
    printf( "%g/2 = %g\n", val, val/2 );
}

I would expect to see an unpredictable result. But I get the correct answer:

(16:53) > clang++ test_division.cpp -o test_division
(16:54) > ./test_division 
2.22507e-308/2 = 1.11254e-308

How is this possible?


Solution

  • Because min gives you the smallest normalized value. You can still have smaller denormalized values (see http://en.wikipedia.org/wiki/Denormalized_number).