News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

qsort with NaN

Started by Alessio, May 07, 2008, 10:23:37 AM

Previous topic - Next topic

Alessio

Hi,

I've an array with some values and NaNs.

How can I use qsort that NaN entries are moved to the end of the sorted array ?

Thanks!

JohnF

You need to create a function that compares the floats and returns a specific value depending on your criteria.

How would you normally compare for a NAN ?

John

Alessio

A way to do it:

int compare(const void *a, const void *b)
{
float arg1 = *((float *)a);
float arg2 = *((float *)b);

if ( isnan(arg1) && isnan(arg2) )
{
return 0;
}
else if ( isnan(arg1) )
{
return 1;
}
else if ( isnan(arg2) )
{
return -1;
}
else if ( arg1 < arg2 )
{
return -1;
}
else if ( arg1 == arg2 )
{
return 0;
}
else
{
return 1;
}
}


JohnF

Ok, fine. If that works for you.

John