in reply to Seeing if two numbers have the same sign

With re, just for fun ;)

sub same_sign { my $i=0; /-(?{$i=$i+1})/ for @_; $i=~/0|2/; }

PooLpi
  • Comment on Re: Seeing if two numbers have the same sign

Replies are listed 'Best First'.
Re^2: Seeing if two numbers have the same sign
by WoodyWeaver (Monk) on Jan 10, 2008 at 23:36 UTC
    I was thinking about that! Convert to a string, and look to see if the first char is a "-" to determine sign. I don't think your routine works, though.
    sub re { my $i=0; /-(?{$i=$i+1})/ for @_; $i=~/0|2/; }
    given the same test cases as before,
    xyxorNeg()spaceship()mult()anchor()re()
    111111
    -1101
    1-101
    -1-11111
    And actually, its only twice as slow...
    unit squarespaceshipmultiplylogicre
    spaceship--------worse 21.74%worse 12.21%better 86.26%
    multiplybetter 21.74%--------better 10.86%better 89.25%
    logicbetter 12.21%worse 10.86%--------better 87.94%
    reg expworse 86.26%worse 89.25%worse 87.94%--------
Re^2: Seeing if two numbers have the same sign
by poolpi (Hermit) on Jan 11, 2008 at 09:55 UTC
    Works under XP with Cygwin 1.5.24-2 but not under Linux 2.6.18-4-686 !

    This one works under Linux 2.6.18-4-686 with Perl v5.8.8 :
    #!/usr/bin/perl use strict; use warnings; sub same_sign { my $i=0; printf( "%2d %2d => ", $_[0], $_[1] ); s/-/$i=$i+1;/e for @_; $i!~/1/; } my $val = [ [ 1, 2 ], [ -1, 2 ], [ 1, -2 ], [ -1, -2 ] ]; for ( 0 .. $#{$val} ) { my ( $x, $y ) = ( $val->[$_][0], $val->[$_][1] ); print same_sign( $x, $y ) ? '' : 'not ', "same sign\n"; }
    Output:
    1 2 => same sign -1 2 => not same sign 1 -2 => not same sign -1 -2 => same sign


    PooLpi