TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KURT KUZBA
from: SUNIR SHAH
date: 1997-07-20 03:13:00
subject: Signs of numbers

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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.