[Python-ideas] PEP 485: A Function for testing approximate equality

Nick Coghlan ncoghlan at gmail.com
Sat Jan 24 08:27:57 CET 2015


On 24 January 2015 at 07:30, Nathaniel Smith <njs at pobox.com> wrote:
> On Fri, Jan 23, 2015 at 12:40 AM, Chris Barker <chris.barker at noaa.gov> wrote:
>> Existing Implementations
>> ------------------------
>>
>> The standard library includes the
>> ``unittest.TestCase.assertAlmostEqual`` method, but it:
>>
>> * Is buried in the unittest.TestCase class
>>
>> * Is an assertion, so you can't use it as a general test (easily)
>>
>> * Uses number of decimal digits or an absolute delta, which are
>>   particular use cases that don't provide a general relative error.
>
> I might phrase this a bit more strongly -- assertAlmostEqual is
> confusing and broken-by-default for common cases like comparing two
> small values, or comparing two large values.

I would personally find the PEP more persuasive if it was framed in
terms of providing an improved definition of assertAlmostEqual that
better handles the limitations of binary floating point dynamic
ranges.

The fact that unittest.assertAlmostEqual is in the standard library
implies that any improvement to it must also be in the standard
library, and moving the definition of near equality that unittest uses
out to the math module so it is reusable in other contexts makes sense
to me, especially if it means being able to share the definition
between unittest and the statistics module.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list