-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
should complex/0 division match complex/(0+0im)? #22983
Comments
So, right now, if you divide anything by a complex zero, you get |
Seems like the real part of the value you get shouldn't depend on the type of the denominator – if you have code that does |
For the record, in NumPy real(1) / complex(0) matches complex(1) / complex(0): >>> import numpy
>>> numpy.float64(1) / numpy.complex128(0)
(inf+nan*j)
>>> numpy.complex128(1) / numpy.complex128(0)
(inf+nan*j) |
The reason that However, Our current behavior, in my mind, is much more sensible:
(The exception to all of this is |
That being said, #include <complex.h>
#include <stdio.h>
int main(void)
{
double complex z = 0.0 + 0.0*I;
double complex w1 = 1.0 / z;
double complex w2 = (1.0 + 0.0*I) / z;
printf("%g+%gi\n", creal(w1), cimag(w1));
printf("%g+%gi\n", creal(w2), cimag(w2));
return 0;
} prints:
|
|
Is there a short answer to why it is picky about the sign of the |
@tkelman, |
why is that? I'm missing why the sign of the imaginary component of that should be negative |
@tkelman, because the imaginary part of |
Also Clang gives me the same result as GCC for your C program. And both GCC/Clang and NumPy gives >>> import numpy as np
>>> np.complex128(1.0 - 0.0j)
(1+0j)
>>> np.imag(np.complex128(1.0 - 0.0j))
array(0.0) The question now seems to be: adhere to mathematics (and the standards) or follow the mass? I'd say the former. Whatever will be the outcome of this discussion, it's probably a good idea to test these corner cases, to be sure they won't change without notice. |
@giordano, to get |
R also gives:
Matlab is even worse, naturally:
and GNU Octave does the same thing (with a |
In gfortran, they had a discussion about this and eventually settled on always returning
|
I'm still surprised that gcc doesn't follow the ISO standard here. Am I misreading it? It seems very clear to me. |
(I also tried |
I opened PR #23013 to add tests for the current behavior. |
It seems like we should either do nothing to continue following the standard, or change |
My suspicion is that the IEEE standard defined |
+1 for doing nothing and continuing to follow the standard. Given that floating-point numbers include ±Inf, ±0, and NaN, some options for the result of division by zero are more sensible than others. My feeling is that the ISO standard's choice is very reasonable, arguably more reasonable than Python or gcc. As I explained above, you get that behavior if you view |
That was the feeling on the triage call as well, so let's close this. |
Sorry for necroposting this, but I just add the occasion to try the Intel compiler:
#include <complex.h>
#include <stdio.h>
int main(void)
{
double complex z = 0.0 + 0.0*I;
double complex w1 = 1.0 / z;
double complex w2 = (1.0 + 0.0*I) / z;
printf("(%g,%g)\n", creal(w1), cimag(w1));
printf("(%g,%g)\n", creal(w2), cimag(w2));
return 0;
} $ icc --version
icc (ICC) 18.0.3 20180410
Copyright (C) 1985-2018 Intel Corporation. All rights reserved.
$ icc -std=c99 complex.c && ./a.out
(inf,-nan)
(inf,-nan)
#include <complex>
#include <iostream>
int main(void)
{
std::complex<double> z(0.0, 0.0);
std::complex<double> w1 = 1.0 / z;
std::complex<double> w2 = std::complex<double>(1.0, 0.0) / z;
std::cout << w1 << std::endl;
std::cout << w2 << std::endl;
return 0;
} $ icc complex.cpp && ./a.out
(inf,-nan)
(inf,-nan) In addition, Mathematica gives |
We're not going to make complex numbers projective by default, but #9790 does raise legitimate concerns about behavioral differences between division by real and complex zeros, e.g.:
More generally, the question is about the menagerie of complex NaNs.
The text was updated successfully, but these errors were encountered: