什么时候会: std::abs(x -y) < std::numeric_limits:<double>:min()

When will: std::abs(x -y) < std::numeric_limits<double>::min()

本文关键字:lt std limits double gt min 什么时候 abs numeric      更新时间:2023-10-16

在此链接中,有一个函数可以检查2浮点值的平等:

template<class T>
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
    almost_equal(T x, T y, int ulp)
{
    // the machine epsilon has to be scaled to the magnitude of the values used
    // and multiplied by the desired precision in ULPs (units in the last place)
    return std::abs(x-y) <= std::numeric_limits<T>::epsilon() * std::abs(x+y) * ulp
        // unless the result is subnormal
        || std::abs(x-y) < std::numeric_limits<T>::min();
}

但是我不完全了解std::abs(x-y) < std::numeric_limits<T>::min()实际上会发生什么?有例子吗?谢谢。

std::numeric_limits<T>::min()返回可以表示的最小的"正常"浮点值。IEEE754可以将值表示为0和std::numeric_limits<T>::min()之间,为"亚正常"数字。看看这个问题,该问题有几个解释的答案。

您可以轻松地生成一个亚正态值:

// Example program
#include <iostream>
#include <limits>
#include <cmath>
int main()
{
    std::cout << "double min: " << std::numeric_limits<double>::min() << " subnormal min: ";
    std::cout << std::numeric_limits<double>::denorm_min() << 'n';
    double superSmall = std::numeric_limits<double>::min();
    std::cout << std::boolalpha << "superSmall is normal: " << std::isnormal(superSmall)  << 'n';
    double evenSmaller = superSmall/2.0;
    std::cout << "evenSmaller = " << evenSmaller << 'n';
    std::cout << std::boolalpha << "evenSmaller is normal: " << std::isnormal(evenSmaller);
    std::cout << std::boolalpha << "std::abs(superSmall - evenSmaller) < std::numeric_limits<double>::min(): " << (std::abs(superSmall - evenSmaller) < std::numeric_limits<double>::min());
}

在我的计算机上生成以下内容:

double min: 2.22507e-308 subnormal min: 4.94066e-324
superSmall is normal: true
evenSmaller = 1.11254e-308
evenSmaller is normal: false 
std::abs(superSmall - evenSmaller) < std::numeric_limits<double>::min(): true