From: Mike Jackson Date: Wed, 3 Apr 2013 09:52:15 +0000 (-0700) Subject: Changed precision example X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=09fd2185105eea26471bd26943b3bd9732f2f197;p=swc-testing-nose.git Changed precision example --- diff --git a/testing/RealWorld.md b/testing/RealWorld.md index ec0b228..99cbd58 100755 --- a/testing/RealWorld.md +++ b/testing/RealWorld.md @@ -12,20 +12,30 @@ This was the approach taken by EPCC and the Colon Cancer Genetics Group (CCGG) o The [Muon Ion Cooling Experiment](http://www.mice.iit.edu/) (MICE) have a large number of tests written in Python. They use [Jenkins](), a *continuous integration server* to build their code and trigger the running of the tests which are then [published online](https://micewww.pp.rl.ac.uk/tab/show/maus). -## When 1 + 1 = 2.000000000000001 +## When 1 + 1 = 2.0000001 -Computers don't do floating point arithmetic too well. This can make simple tests for the equality of two floating point values problematic due to imprecision in the values being compared. We can get round this by comparing to within a given threshold, or delta, for example we may consider *expected* and *actual* to be equal if *expected - actual < 0.000000000001*. +Computers don't do floating point arithmetic too well. This can make simple tests for the equality of two floating point values problematic due to imprecision in the values being compared. + + $ python + >>> expected = 1 + 1 + >>> actual = 2.0000001 + >>> assert expected == actual + +We can get round this by comparing to within a given threshold, or delta, for example we may consider *expected* and *actual* to be equal if *expected - actual < 0.000000000001*. Test frameworks such as `nose`, often provide functions to handle this for us. For example, to test that 2 numbers are equal when rounded to a given number of decimal places, $ python >>> from nose.tools import assert_almost_equal - >>> assert_almost_equal(1.000001, 1.000002, 0) - >>> assert_almost_equal(1.000001, 1.000002, 1) - >>> assert_almost_equal(1.000001, 1.000002, 3) - >>> assert_almost_equal(1.000001, 1.000002, 6) + >>> assert_almost_equal(expected, actual, 0) + >>> assert_almost_equal(expected, actual, 1) + >>> assert_almost_equal(expected, actual, 3) + >>> assert_almost_equal(expected, actual, 6) + >>> assert_almost_equal(expected, actual, 7) ... - AssertionError: 1.000001 != 1.000002 within 6 places + AssertionError: 2 != 2.0000000999999998 within 7 places + +What do we consider to be a suitable threshold for equality? That is application-specific - for some domains we might be happy to round to the nearest whole number, for others we may want to be far, far more accurate. ## When should we test?