# aproximates dezimal multiplication with integer multiplication and leftshifts

my $val = shift;
if( ($val +1 -1) <= 0 )
{
  die 'No valid number given. Please give a positive dezimal number.'."\n";
}
print 'Find approximations for x*'.$val."\n";

my $minLowdev = $val-int($val);
my $minUpdev =  $val-int($val);
my $firstFound = 0;
my $searchRange  = 5;
for(my $nennerShift=1; $nennerShift<32; ++$nennerShift)
{
  for(my $zaehler = int( (2**$nennerShift)*$val)-$searchRange; 
         $zaehler < int( (2**$nennerShift)*$val)+$searchRange;
       ++$zaehler)
  {
      my $res = ($zaehler/(2**$nennerShift));
      if(  ($firstFound==0) and ($res>$val) )
      { 
        --$zaehler;
        $firstFound = 1;
      }

      if( $firstFound >= 0)
      {
        my $dev = abs($res-$val);
        my $show = 0;
        
        if( ($dev < $minUpdev) and ($res>=$val))
        {
          $minUpdev = $dev;
          $show = 1
        }
        if( ($dev < $minLowdev) and ($res<=$val))
        {
          $minLowdev = $dev;
          $show = 1
        } 
        if( $show== 1 )
        {
            print '( x * '.$zaehler.' ) >> '.$nennerShift.' = x * '.$res."\n";
        }
      }
      if($res>$val)
      {
        last;
      }
  }  
}