title: How to find the floor and remainder of a fraction in Java?
body: <p>Given integers 'a' and 'b', I would like a method that returns the floor and remainder of a / b such that:</p>
  
  <ul>
  <li>a / b = floor + remainder / |b| (where |b| is the absolute value of b), and</li>
  <li>0 &lt;= remainder &lt; |b|.</li>
  </ul>
  
  <p>For example, 5/3 = 1 + 2/3.</p>
  
  <p>Here's an attempt that works only for positive <code>a</code> and <code>b</code>:</p>
  
  <pre><code>public static long[] floorAndRemainder(long a, long b) {
    long floor = a / b;
    long remainder = a % b;
    return new long[] { floor, remainder };
  }
  </code></pre>
  
  <p>I need a function that works for all positive and negative numerators and denominators. For example,</p>
  
  <ul>
  <li><code>-5/3 = -2 + 1/3</code></li>
  <li><code>5/-3 = -2 + 1/3</code></li>
  <li><code>-5/-3 = 1 + 2/3</code></li>
  </ul>