开发者

C++: Comparing list of doubles with some invalid values (QNAN)

开发者 https://www.devze.com 2023-02-03 15:37 出处:网络
i need to compare two std::list < double >, bu开发者_开发百科t some doubles may be invalid numbers (QNAN).

i need to compare two std::list < double >, bu开发者_开发百科t some doubles may be invalid numbers (QNAN). If any invalid numbers are list entries the compare process won't work, because a comparison of the same invalid value will always result in 'false'. What is the easiest and most elegant way to solve the problem?

My idea was to create copies of both lists, iterate through them and remove invalid values and then compare the remaining lists. The lists will typically have 20-50 values in them. Is there a more resource friendly way to solve it?


bool compare(float f1, float f2)
{
    return (f1 != f1 && f2 != f2)? true: f1 == f2;
}

std::list<float> l1, l2;
bool res = std::equal(l1.begin(), l1.end(), l2.begin(), compare);
0

精彩评论

暂无评论...
验证码 换一张
取 消