For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > April 2004 > sign testing in php









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sign testing in php
Jeremy Watts

2004-04-15, 10:32 am

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


Matt Foster

2004-04-15, 10:32 am

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.


xeno

2004-04-15, 10:32 am

On Thu, 15 Apr 2004 13:51:37 GMT, "Matt Foster"
<mattfoster@REMOVEblueyonder.co.uk> wrote:

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


You can avoid 1 if case by assuming one option at the start.

result = 0;

if var > 0 result = + elsif var < 0 result = -

shorter :-)

Joost

2004-04-15, 11:39 am

>>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); };



Matt Foster

2004-04-15, 11:39 am

what if someone does

test("cheese")

?

I mean, I don't know because I'm new to PHP ;)



"Joost" <joostdekraker@removethis.gmx.net> wrote in message
news:c5m4kp$7q5$1@info.service.rug.nl...
>
> Or maybe even:
>
> if ($test==0) { return 0; } else { return $test/(abs($test); };
>
>
>



xeno

2004-04-15, 11:40 am

On Thu, 15 Apr 2004 16:13:50 +0200, Joost
<joostdekraker@removethis.gmx.net> wrote:

>Or maybe even:
>
>if ($test==0) { return 0; } else { return $test/(abs($test); };


if (!$test) { return 0; } works also ;-)
Evertjan.

2004-04-15, 3:34 pm

Joost wrote on 15 apr 2004 in alt.comp.lang.php:

>
> Or maybe even:
>
> if ($test==0) { return 0; } else { return $test/(abs($test); };


no else necessary:


if ($test==0) { return 0; };
return $test/(abs($test);


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Matt Foster

2004-04-15, 3:34 pm

I like it :-)


Nurchi BECHED

2004-04-18, 1:30 am

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 blows away your whole leg."
--Bjarne Stroustrup


Evertjan.

2004-04-18, 4:30 am

Nurchi BECHED wrote on 18 apr 2004 in alt.comp.lang.php:

> 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)));
>


There is no use for the "else",
since in the 0 case the function is already ended

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nurchi BECHED

2004-04-18, 9:32 pm

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 blows away your whole leg."
--Bjarne Stroustrup


Nurchi BECHED

2004-04-18, 9:32 pm

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 blows 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 blows away your whole leg."
--Bjarne Stroustrup


Allan Abrahamse

2004-04-19, 1:33 am

function sign($x)
{
return $x?$x>0?1:-1:0;
}


"Jeremy Watts" <jeremy.watts70@ntlworld.com> wrote in message
news:liwfc.49$mN3.34@newsfe1-win...
> 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
>
>



Matthew Vickers

2004-04-20, 6:33 am

On Mon, 19 Apr 2004 00:44:58 GMT
"Nurchi BECHED" <nurchi@telus.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 blows 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 blows 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. "
www.quispiam.com | - Jeremy S. Anderson
Nurchi BECHED

2004-04-21, 3:30 am

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 blows away your whole leg."
--Bjarne Stroustrup


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com