function SGN($test) {
if ($test > 0) {
return "+1";
}
if ($test < 0) {
return "-1";
}
if ($test === 0) {
return "0";
}
}
Just off the top of my head, I may have made a silly mistake.
in QBASIC there is an operator that will return -1 for negative values, +1 for positive, and 0 for zero values. It was called something like SGN() , so that SGN(-1) returns -1 etc. is there a corresponding built in function/operator in PHP? I cant seem to find one in any of the books i have. thanks...
in QBASIC there is an operator that will return -1 for negative values, +1
for positive, and 0 for zero values. It was called something like SGN() ,
so that SGN(-1) returns -1 etc.
is there a corresponding built in function/operator in PHP? I cant seem to
find one in any of the books i have.
thanks
function SGN($test) {
if ($test > 0) {
return "+1";
}
if ($test < 0) {
return "-1";
}
if ($test === 0) {
return "0";
}
}
Just off the top of my head, I may have made a silly mistake.
On Thu, 15 Apr 2004 13:51:37 GMT, "Matt Foster"
<mattfosterREMOVEblueyonder.co.uk> wrote:
You can avoid 1 if case by assuming one option at the start.>function SGN($test) {
> if ($test > 0) {
> return "+1";
> }
> if ($test < 0) {
> return "-1";
> }
> if ($test === 0) {
> return "0";
> }
>}
>
>Just off the top of my head, I may have made a silly mistake.
result = 0;
if var > 0 result = + elsif var < 0 result = -
shorter :-)
>>Just off the top of my head, I may have made a silly mistake.Or maybe even:>
> You can avoid 1 if case by assuming one option at the start.
>
> result = 0;
>
> if var > 0 result = + elsif var < 0 result = -
>
> shorter :-)
>
if ($test==0) { return 0; } else { return $test/(abs($test); };
what if someone does
test("cheese")
?
I mean, I don't know because I'm new to PHP ;)
"Joost" <joostdekrakerremovethis.gmx.net> wrote in message
news:c5m4kp$7q5$1info.service.rug.nl...>> >> >>Just off the top of my head, I may have made a silly mistake.
> > You can avoid 1 if case by assuming one option at the start.
> >
> > result = 0;
> >
> > if var > 0 result = + elsif var < 0 result = -
> >
> > shorter :-)
> >
> Or maybe even:
>
> if ($test==0) { return 0; } else { return $test/(abs($test); };
>
>
>
On Thu, 15 Apr 2004 16:13:50 +0200, Joost
<joostdekrakerremovethis.gmx.net> wrote:
if (!$test) { return 0; } works also ;-)>Or maybe even:
>
>if ($test==0) { return 0; } else { return $test/(abs($test); };
Joost wrote on 15 apr 2004 in alt.comp.lang.php:
no else necessary:>>>>>>Just off the top of my head, I may have made a silly mistake.
>> You can avoid 1 if case by assuming one option at the start.
>>
>> result = 0;
>>
>> if var > 0 result = + elsif var < 0 result = -
>>
>> shorter :-)
>>
> Or maybe even:
>
> if ($test==0) { return 0; } else { return $test/(abs($test); };
if ($test==0) { return 0; };
return $test/(abs($test);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
I like it :-)
Hello, Jeremy!
You wrote on Thu, 15 Apr 2004 14:41:14 +0100:
JW> in QBASIC there is an operator that will return -1 for negative
JW> values, +1 for positive, and 0 for zero values. It was called
JW> something like SGN() , so that SGN(-1) returns -1 etc.
JW> is there a corresponding built in function/operator in PHP? I cant
JW> seem to find one in any of the books i have.
JW> thanks
Here is a mathematical way of doing that:
if ($x==0)
return 0;
else
return ($x/(abs($x)));
With best regards, Nurchi BECHED.
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it s away your whole leg."
--Bjarne Stroustrup
Nurchi BECHED wrote on 18 apr 2004 in alt.comp.lang.php:
There is no use for the "else",> Hello, Jeremy!
> You wrote on Thu, 15 Apr 2004 14:41:14 +0100:
>
> JW> in QBASIC there is an operator that will return -1 for negative
> JW> values, +1 for positive, and 0 for zero values. It was called
> JW> something like SGN() , so that SGN(-1) returns -1 etc.
>
> JW> is there a corresponding built in function/operator in PHP? I cant
> JW> seem to find one in any of the books i have.
>
> JW> thanks
>
> Here is a mathematical way of doing that:
> if ($x==0)
> return 0;
> else
> return ($x/(abs($x)));
>
since in the 0 case the function is already ended
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Hello, Jeremy!
You wrote on Thu, 15 Apr 2004 14:41:14 +0100:
JW> in QBASIC there is an operator that will return -1 for negative
JW> values, +1 for positive, and 0 for zero values. It was called
JW> something like SGN() , so that SGN(-1) returns -1 etc.
JW> is there a corresponding built in function/operator in PHP? I cant
JW> seem to find one in any of the books i have.
JW> thanks
I've actually thought about his:
($x==0) ? return 0 : return ($x/(abs($x)));
Maybe you don't need brackets before '?', but I usually put them.
(I'm not sure if semicolon is required before colon after 'return 0')
This kind of defeats "no else" thing, but looks much shorter and nicer
With best regards, Nurchi BECHED.
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it s away your whole leg."
--Bjarne Stroustrup
Hello, Nurchi!
You wrote to Jeremy Watts on Sun, 18 Apr 2004 04:45:32 GMT:
NB> Hello, Jeremy!
NB> You wrote on Thu, 15 Apr 2004 14:41:14 +0100:
JW>> in QBASIC there is an operator that will return -1 for negative
JW>> values, +1 for positive, and 0 for zero values. It was called
JW>> something like SGN() , so that SGN(-1) returns -1 etc.
JW>> is there a corresponding built in function/operator in PHP? I cant
JW>> seem to find one in any of the books i have.
JW>> thanks
NB> Here is a mathematical way of doing that:
NB> if ($x==0)
NB> return 0;
NB> else return ($x/(abs($x)));
NB> With best regards, Nurchi BECHED.
NB> P.S.
NB> C makes it easy to shoot yourself in the foot;
NB> C++ makes it harder, but when you do, it s away your whole leg."
NB> --Bjarne Stroustrup
With best regards, Nurchi BECHED.
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it s away your whole leg."
--Bjarne Stroustrup
function sign($x)
{
return $x?$x>0?1:-1:0;
}
"Jeremy Watts" <jeremy.watts70ntlworld.com> wrote in message
news:liwfc.49$mN3.34newsfe1-win...to> in QBASIC there is an operator that will return -1 for negative values, +1
> for positive, and 0 for zero values. It was called something like SGN() ,
> so that SGN(-1) returns -1 etc.
>
> is there a corresponding built in function/operator in PHP? I cant seem> find one in any of the books i have.
>
> thanks
>
>
On Mon, 19 Apr 2004 00:44:58 GMT
"Nurchi BECHED" <nurchitelus.net> wrote:
> Hello, Nurchi!
> You wrote to Jeremy Watts on Sun, 18 Apr 2004 04:45:32 GMT:
>
> NB> Hello, Jeremy!
> NB> You wrote on Thu, 15 Apr 2004 14:41:14 +0100:
>
> JW>> in QBASIC there is an operator that will return -1 for negative
> JW>> values, +1 for positive, and 0 for zero values. It was called
> JW>> something like SGN() , so that SGN(-1) returns -1 etc.
>
> JW>> is there a corresponding built in function/operator in PHP? I
> JW>cant> seem to find one in any of the books i have.
>
> JW>> thanks
>
> NB> Here is a mathematical way of doing that:
> NB> if ($x==0)
> NB> return 0;
> NB> else return ($x/(abs($x)));
>
> NB> With best regards, Nurchi BECHED.
>
> NB> P.S.
> NB> C makes it easy to shoot yourself in the foot;
> NB> C++ makes it harder, but when you do, it s away your whole
> NB> leg."--Bjarne Stroustrup
>
>
> With best regards, Nurchi BECHED.
>
> P.S.
> C makes it easy to shoot yourself in the foot;
> C++ makes it harder, but when you do,
> it s away your whole leg."
> --Bjarne Stroustrup
>
>
--
Quispiam Power Computing | "There are two major products that come out
Pendle Hill, Australia | of Berkeley: LSD and UNIX. We don't believe
+61 2 9631 7719 | this to be a coincidence. "
[url]www.quispiam.com[/url] | - Jeremy S. Anderson
Hello, Allan!
You wrote on Mon, 19 Apr 2004 04:25:51 GMT:
AA> function sign($x)
AA> {
AA> return $x?$x>0?1:-1:0;
AA> }
Yeah, man!
This is the best solution!!! :):):)
With best regards, Nurchi BECHED.
P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it s away your whole leg."
--Bjarne Stroustrup
Bookmarks