To: Kurt Kuzba
Subject: Signs of numbers
KK> int sgn(int x)
KK> {
KK> return x ? (x > 0) ? 1 : -1 : 0;
KK> }
KK> It might even be better as a macro.
KK> #define sgn(x) (x ? (x > 0) ? 1 : -1 : 0)
It's better as a function. You run the risk of changing x twice.. like
sign(x++);
x is incremented twice instead of once like you'd expect.
BTW, when writing macros, encapsulate all parameters in parentheses:
#define sgn(x) ((x) ? ((x) > 0) ? 1 : -1 : 0)
To avoid order of operation problems.
SS
--- Maximus 3.01
---------------
* Origin: BitByters BBS, Rockland ON, Can. (613)446-7773 v34, (1:163/215)
|