Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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
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
|
|