On Fri, Apr 21, 2006 at 08:19:23PM +0200, Michael Kerrisk wrote:
> Justin,
> 
> > > What man page is this intended for?
> > qsort()
> > 
> > > > Included is a sample program that demonstrates how to sort
> > > > IEEE floating point values, including NAN and friends, in a
> > > > useful way; please consider including it.
> > > > 
> > > > // make CFLAGS='-W -Wall -O3 -g -std=gnu99' LDFLAGS=-lm fpnansort
> > > > #include <math.h>
> > > > #include <stdlib.h>
> > > > #include <stdio.h>
> > > > #include <assert.h>
> > > > 
> > > > int fpcomp(const void *a, const void *b)
> > > > {
> > > >         double x=*(double *)a,
> > > >                y=*(double *)b;
> > > > 
> > > >         if (x<y) return -1;
> > > >         else if (x>y) return 1;
> > > >         // The not-normal values will be grouped together, but
> > > >         // otherwise unordered:
> > > >         else if (!isnormal(x)) return 1;
> > > >         else if (!isnormal(y)) return -1;
> > > 
> > > Why isnormal()?  
> > "Why not" ?
> > 
> > Seriously, I want all the IEEE special number foo to get out of my
> > way, and this does wha tI want.
> 
> Do you realise that "!isnormal(0.0) == 1"?
No; I guess this is the "subnormal" case.

> > > >         assert(x==y);
> > > 
> > > What is assert doing here?
> > Well, the current qsort() example uses assert() on argc, so why not?
> 
> The context is different: one kills the program because it
> got bad arguments.
... but not because of an internal inconsistency, which is what
assert() is meant to test.

> The other kills the quicksort because the comparison function
> encountered a case that it didn't expect.
... which is a runtime test that stuff is "as it is expected".

> > Seriously, I *think* that x==y is guaranteed, since x and y are
> > both "normal" values, but there might be other special cases of
> > which I'm not even aware; this is as way of checking for them, and
> > at the same time indicating my intent.  (Actually, "subnormal" may
> > just be that case .. will think about it.)
> 
> I'm *not at all* convinced that including an example like this is
> appropriate for the qsort man page: it isn't the place to teach
> about floating point numbers.
Well ok :)

Justin


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to