c++ - Sorting positive NaN's and negative NaN's in a vector -


i attempting create program using bubble sort in c++, have include positive , negative nans , sort them,i have got point program sort negative positive in ascending order including -0 , 0 stuck on making nans not mixed up.

how go sorting these nans, possibly -nans on lhs of integers , nans on rhs?

you can use isnan detect nan , signbit check sign. can use signbit distinguish between positive , negative 0 too.

you need make custom comparator sorting.

comparator example:

bool double_order(double lhs, double rhs) {     if(std::isnan(lhs)) {         if(std::signbit(lhs))  //negative nan less             return not (std::isnan(rhs) && std::signbit(rhs)); //except -nan         else             return false; //positive nan never less     }     if(std::isnan(rhs))         if(std::signbit(rhs)) //negative nans tot larger             return false;         else                 //positive nan larger             return true;     //except other +nan, covered earlier     if(lhs == 0.0 && rhs == 0.0)  //if both operands 0, check ±0         return std::signbit(lhs) > std::signbit(rhs); //signbit of negative true     return lhs < rhs; //otherwise normal comparison (covers infinities) } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -