c++long-doublenumeric-limits

Wrong C++ long double min/max values using numeric_limits in Visual Studio 2019


Using Visual Studio Community 2019 v16.4.2 with the latest stuff it comes with on 64bit Win10.

While testing various datatype limits ran into a weird bug, numeric_limits can't distinguish between double and long double min/max values. Displays more reasonable results using NetBeans with default GNU Mac tool chain.

    // Type limits: float
    std::cout 
        << "MIN float        " << numeric_limits<float>::min() << "\n"
        << "MAX float        " << numeric_limits<float>::max() << "\n"
        << "MIN double       " << numeric_limits<double>::min() << "\n"
        << "MAX double       " << numeric_limits<double>::max() << "\n"
        << "MIN long double  " << numeric_limits<long double>::min() << "\n"
        << "MAX long double  " << numeric_limits<long double>::max() << "\n";

Console Output

MIN float        1.17549e-38
MAX float        3.40282e+38
MIN double       2.22507e-308
MAX double       1.79769e+308
MIN long double  2.22507e-308    // NetBeans on Mac 3.3621e-4932
MAX long double  1.79769e+308    // NetBeans on Mac 1.18973e+4932

Solution

  • The c++ standard only requires long double to have at least the precision of double, so there's nothing wrong with the output of your program.

    Quote from the standard (§3.9.1, lit 8):

    There are three floating point types: float,double, and long double.The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.