title: java integer rounding (division related)
body: <p>I have run into a surprise with integer division <em>not</em> rounding down as expected.</p>
  
  <p>Simple code:</p>
  
  <pre><code>public class HelloMath {
  
      public static void main(String[] args) {
          for (int s=1; s&lt; 20; s++)
          {
              int div = 1&lt;&lt;s;
              int res = (int) ((float)-8/ s);
              System.out.printf("Bit %d, result %d\n", s, res);
          }
      }
  
  }
  </code></pre>
  
  <p>Even with the explicit (float) casts, the output is:</p>
  
  <pre><code>Bit 1, result -8
  Bit 2, result -4
  Bit 3, result -2
  Bit 4, result -2
  Bit 5, result -1
  Bit 6, result -1
  Bit 7, result -1
  Bit 8, result -1
  Bit 9, result 0
  Bit 10, result 0
  Bit 11, result 0
  Bit 12, result 0
  Bit 13, result 0
  Bit 14, result 0
  Bit 15, result 0
  Bit 16, result 0
  Bit 17, result 0
  Bit 18, result 0
  Bit 19, result 0
  </code></pre>
  
  <p>I was expecting -1 all the way down.</p>
  
  <p>The real code where this is happening does this:</p>
  
  <pre><code>public static int fluidTo8th(int fluid)
  {
      if (0 == fluid)
          return 0;   // Technically, this isn't needed :-).
      int wholePart = (fluid-1) * 8 / RealisticFluids.MAX_FLUID; // -1 gets rounding correct;
      // consider fluid of exactly 1/8th.
      return 1+wholePart;
  }
  </code></pre>
  
  <p>RealisticFluids.MAX_FLUID has the value (1&lt;&lt;20).
  The code is supposed to take an input that is 1 to MAX_FLUID (0 should only happen if the input block is air), and return a number from 0 to 7 -- 1 to 1/8th max is 0, 1/8th +1 to 2/8th is 2, up to 7/8th + 1 to 8/8th is 7.</p>
  
  <p>I'm expecting integer math to round all fractions down. But that's not happening -- I wind up with off-by-one errors all over the place as 5.999 winds up becoming 6 instead of 5.</p>
  
  <ol>
  <li>Where is this behavior documented?</li>
  <li>What's the easiest work-around to get the rounding down that I expected?</li>
  </ol>
  
  <p>(full code context: <a href="https://github.com/keybounce/Finite-Fluids/blob/master/src/main/java/com/mcfht/realisticfluids/Util.java" rel="nofollow">https://github.com/keybounce/Finite-Fluids/blob/master/src/main/java/com/mcfht/realisticfluids/Util.java</a>)</p>