How about exploiting that two numbers have the same sign when their product is positive?
sub same_sign { $_[0]*$_[1] > 0 };
The above version will treat 0 as a singularity which never has the same sign with anything, not even with itself. If you don't want that, you can make 0 have the same sign as a positive and a negative number:
sub same_sign { $_[0]*$_[1] >= 0 };
... but you can't say "0 is always positive" or "0 always is negative" with my method.
Update: After thinking about it a bit, I think the <=>-trick is the following:
sub same_sign { ($_[0] <=> 0) == ($_[1] <=> 0) };
because shift <=> 0 is the sign function. It has different properties again - 0 has no sign, and compares as the same sign as 0, but it's different from any positive and any negative number.
In reply to Re: Seeing if two numbers have the same sign
by Corion
in thread Seeing if two numbers have the same sign
by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |