Feature for the next C version

Rich Salz rsalz at bbn.com
Sat Jul 29 06:13:52 AEST 1989


In <1389 at crdgw1.crd.ge.com> davidsen at 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 at uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.



More information about the Comp.lang.c mailing list