Added 'assertNear' for comparing floats in Python unit tests

This method compares floats using combined absolute and relative tolerances,
instead of the "number of decimal digits" that TestCase.assertAlmostEqual uses.
This commit is contained in:
Ray Speth 2012-05-07 18:20:08 +00:00
parent 79a0165260
commit e2d2ba4e3d

View file

@ -1,4 +1,15 @@
import numpy as np
import unittest
class CanteraTest(unittest.TestCase):
def assertNear(self, a, b, rtol=1e-8, atol=1e-12, msg=None):
cmp = 2 * abs(a - b)/(abs(a) + abs(b) + atol)
if cmp > rtol:
message = ('AssertNear: %.14g - %.14g = %.14g\n' % (a, b, a-b) +
'Relative error of %10e exceeds rtol = %10e' % (cmp, rtol))
if msg:
message = msg + '\n' + message
self.fail(message)
def compareTimeSeries(reference, sample, rtol=1e-5, atol=1e-12):
"""