Unexpected return values for np.mod with x2=np.inf and similar

Hi all, I ran into an odd edge-case with np.mod and was wondering if this is the expected behavior, and if so why. This is on a fresh install of python 3.10.14 with numpy 1.26.4 from conda-forge.
import numpy as np
# I have a hard time coming up with a rationale for why 2 of these produce non-inf values and the other 2 produce inf values.
# these are possibly sensible although the signed zero pattern is unexpected to me.
Any ideas why these are the return values? I had a hard time tracking down where in the numpy source np.mod was coming from. Jesse

What operating system? If I recall correctly, NumPy tries to be compatible with CPython for these edge cases. The actual implementation is a bit scattered. I think it would be nice if we could have an "explain" decorator to ufuncs that would return the name of the inner loop used in practice to aid in debugging and teaching. Until then your best bet is to build NumPy locally with debug information and use a debugger, but even that can be challenging at times. Matti On 07/06/2024 21:10, jesse.livezey@gmail.com wrote:

On Mon, 2024-06-10 at 10:49 +0300, Matti Picus wrote:
Right, and there seems nothing odd here to me. Try using `divmod()` on a few numbers (not infinities) to realize that this is how Python defines things. Python modulo is not identical to IEEE modulo as describd in the docs. - Sebastian

What operating system? If I recall correctly, NumPy tries to be compatible with CPython for these edge cases. The actual implementation is a bit scattered. I think it would be nice if we could have an "explain" decorator to ufuncs that would return the name of the inner loop used in practice to aid in debugging and teaching. Until then your best bet is to build NumPy locally with debug information and use a debugger, but even that can be challenging at times. Matti On 07/06/2024 21:10, jesse.livezey@gmail.com wrote:

On Mon, 2024-06-10 at 10:49 +0300, Matti Picus wrote:
Right, and there seems nothing odd here to me. Try using `divmod()` on a few numbers (not infinities) to realize that this is how Python defines things. Python modulo is not identical to IEEE modulo as describd in the docs. - Sebastian
participants (3)
-
jesse.livezey@gmail.com
-
Matti Picus
-
Sebastian Berg