BUG: allclose does not warn for invalid value encountered in multiply or alternativly checks for validity of tolerances · Issue #28205 · numpy/numpy · GitHub
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.
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.
The reason you don't see a warning is because inside of isclose the actual multiplication happens inside of a block that is explicitly ignoring floating point exceptions:
Activity
ngoldbaum commented on Jan 21, 2025
The reason you don't see a warning is because inside of isclose the actual multiplication happens inside of a block that is explicitly ignoring floating point exceptions:
numpy/numpy/_core/numeric.py
Lines 2446 to 2451 in 304f8f2
Calling np.isinf on atol and rtol in that function and explicitly warning if they are inf seems reasonable to me.