Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Seeing if two numbers have the same sign ($x / abs $x)

by lodin (Hermit)
on Jan 11, 2008 at 11:48 UTC ( [id://661862]=note: print w/replies, xml ) Need Help??


in reply to Seeing if two numbers have the same sign

I sometimes use $x / abs $x to get the sign of $x (usually to get a coefficient to change the sign of something else). Unlike all other solutions above it has the added benefit of implicitly blowing up when $x is zero, which is a good thing for me as $x shouldn't be zero when I use this. So a (probably expensive) way of doing you subroutine is

sub same_sign { $_[0] / abs $_[0] == $_[1] / abs $_[1] }

Since the sign of zero usually is undefined, same_sign(0, 0) should usually blow up, even though $_[0] == $_[1]. Things with no sign can't have the same sign.

Of course, it all depends on what you need the signs for.

lodin

Replies are listed 'Best First'.
Re^2: Seeing if two numbers have the same sign ($x / abs $x)
by WoodyWeaver (Monk) on Jan 11, 2008 at 20:11 UTC
    <obsMathRef>
    lodin's observation and syphilis' cantrip come from the same underlying mathematical principles.

    Triangle equality:
    for vectors a, b,

    ||a+b|| <= ||a|| + ||b||, with equality iff a is a positive scalar multiple of b

    (Note here || is "norm", not or.)

    Angle between two vectors:
    The angle theta between two vectors a, b satisfies

    cos theta = (a dot b) / (||a|| ||b||)

    (That the second theorem implies the first is left as an exercise for the reader. :-)

    For one dimensional vectors ("numbers") the norm is the absolute value, and there are only two "directions" -- out along the positive axis and out along the negative axis. So syphilis is applying the triangle equality, while lodin is computing the cosine directly (note cos 0 = 1 -> "same sign" while cos 180 degrees = -1 -> "opposite sign").
    </obsMathRef>

    We now return to our regular interpretation of line noise as script.

    --woody

      lodin is computing the cosine directly (note cos 0 = 1 -> "same sign" while cos 180 degrees = -1 -> "opposite sign").

      Maybe this is what you mean, but I don't actually calculate the angle (or cosine) between $_[0] and $_[1]. I calculate the respective angles of $_[0] and $_[1] against the positive axis, and compare them.

      Equivalently, you could also view it as I'm taking the norm of the two 1D vectors $_[0] and $_[1], which gives me two unit vectors pointing in the direction of $_[0] and $_[1], which I then test for equality. If they're equal, they point in the same direction, and in 1D that means they have the same sign.

      lodin

        arg, eyes are getting old. You are absolutely right. I was thinking of the "trick" people had described about taking the product and checking if positive; you are indeed normalizing the vectors and checking for equality.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found