Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can std::nextafter() in C++11 produce smaller values than std::numeric_limits::min?

Tags:

c++

c++11

I just noticed that std::nextafter(0, 1) on my system seems to produce a value greater than 0 and lower than std::numeric_limits::min().

How is this possible? I thought min() returns the smallest possible number greater than 0.

#include <iostream>

int main(int argc, char *argv[])
{
    double next = std::nextafter(0.0, 1.0);
    double min = std::numeric_limits<double>::min();
    std::cout << "next: " << next << "\nmin:  " << min << "\nnext<min: " << (next < min) << "\nnext>0:   " << (next > 0.0) << std::endl;
}

Output:

next: 4.94066e-324
min:  2.22507e-308
next<min: 1
next>0:   1

My compiler is MinGW 5.3.0 32bit.

like image 995
SteakOverflow Avatar asked Sep 01 '17 13:09

SteakOverflow


1 Answers

std::nextafter() is allowed to return a subnormal number (in your case the smallest subnormal number since you are traversing in an upwards direction from zero), whereas std::numeric_limits::min() is the smallest non-subnormal number.

Reference: http://en.cppreference.com/w/cpp/numeric/math/nextafter

like image 133
Bathsheba Avatar answered Oct 18 '22 03:10

Bathsheba