8000 assert_almost_equal() for floats and assert_array_almost_equal() have different semantics · Issue #5200 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
assert_almost_equal() for floats and assert_array_almost_equal() have different semantics #5200
Closed
@edisongustavo

Description

@edisongustavo

When using assert_almost_equal for floats I get different results when using arrays.

This can be seen by this code:

>>> from numpy.testing import assert_almost_equal
>>> assert_almost_equal(1.01, 1.02, 2)
AssertionError:
Arrays are not almost equal to 2 decimals
 ACTUAL: 1.01
 DESIRED: 1.02
>>> assert_almost_equal([1.01, 1.0], [1.02, 1.0], 2)
>>>

Looking at utils.py the behavior can be explained:

Float

When comparing using simple float values, this is the if that checks if values are equal:

if round(abs(desired - actual), decimal) != 0 :
https://github.com/numpy/numpy/blob/master/numpy/testing/utils.py#L489

Array

But when comparing using arrays:

return around(z, decimal) <= 10.0**(-decimal)
https://github.com/numpy/numpy/blob/master/numpy/testing/utils.py#L838

Making the same assert behave in different ways.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0