title: How do you calculate div and mod of floating point numbers?
body: <p>In Perl, the <code>%</code> operator seems to assume integers. For instance:</p>
  
  <pre><code>sub foo {
      my $n1 = shift;
      my $n2 = shift;
      print "perl's mod=" . $n1 % $n2, "\n";
      my $res = $n1 / $n2;
      my $t = int($res);
      print "my div=$t", "\n";
      $res = $res - $t;
      $res = $res * $n2;
      print "my mod=" . $res . "\n\n";
  }   
  
  foo( 3044.952963, 7.1 );
  foo( 3044.952963, -7.1 );
  foo( -3044.952963, 7.1 );
  foo( -3044.952963, -7.1 );
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>perl's mod=6
  my div=428
  my mod=6.15296300000033
  
  perl's mod=-1
  my div=-428
  my mod=6.15296300000033
  
  perl's mod=1
  my div=-428
  my mod=-6.15296300000033
  
  perl's mod=-6
  my div=428
  my mod=-6.15296300000033
  </code></pre>
  
  <p>Now as you can see, I've come up with a "solution" already for calculating <code>div</code> and <code>mod</code>. However, what I don't understand is what effect the sign of each argument should have on the result. Wouldn't the <code>div</code> always be positive, being the number of times <code>n2</code> fits into <code>n1</code>? How's the arithmetic supposed to work in this situation?</p>