This code
print *, sqrt(cmplx(-1))
print *, sqrt(cmplx(-1,0))
print *, sqrt((-1,0))
print *, sqrt(-(1,0))
gives me this output
(0.00000000,1.00000000)
(0.00000000,1.00000000)
(0.00000000,1.00000000)
(0.00000000,-1.00000000)
I believe that the correct algebra is sqrt(-1)=i
. Why the result of the last line?
The compiler version is GCC 7.3.0, running on Linux openSUSE 42.2 (x86_64).
EDIT
Following @francescalus answer I have tried more cases:
print *, sqrt((-1,-0))
print *, sqrt((-1,-0.))
print *, (-1,-0)
print *, (-1,-0.)
and I get
(0.00000000,1.00000000)
(0.00000000,-1.00000000)
(-1.00000000,0.00000000)
(-1.00000000,-0.00000000)
So, it seems that my compiler support negative zeros for real
numbers. So, I guess it is important to care when working with variables like this:
complex :: asd
asd=(1.,0.)
print *, sqrt(-asd)
Here I get again the wrong result, but the zero negative thing is more difficult to predict. I have so many questions! Do you know some other exmple that can induce a mistake? Do you have an advice to avoid this mistakes? Do you now some compiler flag to turn off the negative cero support for the GCC compiler?