如果数组的第一个元素是NAN,则如何防止STD :: Min和Max返回NAN?

发布于 2025-01-30 05:00:29 字数 775 浏览 3 评论 0原文

有没有办法使最小/最大(std :: min_element)忽略所有NAN?我的意思是,它似乎忽略了中间的NAN,但如果第一个元素是Nan ,则不会。

样本:

template <typename T> inline void GetMinMax(const T* data, const int len, T& min, T& max)
{
    min = *std::min_element(data, data + len);
    max = *std::max_element(data, data + len);
}

float min, max;
std::array<float, 4> ar1 = { -12, NAN, NAN, 13 };
GetMinMax<float>(&ar1[0], ar1.size(), min, max); //min: -12. max: 13

std::array<float, 4> ar2 = { -12, 3, 13, NAN };
GetMinMax<float>(&ar2[0], ar2.size(), min, max);//min: -12. max: 13

std::array<float, 4> ar3 = { NAN, -12, 3, 13 };
GetMinMax<float>(&ar3[0], ar3.size(), min, max);//min: -nan(ind). max: -nan(ind)  !!!!

Is there a way to make min/max (std::min_element) ignore all NANs? I mean, it seems to ignore NANs in the middle but not if the first element is NAN.

Sample:

template <typename T> inline void GetMinMax(const T* data, const int len, T& min, T& max)
{
    min = *std::min_element(data, data + len);
    max = *std::max_element(data, data + len);
}

float min, max;
std::array<float, 4> ar1 = { -12, NAN, NAN, 13 };
GetMinMax<float>(&ar1[0], ar1.size(), min, max); //min: -12. max: 13

std::array<float, 4> ar2 = { -12, 3, 13, NAN };
GetMinMax<float>(&ar2[0], ar2.size(), min, max);//min: -12. max: 13

std::array<float, 4> ar3 = { NAN, -12, 3, 13 };
GetMinMax<float>(&ar3[0], ar3.size(), min, max);//min: -nan(ind). max: -nan(ind)  !!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2025-02-06 05:00:29

最安全的路径是删除在应用任何Min-Max标准算法之前, 的所有NAN值。

考虑std :: min_element 1 的可能实现:

template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
    if (first == last) return last;
 
    ForwardIt smallest = first;       // <-- If the first is a NaN...
    ++first;
    for (; first != last; ++first) {
        if (*first < *smallest) {     // <-- This condition will always be FALSE
            smallest = first;
        }
    }
    return smallest;                  // <-- An iterator to a NaN is returned
}

更格,C ++标准 2 指定:

27.8.1 一般 [alg.sorting.general]

[alg.sorting]两个版本:一个采用类型的函数对象比较的函数对象,一个使用operator&lt;


比较是函数对象类型( [function.objects ] )符合模板参数的要求,名为binaryPredicate [算法。要求] )。当将上下文转换为bool [conv] ),如果呼叫的第一个参数小于第二个论点,则会产生真实,否则为错误。 比较comp在整个算法中都使用订购关系。

对于所有采用比较的算法,有一个版本使用operator&lt;。也就是说,comp(*i,*j)!= false默认为*i&lt; *j!= false。对于 comp应在值上引起严格的弱排序。

术语 strict 是指所有x)的irreflexive关系(comp!comp(x,x,x))和术语弱的要求不如总订购的要求强,但比部分订购的要求更强。 如果我们将Equiv(a,b)定义为!comp(a,b)&amp;&amp; !

(4.1)comp(a,b)&amp;&amp; comp(b,c)暗示comp(a,c)

(4.2)equiv(a,b)&amp;&amp; equiv(b,c)暗示equiv(a,c)

问题,给定 float值/code>,是以下保留:

x&lt; nan == falsenan&lt; x == false,但是x!= nan

仅考虑float值的子集,我们可以实现要求。


1) https://en.cppreference.com/w/gorithm w/w/cpp/cpp/cpp/algorithm algorithm algorithm algorithm /min_element

2)我正在引用草稿 https://eel.is/c+++draft/alg.sorting.general ,重点是我的。

The safest path is to remove all the NaNs values from the range before applying any min-max standard algorithm.

Consider a possible implementation of std::min_element1:

template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
    if (first == last) return last;
 
    ForwardIt smallest = first;       // <-- If the first is a NaN...
    ++first;
    for (; first != last; ++first) {
        if (*first < *smallest) {     // <-- This condition will always be FALSE
            smallest = first;
        }
    }
    return smallest;                  // <-- An iterator to a NaN is returned
}

More formally, the C++ Standard2 specifies:

27.8.1 General [alg.sorting.general]

The operations in [alg.sorting] defined directly in namespace std have two versions: one that takes a function object of type Compare and one that uses an operator<.

Compare is a function object type ([function.objects]) that meets the requirements for a template parameter named BinaryPredicate ([algorithms.requirements]). The return value of the function call operation applied to an object of type Compare, when contextually converted to bool ([conv]), yields true if the first argument of the call is less than the second, and false otherwise. Compare comp is used throughout for algorithms assuming an ordering relation.

For all algorithms that take Compare, there is a version that uses operator< instead. That is, comp(*i, *j) != false defaults to *i < *j != false. For algorithms other than those described in [alg.binary.search], comp shall induce a strict weak ordering on the values.

The term strict refers to the requirement of an irreflexive relation (!comp(x, x) for all x), and the term weak to requirements that are not as strong as those for a total ordering, but stronger than those for a partial ordering. If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations:

(4.1) comp(a, b) && comp(b, c) implies comp(a, c)

(4.2) equiv(a, b) && equiv(b, c) implies equiv(a, c)

The problem, given any float value x, is that the following hold:

x < NaN == false and NaN < x == false, but x != NaN

Only considering the subset of the float values which are not NaNs we can fulfill the requirement.


1) https://en.cppreference.com/w/cpp/algorithm/min_element

2) I'm quoting the draft at https://eel.is/c++draft/alg.sorting.general , emphasis mine.

泪痕残 2025-02-06 05:00:29

NAN与其他数字进行比较是棘手的,请参见将数字与Nan进行比较的结果是什么?

因此,您需要做的是提供自定义比较函数,以便NAN不是元素std :: min_element返回。可能只是使“ nan&lt; x”返回false和“ x&lt; nan”返回true。类似于“&gt;”对于std :: max_element。或尝试过载&lt; =&gt;

注意:该解决方案可能取决于您对STL的实现,具体取决于实现使用的比较操作员。

NAN compares with other numbers is screwy, see What is the result of comparing a number with NaN?.

So what you need to do is to provide a custom compare function so that NAN isn't the element std::min_element returns. Probably just make "NAN < x" return false and "x < NAN" return true. Similar for ">" for std::max_element. Or try overloading <=>.

Note: the solution might depend on your implementation of the STL, depending on which comparison operator the implementation uses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文