-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected NaN when signing ±inf + 0j
#25679
Comments
inf + 0j
inf + 0j
inf + 0j
±inf + 0j
I find NumPy's output strange here, because the complex sign is defined as |
In any case, it seems NumPy has special handling for this case, so |
True. The confrontation between the result of |
The apparent For example, the result of >>> jnp.complex64(jnp.inf+1j) * 2
Array(inf+nanj, dtype=complex64) forms as follows:
because Similarly, the nan value will occur in division: >>> jnp.complex64(jnp.inf+1j) / 2
Array(inf+nanj, dtype=complex64) that is,
For libraries that do implement mixed mode arithmetic, complex division/multiplication with floats would not involve
In practice, however, libraries typically implement mixed mode arithmetic or use special handling when the expected result would be different from the result of using non-mixed mode arithmetic. For instance, numpy casts floats to complex in complex operations, similar to JAX (or vice versa :) ): >>> np.complex64(np.inf+1j) * 2
(inf+nanj) but numpy.sign on complex inputs uses special handling (before numpy 2): >>> print(np.sign.__doc__)
<snip>
For complex inputs, the `sign` function returns
``sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j``.
<snip> or (starting from numpy 2): >>> print(np.sign.__doc__)
<snip>
For complex inputs, the `sign` function returns ``x / abs(x)``, ...
<snip> and >>> np.sign(np.inf+1j)
np.complex128(1+0j) |
Description
I am getting unexpected
nan
sign with the following example.What jax/jaxlib version are you using?
jax v0.4.38, jaxlib v0.4.38 numpy v1.26.4
Which accelerator(s) are you using?
CPU
Additional system info?
Python 3.11, Windows
NVIDIA GPU info
No response
The text was updated successfully, but these errors were encountered: