This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
sorting: compare_doubles()
- From: Pasquale Tricarico <pasquale dot tricarico at pd dot infn dot it>
- To: gsl-discuss at sources dot redhat dot com
- Date: Thu, 06 Dec 2001 18:15:09 +0100
- Subject: sorting: compare_doubles()
Hi,
Probably there is a bug in the compare_doubles() function, as written
in the documentation of the 1.0 release. It's form is:
int compare_doubles (const double * a, const double * b)
{
return (int) (*a - *b);
}
This function returns -1,0 or +1 depending on the values assumed by *a
and *b, except in the case of two doubles with an absolute difference
smaller than one; i.e.:
a = 15.3;
b = 14.9;
in this case, the function will return ((int)0.4), that is equal to
zero, even if the two doubles are different!!
It is probably better to use some if(), i.e.:
int compare_doubles (const double * a, const double * b)
{
if ((*a) < (*b)) return (-1);
if ((*a) > (*b)) return (+1);
return 0;
}
Best regards.
P. Tricarico
----------------------------------------------------------
P. Tricarico PhD student in Physics
Website: http://www.pd.infn.it/~tricaric
----------------------------------------------------------