Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!noao!mcdsun!fnf From: fnf@mcdsun.UUCP (Fred Fish) Newsgroups: comp.lang.c Subject: Expressions compared to zero in *c* Message-ID: <374@mcdsun.UUCP> Date: Tue, 8-Sep-87 12:03:28 EDT Article-I.D.: mcdsun.374 Posted: Tue Sep 8 12:03:28 1987 Date-Received: Thu, 10-Sep-87 00:47:37 EDT Organization: Motorola Microcomputer Division, Tempe, Az. Lines: 173 I am posting this for a coworker, please respond directly to him with comments or questions. Thanks. His email address is hao!noao!mcdsun!nud!dragon!jon -Fred ======cut before next line======cut after this line=====cut before next line== Message-ID: <472@dragon.UUCP> Date: 18 Aug 87 22:47:34 GMT Reply-To: jon@dragon.UUCP (Jon Kettenhofen, x5656) Organization: Motorola Microcomputer Division, Tempe, Az. Lines: 159 Does (a-b) < 0 imply a < b ? Pick a and b for controversy. Here's a program, the output of which may cause some uproar. The answers I like most are the ones which ignore overflow. Do you believe that the ANSI standard should define this can of worms or cop out with the usual "implementation defined"? jon kettenhofen, Motorola MCD, Tempe AZ ======cut before next line======cut after this line=====cut before next line== /* Inconsistent comparison operator when overflow is forced. The problem in these cases has to do with the special case of comparison to zero. The resultant status flags from the calculation of the (left-hand) expression are said (in reader.c) to be the same as the comparison of that result with zero. However, if the zero is instead in a variable, we shouldn't get the same result because the overflow status will be reset (0) when the result of the expression is compared with the variable. The VAX compiler code never looks at overflow. jon 870727 Error status at present is arbitrary. jon 870728 */ #define VAL1 0x81818181 #define VAL2 0x7f7f7f7f main() { int error = 0; char z; z ^= z; { #define TYPA char /* char, short, int, long */ #define sTYPA "(char)" /* char, short, int, long */ unsigned TYPA a, b; TYPA c = 0; a = VAL1; b = VAL2; if( (TYPA) a > (TYPA) b ) { ++error; } printf("%sa<%sb is %x\n", sTYPA, sTYPA, ( (TYPA) a < (TYPA) b ) ); if( (TYPA) ( a - b ) > (TYPA) 0 ) { ++error; } printf("%s(a-b)<%s0 is %x\n", sTYPA, sTYPA, ( (TYPA)(a-b) < (TYPA) 0 ) ); if( (TYPA) ( a - b ) > (TYPA) z ) { ++error; } printf( "%s(a-b)<%sz = %x\n", sTYPA, sTYPA, ( (TYPA)(a-b) < (TYPA)z ) ); if( (TYPA) ( a -= b ) > (TYPA) 0 ) { ++error; } a = VAL1; printf( "%s(a-=b)<%s0 = %x\n", sTYPA, sTYPA, ( (TYPA)(a-=b) < (TYPA)0 ) ); if( (TYPA) ( a -= b ) > (TYPA) z ) { ++error; } a = VAL1; printf( "%s(a-=b)<%sz = %x\n", sTYPA, sTYPA, ( (TYPA)(a-=b) < (TYPA)z ) ); if( (TYPA) ( c = a - b ) > (TYPA) 0 ) { ++error; } printf("%s(c=a-b)<%s0 is %x\n", sTYPA, sTYPA, ( (TYPA)(c=a-b) < (TYPA) 0 ) ); if( (TYPA) ( c = a - b ) > (TYPA) z ) { ++error; } printf("%s(c=a-b)<%sz is %x\n", sTYPA, sTYPA, ( (TYPA)(c=a-b) < (TYPA) z ) ); } { struct usl { int sum : 3; int dummy : 23; int unbits : 3; unsigned tib932 : 3; unsigned tiba32 : 3; } pgm; #define a pgm.tib932 #define b pgm.tiba32 #define c pgm.sum #define z pgm.unbits #define TYPE int /* char, short, int, long */ #define sTYPE "(3bits)" /* char, short, int, long */ a = 0x5; b = 0x3; z ^= z; if( (TYPE) a > (TYPE) b ) { ++error; } printf("%stib932<%stiba32 is %x\n", sTYPE, sTYPE, ( (TYPE) a < (TYPE) b ) ); if( (TYPE) ( a - b ) > (TYPE) 0 ) { ++error; } printf("%s(tib932-tiba32)<%s0 is %x\n", sTYPE, sTYPE, ( (TYPE)(a-b) < (TYPE) 0 ) ); if( (TYPE) ( a - b ) > (TYPE) z ) { ++error; } printf( "%s(tib932-tiba32)<%spgm.unbits = %x\n", sTYPE, sTYPE, ( (TYPE)(a-b) < (TYPE)z ) ); if( (TYPE) ( a -= b ) > (TYPE) 0 ) { ++error; } a = 0x5; printf( "%s(tib932-=tiba32)<%s0 = %x\n", sTYPE, sTYPE, ( (TYPE)(a-=b) < (TYPE)0 ) ); if( (TYPE) ( a -= b ) > (TYPE) z ) { ++error; } a = 0x5; printf( "%s(tib932-=tiba32)<%spgm.unbits = %x\n", sTYPE, sTYPE, ( (TYPE)(a-=b) < (TYPE)z ) ); if( (TYPE) ( c = a - b ) > (TYPE) 0 ) { ++error; } printf("%s(pgm.sum=tib932-tiba32)<%s0 is %x\n", sTYPE, sTYPE, ( (TYPE)(c=a-b) < (TYPE) 0 ) ); if( (TYPE) ( c = a - b ) > (TYPE) z ) { ++error; } printf("%s(pgm.sum=tib932-tiba32)<%spgm.unbits is %x\n", sTYPE, sTYPE, ( (TYPD)(c=a-b) < (TYPD) z ) ); } exit( error ); } -- = Drug tests; just say *NO*! = Fred Fish Motorola Computer Division, 3013 S 52nd St, Tempe, Az 85282 USA = seismo!noao!mcdsun!fnf (602) 438-3614