小编Edo*_*ren的帖子

为什么 numeric_limits Machine Epsilon 不满足 1+e>1 条件?

如果我没记错的话,Machine Epsilon 的定义是满足条件的最低数字:

1+e>1

我试图使用 来测试这个,std::numeric_limits<float>::epsilon()但如果您尝试使用以下方法获取先前的浮点数,则该值不满足此要求std::nextafter

#include <cmath>
#include <iostream>
#include <limits>

int main() {
    float e = std::numeric_limits<float>::epsilon();
    float previous = std::nextafter(e, -std::numeric_limits<float>::infinity());

    std::cout << std::boolalpha << ((1.0f + previous) > 1.0f) << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

This stills outputs true https://coliru.stacked-crooked.com/a/841e19dafcf0bf6f.

After trying to get the number using std::nextafter I noticed that the proper Machine Epsilon should be:

std::nextafter(std::numeric_limits<float>::epsilon() / 2.0f, std::numeric_limits<float>::infinity())
Run Code Online (Sandbox Code Playgroud)

I tested it using this code:

#include <cmath>
#include <iostream>
#include <limits> …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point precision floating-accuracy epsilon

3
推荐指数
1
解决办法
65
查看次数