Newer
Older
labs / tiddlers / content / labs / lab02 / _Labs_02_Testing Floating Point Numbers.md

We are using double precision (64-bit) floating point numbers here. You should remember from COMP 101 that floating point numbers are not entirely precise (even the so-called double precision ones). We have been lucky so far, but let's see what happens if we get unlucky.

We will use the classic 0.1 and 0.2 values that show up the imprecision of 64-bit floating point numbers very nicely.

  1. Change the sale price in the setUp method to 0.1, and the quantity purchased to 0.2.

  2. The result of 0.1 * 0.2 is 0.02 in base 10. Modify the testGetItemTotal test to use 0.02 in the is method.

  3. Run the test.

    As expected, the evil 0.1 and 0.2 values have struck again. You can expand the arrows beside the test methods in the <

    > pane to see the explanation for the test failure:

    Expected: is <0.02>
      but: was <0.020000000000000004>
  4. How do we deal with this? We give the test some wiggle room to deal with precision errors.

    Modify the assertThat statement to:

    assertThat(result, is(closeTo(0.02, 0.0001)));

    The 0.0001 is how much error we are willing to accept.

    The value that you use for the error is dependent on your application --- if accumulated approxcimation error is something that you are worried about then you might choose a very small number here to ensure that approximation errors are being kept to a minimum.

  5. Fix the imports again to deal with the closeTo method.

  6. Run the test again. We should be all good again.