Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The second question you ask concerns how large an <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#ulp%28double%29" rel="nofollow">ulp</a> is in Java.</p> <p>If the ulp exceeds <code>1/(n)</code>, then rounding the multiplication would not recover the original divided int. Typically, larger ulps are associated with larger double values. An ulp associated with a double starts to exceed 1 at around 9E15; if your recovered doubles were around there, then you might finding problems with round() not getting the expected answer. As you are working with int values, though, the largest value of the numerator of your division will be <code>Integer.MAX_VALUE</code>.</p> <p>The following program tests all the positive integer values of <code>n</code> to see which one causes the largest potential for rounding error when trying to recover the divided int:</p> <pre><code> public static void main(String[] args) { // start with large number int m = Integer.MAX_VALUE; double d = 0; double largestError = 0; int bigErrorCause = -1; for (int n = 1; n &lt; Integer.MAX_VALUE; n++) { d = (double) m / n; double possibleError = Math.ulp(d) * n; if (possibleError &gt; largestError) { largestError = possibleError; bigErrorCause = n; } } System.out.println("int " + bigErrorCause + " causes at most " + largestError + " error"); } </code></pre> <p>The output is:</p> <blockquote> <p>int 1073741823 causes at most 4.768371577590358E-7 error</p> </blockquote> <p>Rounding that using Math.round, then casting to int should recover the original int.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload