// Derived from stackoverflow: https://stackoverflow.com/questions/2835278/what-is-a-bias-value-of-floating-point-numbers // g++ -std=c++14 sorter.cpp #include #include #include int main() { // some arbitrary floating point values std::vector vals = {1e21, 1, 2.2, 2, 123, 1.1, 0.0001, 3, 17, -0.001, -0.00101, -1e22}; std::vector ivals; // Take those floating point values, and treat the bits as integers: for (auto &&v : vals) ivals.push_back(*reinterpret_cast(&v)); // Sort them as integers: std::sort(ivals.begin(), ivals.end(), [](auto a, auto b) { if (a < 0.0 && b < 0.0) return b < a; return a < b; }); // Print out both the integers and the floating point value those bits represent: for (auto &&i : ivals) std::cout << i << "\t(" << *reinterpret_cast(&i) << ")\n"; }