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

$a = 3; $b = 4; print (sqrt(($a-$b)**2) - ($a-$b));
Reasoning:
A-B is positive or negative (assume disequal)

negative numbers, squared, and then rooted, disequal themselves
  • Comment on Re: Compare two signed integers without using comparision operator in perl
  • Download Code

Replies are listed 'Best First'.
Re: Re: Compare two signed integers without using comparision operator in perl
by bart (Canon) on May 06, 2003 at 19:18 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.