[Python-ideas] PEP 485: A Function for testing approximate equality
chris.barker at noaa.gov
Mon Jan 26 06:43:27 CET 2015
On Sun, Jan 25, 2015 at 5:39 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
> On Jan 25, 2015, at 17:21, Chris Barker <chris.barker at noaa.gov> wrote:
> Unfortunately, I don't think we can change assertAlmostEqual.
> Agreed -- would have thought that was off the table.
> If assertAlmostEqual is primarily intended for float comparisons,
See Nathanials note -- but I think the key thing is that assertAlmostEqual
is NOT really intended for float comparisons -- rather, it's intended for
decimal numbers that happen to be stored in floats (does it pre-date the
In practical use, most of us are used to thinking in terms of decimal
numbers, it's what we all learn in grade school. An in practice, Python,
and every other language I've seen, takes decimal literals for floats, and
displays floats as decimals. Also in practical use, using floats for
decimal numbers works just fine, particularly if you round on output.
So I that's the use-case for assertAlmostEqual -- it provides an easy way
to check if numbers are almost equal to N decimal places. I guess that's a
pretty common use case. So maybe folks would want to keep it around.
(by the way -- does it work for Decimal objects -- it really should, it's
more appropriate for them anyway!)
On the other hand, those of us trained in science and engineering are used
to working with scientific or engineering notation -- i.e. x.xxxEn (some
number times ten raised to a power)-- a mantissa and an exponent. While we
still decimal, it's a better match for floating point numbers.
We are also trained to consider "significant figures" -- or the number of
digits of accuracy of a value -- measured or computed.
has the same number of significant figures as
But they are not of the same value at all.
Anyway, a relative comparison provides something akin to a comparison to a
certain number of significant figures -- really common and useful, and
familiar to at least those trained in most disciplines of science or
Key here is that I'm not suggesting a better or more correct
assertAlmostEqual. I'm suggesting something different (arguably more
I'm honestly not sure how useful assertAlmostEqual is -- but apparently is
was useful enough to put in the first place ,and to get used since then.
> That would imply that assertCloseTo should include the same sequence
> behavior as assertAlmostEqual, not just assertTrue(close_to), so it really
> is a drop-in replacement for most users.
even if it isn't a replacemen -- that sequence behavior is nice -- and
Steven has already written it as well.
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas