[Python-Dev] math.areclose ...?

Bob Ippolito bob at redivi.com
Sun Feb 5 20:46:25 CET 2006

On Feb 5, 2006, at 11:31 AM, Raymond Hettinger wrote:

> [Bob Ipppolito]
>> For those of us that already know what we're doing with floating   
>> point, areclose would be very convenient to have.
> Do you agree that the original proposed use (helping newbs ignore  
> floating
> point realities) is misguided and error-prone?

Maybe it's a bit misguided, but it's less error-prone than more naive  
comparisons.  It could delay the necessity for a newer programmer to  
lean all about floating point, but maybe most of those users don't  
really need to learn it.

Whether the function is there or not, this is really a documentation  
issue.  If the function is there then maybe it could highly suggest  
reading some "floating point in Python" guide that would describe the  
scenario, then lists common pitfalls with patterns that avoid those  

> Just curious, for your needs, do you want both absolute and  
> relative checks combined into the same function?

Having both makes it less likely that you'll need to tweak the  
constants, except of course if you're working with very small numbers  
such that the absolute tolerance is too big.  Of course, if you only  
want one or the other in a given case, you can always pass in 0  

For my needs, the proposed function and default tolerances would be  
better than the sloppy stuff that usually ends up in my tests.

>>  Especially for  unit testing.  I could definitely throw away a  
>> bunch of ugly code  that uses less correct arbitrary tolerance  
>> guesses if it were around.
> The unittest module already has assertAlmostEqual().  Does that
> method meet your needs or does it need to be improved in some way?

I generally write tests that don't run directly under the unittest  
framework, such as doctests or assert-based functions for nose or  
py.test.  The unittest module does not expose assertAlmostEqual as a  
function so it's of little use for me.


More information about the Python-Dev mailing list