如果数组的第一个元素是NAN,则如何防止STD :: Min和Max返回NAN?
有没有办法使最小/最大(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最安全的路径是删除在应用任何Min-Max标准算法之前, 的所有NAN值。
考虑
std :: min_element
1 的可能实现:更格,C ++标准 2 指定:
问题,给定
float
值/code>,是以下保留:x&lt; nan == false
和nan&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_element
1:More formally, the C++ Standard2 specifies:
The problem, given any
float
valuex
, is that the following hold:x < NaN == false
andNaN < x == false
, butx != 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.
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.