qsort compare functions
matthias.andree at gmx.de
Fri Nov 22 06:21:38 EST 2002
David Relson <relson at osagesoftware.com> writes:
> Qsort simply wants to know how to order two items. Bogofilter wants
> them in probability order (primarily) and alphabetic order
> (secondarily). Given this info, I assert that the proper code is:
> const discrim_t *d1 = id1;
> const discrim_t *d2 = id2;
> if (d1->prob > d2->prob) return 1;
> if (d1->prob < d2->prob) return -1;
> return strcmp(d1->key, d2->key);
> If there are no objections, I shall make this correction.
Object. This assumes we have a clear == semantic for double, which we
don't really have. The EPS stuff is there to kill rounding errors.
My code checks if d1->prob and d2->prob are close enough to each other
to consider them equal.
Remember: (1.0/3)*3 == 1.0 does not hold, and therefore < or > may match
even though the values are "equal" in the rounding error constraints
More information about the Bogofilter-dev