Professional Web Applications Themes

sign testing in php - PHP Development

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

  1. #1

    Default sign testing in php

    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


    Jeremy Watts Guest

  2. #2

    Default Re: sign testing in php

    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.


    Matt Foster Guest

  3. #3

    Default Re: sign testing in php

    On Thu, 15 Apr 2004 13:51:37 GMT, "Matt Foster"
    <mattfosterREMOVEblueyonder.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 :-)

    xeno Guest

  4. #4

    Default Re: sign testing in php

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



    Joost Guest

  5. #5

    Default Re: sign testing in php

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

    Matt Foster Guest

  6. #6

    Default Re: sign testing in php

    On Thu, 15 Apr 2004 16:13:50 +0200, Joost
    <joostdekrakerremovethis.gmx.net> wrote:
    >Or maybe even:
    >
    >if ($test==0) { return 0; } else { return $test/(abs($test); };
    if (!$test) { return 0; } works also ;-)
    xeno Guest

  7. #7

    Default Re: sign testing in php

    Joost wrote on 15 apr 2004 in alt.comp.lang.php:
    >>>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); };
    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)
    Evertjan. Guest

  8. #8

    Default Re: sign testing in php

    I like it :-)


    Matt Foster Guest

  9. #9

    Default Re: sign testing in 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)));

    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 Guest

  10. #10

    Default Re: sign testing in php

    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)
    Evertjan. Guest

  11. #11

    Default Re: sign testing in 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

    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


    Nurchi BECHED Guest

  12. #12

    Default Re: sign testing in php

    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


    Nurchi BECHED Guest

  13. #13

    Default Re: sign testing in php

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


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

    Allan Abrahamse Guest

  14. #14

    Default Re: sign testing in php

    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
    Matthew Vickers Guest

  15. #15

    Default Re: sign testing in php

    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


    Nurchi BECHED Guest

Similar Threads

  1. newsletter sign up
    By Alistair2802 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 0
    Last Post: November 19th, 12:31 PM
  2. No + (plus sign)
    By new be in forum Coldfusion - Getting Started
    Replies: 5
    Last Post: August 25th, 07:57 PM
  3. Sign In/Sign Out like macromedia page
    By ajohn30@adelphia.net in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: April 25th, 09:33 AM
  4. minus sign proportion to + sign in bold
    By Weintraub in forum Macromedia Freehand
    Replies: 7
    Last Post: August 2nd, 03:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139