Showing content from http://mail.python.org/pipermail/python-dev/attachments/20100328/0581bad0/attachment.html below:
A good summary on floating point comparison algorithms can be found at <div><br></div><div><a href="http://www.boost.org/doc/libs/1_34_0/libs/test/doc/components/test_tools/floating_point_comparison.html">http://www.boost.org/doc/libs/1_34_0/libs/test/doc/components/test_tools/floating_point_comparison.html</a><br>
<br></div><div>I, too, redefine almostEquals so that the comparison is more robust</div><div><br></div><div>+1</div><div><br><div class="gmail_quote">On Sun, Mar 28, 2010 at 2:06 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Sun, 28 Mar 2010 02:54:19 pm Charles McCreary wrote:<br>
> Perhaps not just absolute but relative tolerance, e.g.:<br>
><br>
> def isclose(x, y, rtol=1.e-5, atol=1.e-8):<br>
> return abs(x-y) <= atol + rtol * abs(y)<br>
<br>
</div>I'm not sure why you add the tolerances like that. Surely a more<br>
appropriate approach is to compare the values against the tolerances<br>
individually, and return True if they meet either condition.<br>
<br>
For what it's worth, here's a recipe I wrote for approximately equal,<br>
following someone's complaint on comp.lang.python that Python has no<br>
approximately-equal operator.<br>
<br>
<a href="http://code.activestate.com/recipes/577124-approximately-equal/" target="_blank">http://code.activestate.com/recipes/577124-approximately-equal/</a><br>
<br>
Comments and criticism welcome.<br>
<br>
I've never found unittest.TestCase.assertAlmostEqual to be useful. I<br>
generally write my own comparison function, then call:<br>
<br>
assert_(compare(x, y))<br>
<br>
as needed. I'm +0.5 on Michael's suggested change -- it will make<br>
assertAlmostEqual marginally more useful, which is a plus, but I'm not<br>
sure that absolute tolerances are better than relative. But how general<br>
do we want to be? "Almost equal" means something different to everyone.<br>
<div class="im"><br>
<br>
<br>
> On Fri, Mar 26, 2010 at 7:59 PM, Michael Foord<br>
<<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>>wrote:<br>
> > Hello all,<br>
> ><br>
> > A user has suggested an optional argument to<br>
> > unittest.TestCase.assertAlmostEqual for specifying a maximum<br>
> > difference between the expected and actual values, instead of using<br>
> > rounding.<br>
</div>[snip]<br>
<br>
<br>
<br>
--<br>
<font color="#888888">Steven D'Aprano<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/charles.r.mccreary%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/charles.r.mccreary%40gmail.com</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Charles McCreary P.E.<br>CRM Engineering<br>903.643.3490 - office<br>903.224.5701 - mobile/GV<br>
</div>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4