Article View: comp.std.c
Article #4927Re: Restrictions on qsort compare function?
From: Norman Diamond
Date: Fri, 22 Mar 1996 02:58
Date: Fri, 22 Mar 1996 02:58
49 lines
2720 bytes
2720 bytes
In article <1996Mar21.113301.2622@sq.com>, msb@sq.com (Mark Brader) writes: # The function shall return an integer less than, equal to, or greater # than zero if the first argument is considered to be respectively # less than, equal to, or greater than the second. >In other words, it must yield an ordering of the possible data values. >This is only the case if > >1. It is a pure function: > repeated calls to compar(x,y) yield a result having the > same sign each time > >2. It is antisymmetric (I think that's the right word): > if compar(x,y) < 0, then compar(y,x) > 0 > if compar(x,y) == 0, then compar(y,x) == 0 > if compar(x,y) > 0, then compar(y,x) < 0 > >3. If is transitive: > if compar(x,y) > 0 && compar(y,z) > 0, then compar(x,z) > 0 You have given an intuitively and logically reasonable definition of a comparison system, but the standard does not. The standard has trouble defining a pure binary numeration system and even has trouble refering to a document which did it. Until the standard finds a definition of a comparison system, it is defective. >>>On tracking down a bug in some old code I noticed that we had the >>>compare function returning something like "a > b" instead of "b - a". >>Just one sentence earlier, you gave the exact reason why >>it doesn't matter if you do "a > b" instead of "a - b". >Of course it matters. With this function, if compar(x,y) > 0, then >compar(y,x) == 0. This is not antisymmetric. Argh! I was remembering what the hardware usually does for comparison instructions, and forgot that C operators lose part of that information. I must be getting Cnile. >(a > b)? 1: (a < b)? -1: 0 Yup. Incidentally, do you think the average implementation's interprocedural optimization phase will change this user function into one machine instruction in the library's qsort() function :-? -- << If this were the company's opinion, I would not be allowed to post it. >> "I paid money for this car, I pay taxes for vehicle registration and a driver's license, so I can drive in any lane I want, and no innocent victim gets to call the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
Technical Details
Message-ID:
<4it51b$ng8@usenet.pa.dec.com>Path:
arc1-ca.pugleaf.net!tbj.dec.com!diamondReferences:
<4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <1996Mar21.113301.2622@sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!tbj.dec.com!diamond From: diamond@tbj.dec.com (Norman Diamond) Newsgroups: comp.std.c Subject: Re: Restrictions on qsort compare function? Date: 22 Mar 1996 02:58:51 GMT Organization: Digital Equipment Corporation Japan , Tokyo Message-ID: <4it51b$ng8@usenet.pa.dec.com> References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <1996Mar21.113301.2622@sq.com> Reply-To: diamond@tbj.dec.com (Norman Diamond) NNTP-Posting-Host: jit533.tbj.dec.com