Path: utzoo!attcan!utgpu!watmath!iuvax!mailrus!bbn!bbn.com!rsalz From: rsalz@bbn.com (Rich Salz) Newsgroups: comp.lang.c Subject: Re: Feature for the next C version Message-ID: <1913@prune.bbn.com> Date: 28 Jul 89 20:13:52 GMT References: <1389@crdgw1.crd.ge.com> Organization: BBN Systems and Technologies Corporation Lines: 41 In <1389@crdgw1.crd.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes: > This brings up the one feature of FORTRAN which is missing and would >be useful in C (and other structured languages)... the arithmetic IF. Ick. The impression I have is that most folks think arithmetic IF is a bad thing. I don't really know, and it doesn't matter too much. > When doing tree searches and many kinds of sorts, you often have logic >which looks something like this: > if (a == b) { > /* match found */ > } > else if (a < b) { > /* search lower portion of the tree */ > } > else { > /* search upper portion of the tree */ > } Easy enough. First, create a helper function: int sgn(x) int x; { return x ? (x < 0 : -1 : 1) : 0; } Then: switch(sgn(a - b)) { case -1: ... break; case 0: ... break; case 1: ... break; } -- Please send comp.sources.unix-related mail to rsalz@uunet.uu.net. Use a domain-based address or give alternate paths, or you may lose out.