C++ 11中的std :: nextafter()如何产生比std :: numeric_limits :: min更小的值?

Ste*_*low 13 c++ c++11

我只是注意到std::nextafter(0, 1)在我的系统上似乎产生一个大于0且小于0的值std::numeric_limits::min().

这怎么可能?我认为min()返回大于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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

next: 4.94066e-324
min:  2.22507e-308
next<min: 1
next>0:   1
Run Code Online (Sandbox Code Playgroud)

我的编译器是MinGW 5.3.0 32bit.

Bat*_*eba 17

std::nextafter()允许返回一个次正规数(在你的情况下是最小的正常数,因为你是从零向上行进),而是std::numeric_limits::min()最小的非次正规数.

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

  • 请注意,次正规数是一个真正的痛苦. (2认同)