Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site terak.UUCP Path: utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!petrus!bellcore!decvax!genrad!panda!talcott!harvard!seismo!hao!noao!terak!doug From: doug@terak.UUCP (Doug Pardee) Newsgroups: net.lang.c Subject: Numeric comparisons Message-ID: <693@terak.UUCP> Date: Fri, 30-Aug-85 14:29:16 EDT Article-I.D.: terak.693 Posted: Fri Aug 30 14:29:16 1985 Date-Received: Mon, 2-Sep-85 03:25:18 EDT References: <10176@ucbvax.ARPA> <5118@mit-eddie.UUCP> Organization: Calcomp Display Products Division, Scottsdale, AZ, USA Lines: 24 > ... since comparison is merely done by subtraction (a > compare instruction is usually just a subtract instruction that doesn't > store the result anywhere)... Compiler writers note -- a comparison is not "just a subtract". For example: #define NBITS 16 /* NBITS is number of bits in an integer */ int a, b; a = 3 << (NBITS-3); /* 24576 for NBITS=16 */ b = -a; if (a>b) printf("Comparison was done by bit-wise comparison\n"); else printf("Comparison was done by subtraction\n"); /* WRONG */ Compiler users note -- if your compiler gives the wrong results, the compiler writer might not be completely at fault. Many early CPU chips (8080A, Z80, 6502, etc.) did comparison by subtraction, and a compiler would have had to generate extra code to test for Overflow in order to get the correct result. -- Doug Pardee -- CalComp -- {seismo!noao,decvax!noao,ihnp4}!terak!doug