[Numpy-discussion] assert_equal(-0.0, 0.0) fails.

Warren Weckesser warren.weckesser at gmail.com
Mon Apr 14 07:09:11 EDT 2014


The test function numpy.testing.assert_equal fails when comparing -0.0 and 0.0:

In [16]: np.testing.assert_equal(-0.0, 0.0)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-16-4063bd6da228> in <module>()
----> 1 np.testing.assert_equal(-0.0, 0.0)

/Users/warren/anaconda/lib/python2.7/site-packages/numpy/testing/utils.pyc
in assert_equal(actual, desired, err_msg, verbose)
    309         elif desired == 0 and actual == 0:
    310             if not signbit(desired) == signbit(actual):
--> 311                 raise AssertionError(msg)
    312     # If TypeError or ValueError raised while using isnan and
co, just handle
    313     # as before

AssertionError:
Items are not equal:
 ACTUAL: -0.0
 DESIRED: 0.0

There is code that checks for this specific case, so this is
intentional.  But this is not consistent with how negative zeros in
arrays are compared:

In [22]: np.testing.assert_equal(np.array(-0.0), np.array(0.0))  # PASS

In [23]: a = np.array([-0.0])

In [24]: b = np.array([0.0])

In [25]: np.testing.assert_array_equal(a, b)  # PASS


Is there a reason the values are considered equal in an array, but not
when compared as scalars?

Warren



More information about the NumPy-Discussion mailing list