in reply to Re: Compare two signed integers without using comparision operator in perl
in thread Compare two signed integers without using comparision operator in perl

What's wrong with abs?
print abs($a-$b) - ($a-$b);
  • Comment on Re: Re: Compare two signed integers without using comparision operator in perl
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Compare two signed integers without using comparision operator in perl
by Util (Priest) on May 07, 2003 at 00:10 UTC

    abs($a-$b) - ($a-$b) equals 0 when $a>=$b, and equals 2*abs($a-$b) when $a<$b.

    Perhaps you meant to say abs($a-$b)/($a-$b). This would produce the same (-1,0,1) that <=> returns, but it needs special handling to avoid division by zero, e.g. the eval in tye's post.