TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: PETER HAYWOOD
from: JAVIER KOHEN
date: 1997-08-21 18:40:00
subject: Re: Signs of numbers

On 09-Aug-97, Peter Haywood wrote to Kurt J. Tischer about Re: Signs of 
numbers.
 PH> #define Sgn(x) (x  0 ? 1 : 0)
You shouldn't be using macros in C++, try inline functions instead. But if
you insist with macros, put () around the arguments, ie:
#define Sgn2(x) ((x)  0 ? 1 : 0)
because:
Sgn(a == 0);
will expand to
(a == 0  0 ? 1 : 0)
that's equivalent to:
(a == (0  0) ? 1 : 0)      /* Wrong */
and not to
((a == 0)  0 ? 1 : 0)      /* Right */
as it is when using Sgn2().
BTW, the problem with macros is that you can't call Sgn() with ie
getchar(), because you'll finish getting two chars instead of one...
explanation is lefto to the reader as an exercise.
Javier Kohen  [The_Crusher] http://jkohen.base.org
... Es medio boludo þ Boludo enano
-!- CrusherTag 0.3.2.
--- Terminate 5.00 UnReg
---------------
* Origin: The King of The Ring (4:900/748.3)

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™.