Re: Equivelent of BASIC SGN function

From: Date: Sun, 13 Aug 2000 19:44:09 +0000
Subject: Re: Equivelent of BASIC SGN function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11530@lists.php.net to get a copy of this message
Gary Landers writes: > I am recoding a Basic program into PHP > > the program uses a SNG function which acts like this > > SGN returns a value indicating the sign of a numeric expression (1 if the > expression is positive, 0 if it is zero, or -1 if it is negative). > > Example: > PRINT SGN(1), SGN(-1), SGN(0) > > Output is: > 1 -1 0 > > I cant find a like function in PHP and the only solution I come up with is > lengthy. > > > --- > Gary Landers How about this: <?php function SGN($val) { return $val == 0 ? 0 : ($val > 0 ? 1 : -1); } echo "Sign of 5: " . SGN(5) . "\n"; echo "Sign of 0: " . SGN(0) . "\n"; echo "Sign of -5: " . SGN(-5) . "\n"; ?> Cheers, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#11530) next »