computer science goes bonk / all posts / rss / about

feuding with std::numeric_limits

My long-standing feud with std::numeric_limits may be coming to a close:

#include <iostream>
#include <limits>

int main() {
  std::cout                                  // example output:
    << std::numeric_limits<short>::min()     // -32768
    << std::numeric_limits<short>::max()     //  32767

    << std::numeric_limits<int>::min()       // -2147483648
    << std::numeric_limits<int>::max()       //  2147483647

    << std::numeric_limits<double>::min()    //  2.22507e-308...wait, what?
    << std::numeric_limits<double>::max()    //  1.79769e+308
}

The minimum value for a double is basically zero? Get outta here.

Fortunately, C++11 came to its senses -- partially -- and provides std::numeric_limits::lowest as a way of getting what most people expect to get from std::numeric_limits::min:

#include <iostream>
#include <limits>

int main() {
  std::cout                                  // example output:
    << std::numeric_limits<double>::lowest() // -1.79769e+308 (yay!)
    << std::numeric_limits<double>::max()    //  1.79769e+308   
}

It's not quite as sensible as boost::numeric::bounds, which defines both lowest and highest, but whatever. Baby steps. We'll get there someday.