Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote small test. It calcutates expected value with double precision. Then it calculates an error with your options. The first option is better:</p> <pre><code>Algorithm: FloatTest$1 option 1 error = 3.802792362162126 option 2 error = 4.333273185303996 Algorithm: FloatTest$2 option 1 error = 3.802792362167937 option 2 error = 4.333273185305868 </code></pre> <p>The Java code: </p> <pre><code>import org.junit.Test; public class FloatTest { @Test public void test() { testImpl(new ExpectedAlgorithm() { public double te(double cos_theta) { return Math.sqrt(1.0f - cos_theta * cos_theta); } }); testImpl(new ExpectedAlgorithm() { public double te(double cos_theta) { return Math.sqrt((1.0f + cos_theta) * (1.0f - cos_theta)); } }); } public void testImpl(ExpectedAlgorithm ea) { double delta1 = 0; double delta2 = 0; for (double cos_theta = -1; cos_theta &lt;= 1; cos_theta += 1e-8) { double[] delta = delta(cos_theta, ea); delta1 += delta[0]; delta2 += delta[1]; } System.out.println("Algorithm: " + ea.getClass().getName()); System.out.println("option 1 error = " + delta1); System.out.println("option 2 error = " + delta2); } private double[] delta(double cos_theta, ExpectedAlgorithm ea) { double expected = ea.te(cos_theta); double delta1 = Math.abs(expected - t1((float) cos_theta)); double delta2 = Math.abs(expected - t2((float) cos_theta)); return new double[]{delta1, delta2}; } private double t1(float cos_theta) { return Math.sqrt(1.0f - cos_theta * cos_theta); } private double t2(float cos_theta) { return Math.sqrt((1.0f + cos_theta) * (1.0f - cos_theta)); } interface ExpectedAlgorithm { double te(double cos_theta); } } </code></pre>
    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. 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.
 

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