Mail Archives: djgpp/2000/01/21/14:46:04
Eric Rudd <rudd AT cyberoptics DOT com> wrote:
> I think it's clear from both standards that an inconsistent
> comparison function results in undefined behavior of qsort.
Yep. Guess someone filed a Defect Report to complain that the older
version didn't really state that clearly enough, so the changed the
wording. The new version leaves no room for doubt.
> However, I don't know how to *guarantee* consistent behavior in the
> comparison function, so the quality of implementation is important.
I guess your example case would have to be covered by the usual rule
that 'Thou shalt not test floating point numbers for exact
(in)equality'. Especially not on an x87 FPU without taking special
precautions against excess precision :-)
> The problem of inconsistent comparison functions would be of little
> importance if the implementation of qsort 1) never deferenced beyond
> the ends of the array, no matter what nonsense is returned from the
> comparison function;
Additional work overhead, for one. Stability under incorrect usage has
never really been a strong point of the C Standard Library, so why
should qsort() be treated differently? C does it fast and dirty, not
rock-solid and slow :-)
> 2) did a "fuzzy" sort with a "fuzzy" comparison function.
But that wouldn't be 'sorting' any longer. Not even to mention that
anno domini 1989, I don't think the 'fuzzy' hype had already
happened...
Just some minor comments regarding that comparison function you show:
> x = atof((const char *) a);
> y = atof((const char *) b);
Bad idea. atof()ing the same string more than once is a waste of time
and effort. Scanning strings into floating point numbers is just about
the slowest operation in the whole library. Only non-hardwired
transcendental functions are bound to be even slower.
And, of course, the potential of getting the comparison wrong would
become smaller without the atof() calls, too.
> retval = 0;
> if (x > y) retval = 1;
> if (x < y) retval = -1;
For this part to word as advertised, I think the '-ffloat-store' gcc
option might be needed.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -