本文介绍了为什么Release / Debug对std :: min有不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

以下是测试程序:

void testFunc()
{
    double maxValue = DBL_MAX;
    double slope = std::numeric_limits<double>::quiet_NaN();

    std::cout << "slope is " << slope << std::endl;
    std::cout << "maxThreshold is " << maxValue << std::endl;
    std::cout << "the_min is " << std::min( slope, maxValue) << std::endl;
    std::cout << "the_min is " << std::min( DBL_MAX, std::numeric_limits<double>::quiet_NaN()) << std::endl;
}

int main( int argc, char* argv[] )
{
    testFunc();
    return 0;
}



在Debug中,我得到:

In Debug, I get:

slope is nan
maxThreshold is 1.79769e+308
the_min is nan
the_min is 1.79769e+308

在发布中,我得到:

slope is nan
maxThreshold is 1.79769e+308
the_min is 1.79769e+308
the_min is nan

为什么我在Release比Debug中得到不同的结果?

Why would I get a different result in Release than Debug?

我已经检查过Stack Overflow href =http://stackoverflow.com/questions/1632145/use-of-min-and-max-functions-in-c>在C ++中使用min和max函数 ,以及它没有提及任何版本/调试差异。

I already checked Stack Overflow post Use of min and max functions in C++, and it does not mention any Release/Debug differences.

我使用Visual Studio 2015。

I am using Visual Studio 2015.

推荐答案

在中比较NAN与任何内容总会产生 false

In IEEE 754 comparing NAN to anything will always yield false, no matter what it is.

slope > 0; // false
slope < 0; // false
slope == 0; // false

更重要的是你

slope < DBL_MAX; // false
DBL_MAX < slope; // false

所以看起来编译器重新排序参数/ use > ; < = 而不是< ,这就是为什么你得到不同的结果

So it seems that the compiler reorders the parameters/uses > or <= instead of <, and that's why you get the differing results.

例如,这些函数可以描述为

For example, those functions could be described as such

发布:

double const& min(double const& l, double const r) {
    return l <= r ? l : r;
}

调试:

double const& min(double const& l, double const& r) {
    return r < l ? r : l;
}

std :: min的需求(LessThanComparable) 旁白,算术具有相同的含义。但是当它们与NaN一起使用时,它们产生不同的结果。

The requirements (LessThanComparable) on std::min aside, those have the same meaning arithmetically. But they yield different results when you use them with NaN.

这篇关于为什么Release / Debug对std :: min有不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:38