Sind Gleitkommazahlen sortierbar? Wegen NaN gibt es keine Ordnung und das Sortieren mit NaN im Datensatz schlägt fehlt.
Aber man kann sich schnell selbst eine Ordnung bauen:
#include <vector>
#include <algorithm>
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
using nl = numeric_limits<double>;
int main() {
cout << boolalpha;
vector<double> v { 1.0, 2.0, 0.0, 1.0/3.0, -0.0,
nl::quiet_NaN(), nl::infinity(), nl::epsilon(), nl::denorm_min(),
nl::max(), nl::min(), nl::lowest()
};
sort(v.begin(), v.end());
for(const auto x : v) {
cout << x << " ";
}
cout << "\nis sorted " << is_sorted(v.begin(), v.end()) << " << offensichtlich falsch\n";
auto comp = [](const auto lhs, const auto rhs) {
if(isnan(lhs)) return false;
if(isnan(rhs)) return true;
return lhs<rhs;
};
sort(v.begin(), v.end(), comp);
for(const auto x : v) {
cout << x << " ";
}
cout << "\nis sorted " << is_sorted(v.begin(), v.end()) << "\n";
}
-1.79769e+308 0 -0 0.333333 1 2 nan 4.94066e-324 2.22507e-308 2.22045e-16 1.79769e+308 inf is sorted true << offensichtlich falsch -1.79769e+308 0 -0 4.94066e-324 2.22507e-308 2.22045e-16 0.333333 1 2 1.79769e+308 inf nan is sorted true << richtig