1

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
deVoid
  • 13
  • 4

2 Answers2

8

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
florestan
  • 4,405
  • 2
  • 14
  • 28
1

long double and double are equivalent using Visual Studio.

Both double and long double are containing 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/-1.7E308 with at least 15 digits of precision.

This is in correspondence with the c++ standard. It only requires long double to have at least the precision of double. See here for a official reference.

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • Please define what you mean by "identical". They are different types. – Aykhan Hagverdili Jan 06 '20 at 12:34
  • Yeah, "equivalent" would be more accurate, but even then there's room for misunderstanding (as it suggests that the lexical keyword sequences `long double` and `double` are equivalent, which they aren't) – Lightness Races in Orbit Jan 06 '20 at 12:37
  • "`long double` and `double` are identical using Windows" is also not correct. MinGW-w64 has a `long double` with higher precision than plain `double`. – rubenvb Jan 06 '20 at 12:57