From a5bb695c24070c4c72e462befbd2e9bf30c69ad0 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 24 Apr 2013 21:47:19 +0000 Subject: [PATCH] [Test] Use relative and absolute tolerances for comparing files Use the same tolerances for comparing text output files as is used for CSV files. The absolute tolerance is the maximum (less restrictive) of the specified tolerance and the tolerance implied by the recorded precision. --- site_scons/buildutils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/site_scons/buildutils.py b/site_scons/buildutils.py index 0141e3ec8..8cb9bf5df 100644 --- a/site_scons/buildutils.py +++ b/site_scons/buildutils.py @@ -190,6 +190,9 @@ def compareTextFiles(env, file1, file2): if not diff: return 0 + atol = env['test_csv_threshold'] + rtol = env['test_csv_tolerance'] + # Replace nearly-equal floating point numbers with exactly equivalent # representations to avoid confusing difflib reFloat = re.compile(r'(\s*)([+-]{0,1}\d+\.{0,1}\d*[eE]{0,1}[+-]{0,1}\d*)') @@ -220,7 +223,10 @@ def compareTextFiles(env, file1, file2): delta = max(getPrecision(floats1[j][1]), getPrecision(floats2[j][1])) num1 = float(floats1[j][1]) num2 = float(floats2[j][1]) - if not num1 - 1.1*delta < num2 < num1 + 1.1*delta: + abserr = abs(num1-num2) + relerr = abserr / (0.5 * abs(num1 + num2) + atol) + if abserr > (1.1*delta + atol) and relerr > rtol: + print 'Values differ: {: 14g} {: 14g}; rel. err = {:.3e}; abs. err = {:.3e}'.format(num1, num2, relerr, abserr) allMatch = False break