user1235183
user1235183

Reputation: 3072

Is DBL_MIN the smallest positive double?

Q: Is DBL_MIN the smallest positive double?

The code below seems to anwser this question with no. But if this is true, how is DBL_MIN the defined and what is its use or purpose.

Platform: Windows7 & Visual Studio 2013

double next_to_zero = std::nextafter(0.0,DBL_MIN);
bool b =  DBL_MIN <= next_to_zero;
std::cout << std::boolalpha 
          << "is dbl_min the smallest representable double? "
          << b << '\n';

std::cout << std::setprecision(56)
          << "dbl_min = " << DBL_MIN << '\n'
          << "next to zero = " << next_to_zero;

outputs:

is dbl_min the smallest representable double? false

dbl_min = 2.2250738585072013830902327173324040642192159804623318306e-308

next to zero = 4.9406564584124654417656879286822137236505980261432476443e-324

Upvotes: 13

Views: 19258

Answers (3)

Kai Petzke
Kai Petzke

Reputation: 2984

Instead of using header constants like DBL_MIN, I recommend using the numerical_limits class from header . The later provides both min(), which is the smallest normalized number, and denorm_min(), which is the absolute smallest representable number, for each floating point type known by the compiler. Example for double:

#include <iostream>
#include <limits>

int main() {
    std::cout << "min: " << std::numeric_limits<double>::min() << '\n';
    std::cout << "denorm min: " << std::numeric_limits<double>::denorm_min() << '\n';
    return 0;
}

Typical output:

min: 2.22507e-308
denorm min: 4.94066e-324

Please keep in mind, that many CPUs fall back to microcode assist and require a huge number of extra cycles for arithmetic with denormalized floating point values. The worst cases are usually, when adding or multiplying normalized numbers leads to denormalized ones. Therefore, it is strongly recommended to avoid their usage, unless absolutely necessary.

Upvotes: 0

chux
chux

Reputation: 154174

Is DBL_MIN the smallest positive double?

Not certainly.
DBL_MIN is the smallest positive normal double.

DBL_TRUE_MIN is the smallest positive double (since C++17). It will be smaller than DBL_MIN when double supports subnormals.

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234835

I'm restricting this answer, perhaps unnecessarily, to IEEE754 floating point.

DBL_MIN is not allowed to be a subnormal number.

But std::nextafter is allowed to return a subnormal number.

Hence the return value of the latter could be less than DBL_MIN.

For more details see https://en.wikipedia.org/wiki/Denormal_number

Upvotes: 14

Related Questions