Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpda!hpcupt1!thomasw From: thomasw@hpcupt1.cup.hp.com (Thomas Wang) Newsgroups: comp.lang.c Subject: Re: strcmp Message-ID: <67790004@hpcupt1.cup.hp.com> Date: 19 Jun 91 00:46:04 GMT References: <2695@m1.cs.man.ac.uk> Organization: Hewlett Packard, Cupertino Lines: 32 > Are there any examples of source code for strcmp, I need to implement > code for a large variety of types in the style of strcmp and > would like to see what has been done before. > I know this seems like a simple school exercise, but I would like to see > various implementations. In my opinion, the +1, 0, and -1 comparisons are flawed, because they do not take account unordered comparisons. So I usually define 6 macros. #define COMP_GREATER (1) #define COMP_EQUAL (0) #define COMP_LESS (-1) #define COMP_UNORDERED (MININT) #define GE(x) ((x) >= COMP_EQUAL) /* greater than or equal */ #define LE(x) (-(x) >= COMP_EQUAL) /* less than or equal, -MININT == MININT */ This system is compatible with strcmp(), yet able to deal with unordered comparison. For example, compare 1.0 against NaNs should return COMP_UNORDERED. int32 intcmp(int a, int b) { return((a == b) ? COMP_EQUAL : ((a > b) ? COMP_GREATER : COMP_LESS)); } -Thomas Wang (Everything is an object.) wang@hpdmsjlm.cup.hp.com thomasw@hpcupt1.cup.hp.com