Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!rpi!crdgw1!sungod!davidsen From: davidsen@sungod.crd.ge.com (William Davidsen) Newsgroups: comp.lang.c Subject: Re: "arithmetic if":: Re: Feature for the next C version Message-ID: <1429@crdgw1.crd.ge.com> Date: 1 Aug 89 17:46:10 GMT References: <55480@tut.cis.ohio-state.edu> <1989Jul20.152935.14872@utzoo.uucp> <67@motto.UUCP> <18764@mimsy.UUCP> <1389@crdgw1.crd.ge.com> <8515@batcomputer.tn.cornell.edu> Sender: news@crdgw1.crd.ge.com Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: General Electric Corp. R&D, Schenectady, NY Lines: 75 Thanks to all the people who took the time to write about the lack of something filling the same function as the FORTRAN arithmetic IF. As someone pointed out, I'm not looking for arithmetic IF, but a way to do three separate things based on a value, usually the compare of two values in a sort or tree search. Some interesting ideas: ifcase (a < b) code; code; (a == b) more(code); (a > b) still(more); endcase; If I were going to make a really readable program, I could do this (and did, just to see how it looked and worked). #define LESS -1 #define EQUAL 0 #define GREATER 1 float T_tmp; #define COMPARE(a,b) ((T_tmp=a-b)==0 ? 0 : ((T_tmp>0)<<1)-1) For the person who said his FORTRAN didn't uses the flags set on a single test, that's poor compilation. I ran a program into the SunOS compiler (v3.5) and her's the program and partial output. m = 4 if (m) 1,2,3 1 print 5, m goto 4 2 print 10, m goto 4 3 print 15, m 4 stop 5 format(2H <, i5) 10 format(2h =, i5) 15 format(2H >, i5) end _______________ | | Partial generated code, comments are mine |________________ L11: movl #0x4,a5@(-0x8000) movl a5@(-0x8000),a6@(-0xc) tstl a6@(-0xc) ; one test jge L20 ; first jump jra L16 ; second jump L20: ; Fall into inline code tstl a6@(-0xc) jne L21 jra L17 L21: jra L18 L16: Again thanks for all the input, hopefully any new standard will come up with a reasonable way to write this, to (a) make it easy to read, (b) make it easy for compilers to generate good code (ie. recognize the special case). Obviously no one wants to have labels, but perhaps something like: n = (a-b $ -1 : 0 : 1); - or - return (a-ptr->val $ getv(ptr->prev : ptr->val : getv(ptr->next)); where there would be a "quadratic operator." No one could argue that this isn't in the spirit of the language, and an operator might be more easily added to the language than a control structure. bill davidsen (davidsen@crdos1.crd.GE.COM) {uunet | philabs}!crdgw1!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me