TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KURT J. TISCHER
from: KURT KUZBA
date: 1997-07-12 13:24:00
subject: Signs of numbers

KJ>   a function to determine the sign of a number which would
KJ>   return 1 if positive, 0 if zero, and -1 if negative...?
   This isn't too difficult if you look at the question.
how do i
if(x > 0) return 1;
if(x == 0) return 0;
if(x < 0) return -1;
   Now you only need to write the code.
int sgn(int x)
{
   return x ? (x > 0) ? 1 : -1 : 0;
}
   It might even be better as a macro.
#define sgn(x) (x ? (x > 0) ? 1 : -1 : 0)
   Come to think of it, this is so elementary in C++ or C that
   you can easily perform the calculation inline with normal
   code and be perfectly happy with the results.
   Adding a single line of code to your program wherever you
   would add a function call is a trivial gain in code size
   and a substantial gain in code speed with a bit of code this
   tiny. And since it IS inline, you don't need to worry about
   the variable type, only the type of the result, which may be
   explicitly cast to int easily enough.
   That would explain why there isn't a sgn() function, even
   though Microsoft knows it is useful and could have added one.
int sgn = int(x ? (x > 0) ? 1 : -1 : 0);
   Not really too much of a strain on the bytes budget. :)
> ] I will not tell my History Teacher to, "Get over it"........
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

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