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
In reply to Re: Seeing if two numbers have the same sign ($x / abs $x)
by lodin
in thread Seeing if two numbers have the same sign
by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |