http://www.perlmonks.org?node_id=661623

grinder has asked for the wisdom of the Perl Monks concerning the following question:

I needed to see if two numbers have the same sign (both positive, or both negative), so I wrote the following:

#! /usr/local/bin/perl use strict; use warnings; use Test::More tests => 8; sub same_sign { my $x = shift; my $y = shift; return if $x > 0 and $y < 0 or $x < 0 and $y > 0; return 1; } ok(same_sign( 2, 4), '+ve +ve'); ok(same_sign(-2,-4), '-ve -ve'); ok(!same_sign( 2,-4), '+ve -ve'); ok(!same_sign(-2, 4), '-ve +ve'); ok(same_sign( 2, 0), '+ve 0'); ok(same_sign( 0, 4), '0 +ve'); ok(same_sign( -2, 0), '-ve 0'); ok(same_sign( 0, -4), '0 -ve');

I may be remembering things incorrectly, but I'm sure I learnt some simple trick at school involving abs or the spaceship operator, but I can't remember it. Is there another way do this? Is the code I've written clear, or could it be improved?

I'm not interested in the theological debates concerning the sign of zero, though :)

• another intruder with the mooring in the heart of the Perl