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

时间:2017-09-01 13:45:57

标签: c++ c++11

我刚注意到我系统上的glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);似乎产生的值大于0且低于std::nextafter(0, 1)

这怎么可能?我以为std::numeric_limits::min()会返回大于0的最小可能数字。

min()

输出:

#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;
}

我的编译器是MinGW 5.3.0 32位。

1 个答案:

答案 0 :(得分:17)

允许

std::nextafter()返回次正规数字(在您的情况下是最小的次正规数,因为您从零开始向上移动),而std::numeric_limits::min()是最小的非次正规数。

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