Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Seeing if two numbers have the same sign

by demerphq (Chancellor)
on Jan 10, 2008 at 18:55 UTC ( #661703=note: print w/ replies, xml ) Need Help??

in reply to Seeing if two numbers have the same sign

You are doing more work than you need to. Remember that mathematical comparisons return booleans, and booleans can be directly compared.

if ($x<0 == $y<0) { print "$x and $y have the same sign"; }

(($x ^ $y) < 0) would probably also work assuming they are integers. I think the trick with abs or the comparison operator would probably actually be less efficient than doing a comparison. Using multiplication is going to be slow as well.

BTW the sign of zero isnt a theological debate, it is an implementation detail of the machine you are working on and the data types you are using. :-)


Comment on Re: Seeing if two numbers have the same sign
Download Code
Re^2: Seeing if two numbers have the same sign
by hipowls (Curate) on Jan 10, 2008 at 21:22 UTC

    Neat (wish I'd thought of it) but it implicitly assumes that zero is a positive number, given 0 and -1 the test returns 0, given 0 and +1 it returns 1. If the test was ( $x<=0 == $y<=0 ) then zero is treated as both positive and negative and the two previous examples return 1. Which is consistent but might not be what you want.

      Maybe I'm just old-fashioned (primitive?), but my understanding of the term "same sign", at least as it relates to programming, has always been: when the sign bit of two values is the same, those values have "the same sign". In that interpretation, zero is always included among the set of "non-negative" values: it is the same sign as "1", and not the same sign as "-1".
Re^2: Seeing if two numbers have the same sign
by WoodyWeaver (Scribe) on Jan 10, 2008 at 23:18 UTC
    I don't understand your (($x^$y)<0).


    sub andNeg { return (($_[0]^$_[1]) < 0); }
    and test harness
    print "<table border>\n<tr><td>", join("</td><td>", "x", "y", "andNeg( +)", "spaceship()", "mult()", "anchor()"), "</td></tr>\n"; for (0..3) { my $x = 1 - 2 * ($_ % 2); my $y = 1 - 2 * int($_ / 2); print "<tr><td>", join("</td><td>", $x, $y, andNeg($x, $y), spaceshi +p($x, $y), mult($x, $y), anchor($x, $y)), "</td></tr>\n"; } print "</table>\n";
    I get
    As for spaceship, multiplication, and logic, implemented as
    sub spaceship { (0 <=> $_[0]) == (0<=> $_[1]); } sub mult { $_[0]*$_[1] > 0; } sub anchor { if ($_[0] > 0) { if ($_[1] > 0) { # both pos return 1 } } elsif ($_[0] < 0) { if ($_[1] < 0) { # both neg return 1 } } return 0; }
    I get
    unit squarespaceshipmultiplylogic
    spaceship--------worse 22.28%worse 10.40%
    multiply better 22.28%--------better 13.26%
    logic better 10.40%worse 13.26%--------
    signed intspaceshipmultiplylogic
    spaceship--------worse 21.43%worse 7.65%
    multiply better 21.43%--------better 14.92%
    logic better 7.65%worse 14.92%--------
    that is, if you take points (x,y) uniformly chosen from the unit disk [-1,1]x[-1,1], then spaceship is 22% worse than testing multiplication, multiplication is 13% better than using logic, etc; and if you take them as random unsigned ints, multiplication is (surprisingly!) even better.

    full test program (don't use this use Benchmark; instead)

      Hmm. I didnt actually test it and had assumed that the result of binary xor was going to be signed, but of course its unsigned, hence the xor ideas doesnt work as written. My bad. Sorry. Id just use the $x<0 == $y<0 approach anyway. :-)


Re^2: Seeing if two numbers have the same sign
by girarde (Friar) on Jan 11, 2008 at 18:00 UTC
    I like your method better than mine (snarls enviously).

    Mathematically, the sign of zero is undefined. Just as in football, you can't say which side of center the center lines up on.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://661703]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-07-28 10:26 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (195 votes), past polls