0

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.

Dmitry
  • 2,026
  • 1
  • 18
  • 22
a__c
  • 11
  • 3
  • 1
    Are you sure you want/need to support `NaN`? Given it's not a number, I wouldn't say it had a definite ordering to follow. – Carcigenicate Mar 10 '17 at 16:11
  • Yes I do want to include NaN because it's part of my essay. I have competed all the other parts except adding this into the algorithm – a__c Mar 10 '17 at 16:17
  • 2
    "NaN is not getting sorted properly" - how would you define "properly"? Should it be at the beginning, the end, in the middle? Before or after the infinities? – Mark Ransom Mar 10 '17 at 16:56
  • 1
    Possible duplicate of [Why is there a 0 floating point in my Quicksort algorithm list and how to include a NaN into my Quicksort algorithm?](http://stackoverflow.com/questions/42721985/why-is-there-a-0-floating-point-in-my-quicksort-algorithm-list-and-how-to-includ) – Paul Hankin Mar 10 '17 at 17:12
  • 1
    You asked the same question twice as two different users? Why? – Paul Hankin Mar 10 '17 at 17:12
  • 1
    You also asked an almost identical question last week, with quicksort replaced with bubble sort: http://stackoverflow.com/questions/42578497/bubble-sort-in-c-with-nan-infinity-and-infinity . – Paul Hankin Mar 10 '17 at 17:15
  • Possible duplicate of [Bubble Sort in C with NAN, INFINITY AND -INFINITY](http://stackoverflow.com/questions/42578497/bubble-sort-in-c-with-nan-infinity-and-infinity) – nibot Mar 13 '17 at 18:13

2 Answers2

2

By convention, NaN (literally "not a number") is not comparable with other quantities. Any comparison will be false. NaN < 5 is false. NaN > 5 is false. NaN == NaN is false. NaN != NaN is false.

Your comparison function, as written, takes the opposite approach. It says that NaN is equal to anything and everything (by returning zero if NaN is involved). Quick sort is not a stable sorting algorithm. The manpage for qsort says: "If two members compare as equal, their order in the sorted array is undefined."

You must do one of two things:

  1. Use a stable sorting algorithm, in which items that compare as equal are guaranteed to remain in their original order.
  2. Use the isnan function in your comparison function to identify NaNs, and decide what to do about them.

Which path you choose comes down to specifying the behavior that you consider "correct".

nibot
  • 14,428
  • 8
  • 54
  • 58
0

Probably the easiest thing to do would be to pass over the list, removing any NaN's, and recording how many you found. Then after the list has been sorted, appending that many NaN's to either the front or the back of the sorted list, wherever you choose to place NaN's.

E.D.
  • 639
  • 6
  • 14