Skip to content

BUG: allclose does not warn for invalid value encountered in multiply or alternativly checks for validity of tolerances #28205

@TobiasGlaubach

Description

@TobiasGlaubach

Describe the issue:

First and foremost thanks for all the work so many people put into this awesome library and this bug report is just to hopefully improve numpy even further.

I know this is really an edge case caused by faulty user, input. I stumbled on it when I accidentally set rtol to np.inf and had zeros in the b array.

The issue:

because np.allclose is defined by:

absolute(a - b) <= (atol + rtol * absolute(b))

this means that if rtol = np.inf and there is a 0 in b allclose automatically evaluates to False because (in python) inf * 0 --> nan and 0 <= nan --> False. Which is as per python definition correct.

The unfortunate part is, that np.allclose does not warn against invalid value multiplication as multiplying a numpy zero array with inf does:

RuntimeWarning: invalid value encountered in multiply np.abs([0., 0.]) * np.inf

Maybe np.allclose should warn for this was well, or prevent np.inf to be put into atol and rtol?

If there is agreement on how this should be handled I am happy to implement/support this.

Reproduce the code example:

np.allclose([0., 0.], [0, 0.], atol=0, rtol=np.inf)
>> False

np.abs([0., 0.]) * np.inf
>> RuntimeWarning: invalid value encountered in multiply
>> np.abs([0., 0.]) * np.inf
>> array([nan, nan])

Error message:

Python and NumPy Versions:

1.24.3
3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]

Runtime Environment:

No response

Context for the issue:

This is really an edge case and will only arise if someone accidentally inserts a wring input for rtol, but in the end this is what warnings are for :-D.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions