The following is my code:
#include <stdlib.h>
#include <stdio.h>
#include <limits>
#define INFINITY std::numeric_limits<float>::infinity()
#define NEGINFINITY -std::numeric_limits<float>::infinity()
int floatcomp(const void* elem1, const void* elem2)
{
if (*(const float*)elem1 < *(const float*)elem2)
return -1;
return *(const float*)elem1 > *(const float*)elem2;
}
int main()
{
float array[10] = {INFINITY, 3.5f, 144.4f, NAN, 12.4f, NEGINFINITY, 1.4f, -0.0f, 5.9f};
int i;
for (i = 0; i < 10; i++)
printf("%f\n", array[i]);
printf("\n");
qsort(array, 10, sizeof(float), floatcomp);
for (i = 0; i < 10; i++)
printf("%f\n", array[i]);
return 0;
}
Adding NaN to the array, the NaN is not getting sorted properly as it should be and I'm not able to correctly add in two different mantissa's (signalling and quiet) for the NaN into my code as a string at the top.