C
Chris Saunders
I'm attempting to understand this function. I'm attempting to adapt it to
another language - Eiffel. It seems to me that the function doesn't work
correctly. Here is the function:
template <typename T> inline Complex<T> Complex<T>::sqrt() const {
T srss = std::sqrt( r*r + i*i );
return Complex<T>(
std::sqrt( 2*(srss + r) ),
0.5*Complex<T>( i, -r ).csgn()*std::sqrt( 2*( srss - r ) )
);
}
First I'll explain the csgn() function. If the complex number is zero it
returns 0.0 otherwise it returns the sign of r which is the real part of the
complex number. csgn()'s return value is a double. Now if the the
imaginary part of the complex number is 0.0 and the real part is less than
zero it seems to me that the imaginary part of the result will be 0.0 which
must be incorrect because the square root of a negative real number will
have its imaginary part set to the square root of -r. Note that csgn(i, -r)
returns 0.0 in this case.
I'm going to include the csgn() function here on the small possibility that
I am not interpreting is correctly:
template <typename T> inline T Complex<T>::csgn() const {
return is_zero() ? 0 : Support<T>::sign( r );
}
Consider Support<T>::sign to be a regular sign function.
Thanks for any assistance.
Regards
Chris Saunders
another language - Eiffel. It seems to me that the function doesn't work
correctly. Here is the function:
template <typename T> inline Complex<T> Complex<T>::sqrt() const {
T srss = std::sqrt( r*r + i*i );
return Complex<T>(
std::sqrt( 2*(srss + r) ),
0.5*Complex<T>( i, -r ).csgn()*std::sqrt( 2*( srss - r ) )
);
}
First I'll explain the csgn() function. If the complex number is zero it
returns 0.0 otherwise it returns the sign of r which is the real part of the
complex number. csgn()'s return value is a double. Now if the the
imaginary part of the complex number is 0.0 and the real part is less than
zero it seems to me that the imaginary part of the result will be 0.0 which
must be incorrect because the square root of a negative real number will
have its imaginary part set to the square root of -r. Note that csgn(i, -r)
returns 0.0 in this case.
I'm going to include the csgn() function here on the small possibility that
I am not interpreting is correctly:
template <typename T> inline T Complex<T>::csgn() const {
return is_zero() ? 0 : Support<T>::sign( r );
}
Consider Support<T>::sign to be a regular sign function.
Thanks for any assistance.
Regards
Chris Saunders