[Python-Dev] Optional delta argument for assertAlmostEqual (original) (raw)
Charles McCreary charles.r.mccreary at gmail.com
Sun Mar 28 16:35:58 CEST 2010
- Previous message: [Python-Dev] Optional delta argument for assertAlmostEqual
- Next message: [Python-Dev] Optional delta argument for assertAlmostEqual
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
A good summary on floating point comparison algorithms can be found at
I, too, redefine almostEquals so that the comparison is more robust
+1
On Sun, Mar 28, 2010 at 2:06 AM, Steven D'Aprano <steve at pearwood.info>wrote:
On Sun, 28 Mar 2010 02:54:19 pm Charles McCreary wrote: > Perhaps not just absolute but relative tolerance, e.g.: > > def isclose(x, y, rtol=1.e-5, atol=1.e-8): > return abs(x-y) <= atol + rtol * abs(y)
I'm not sure why you add the tolerances like that. Surely a more appropriate approach is to compare the values against the tolerances individually, and return True if they meet either condition. For what it's worth, here's a recipe I wrote for approximately equal, following someone's complaint on comp.lang.python that Python has no approximately-equal operator. http://code.activestate.com/recipes/577124-approximately-equal/ Comments and criticism welcome. I've never found unittest.TestCase.assertAlmostEqual to be useful. I generally write my own comparison function, then call: assert(compare(x, y)) as needed. I'm +0.5 on Michael's suggested change -- it will make assertAlmostEqual marginally more useful, which is a plus, but I'm not sure that absolute tolerances are better than relative. But how general do we want to be? "Almost equal" means something different to everyone.
> On Fri, Mar 26, 2010 at 7:59 PM, Michael Foord <fuzzyman at voidspace.org.uk>wrote: > > Hello all, > > > > A user has suggested an optional argument to > > unittest.TestCase.assertAlmostEqual for specifying a maximum > > difference between the expected and actual values, instead of using > > rounding. [snip] -- Steven D'Aprano
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/charles.r.mccreary%40gmail.com
-- Charles McCreary P.E. CRM Engineering 903.643.3490 - office 903.224.5701 - mobile/GV -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20100328/0581bad0/attachment.html>
- Previous message: [Python-Dev] Optional delta argument for assertAlmostEqual
- Next message: [Python-Dev] Optional delta argument for assertAlmostEqual
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]