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
Considering that 'r' and 'i' are the real and imaginary parts of the
complex number, 'srss' is the hypotenuse, that's simple, right?
>
> return Complex<T>(
> std::sqrt( 2*(srss + r) ),
> 0.5*Complex<T>( i, -r ).csgn()*std::sqrt( 2*( srss - r ) )
> );
According to my sources(*), given the hypotenuse calculated before, the
[complex] square root of a complex number is calculated according to
this formula
sqrt(x + iy) = sqrt(0.5)*[sqrt(hyp + x) + i*sgn(y)*sqrt(hyp - x)]
where 'sgn' is a simple sign function. It would seem that there is an
unnecessary creation of a temporary of type 'Complex<T>' and a factor
incorrect. I'd write the expression in the 'return' statement as
Complex<T>(sqrt(0.5*(srss + r)), sign(i)*sqrt(0.5*(srss - r)))
> }
>
> First I'll explain the csgn() function.
There seems to be no need in it.
> 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 );
There is no need to test 'is_zero'. Just return Support<T>::sign(r),
no? The point is that if the complex number is 0, its real part is
zero, and 'Support::sign' will return that zero, right? Or does
Support::sign consider the sign of 0 positive?
> }
Again, you can safely ignore it, I think.
> Consider Support<T>::sign to be a regular sign function.
How regular? Assuming that the sign of 0 is 0, so the function should be
template <typename T> inline Complex<T> Complex<T>::sqrt() const {
T srss = std::sqrt( r*r + i*i );
return Complex<T>(
std::sqrt( 0.5*(srss + r) ),
Support<T>::sign(i)*std::sqrt( 0.5*( srss - r ) )
);
}
Do test it please.
(*) http://mathworld.wolfram.com/SquareRoot.html
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Regards
Chris Saunders
"Victor Bazarov" <v.Aba...@comAcast.net> wrote in message
news:gqcatr$6ht$1...@news.datemas.de...
[snip,snip,snip]
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask
>
I always find this funny :)
Regards
Chris Saunders
"Vladimir Jovic" <vlada...@gmail.com> wrote in message
news:gqcirv$9j3$1...@news01.versatel.de...
Top-posting in this forum is not a personal style preference. See
FAQ 5.4 http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
For some reason (must be the diabetes acting up) the OP thought the
top-posting offends me. It doesn't. I just don't reply to top-posted
messages. That's *my personal preference*. And I let everybody know in
advance. What's wrong with that? Not imposing or anything...
Moving on...
Well, in your response to my post, you found it necessary to insult me. I
was not being affected by diabetes at the time of the response (and am not
now). I have read
"http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4" and
disagree with it. I do resent what I quoted above but am still grateful for
the original response.
Regards
Chris Saunders
"Victor Bazarov" <v.Aba...@comAcast.net> wrote in message
news:gqfvfg$f5i$2...@news.datemas.de...
What is besser about top-posting than posting the answer directly under
its context?
Read here:
http://learn.to/quote/ particularly point 2.3
--
Thomas
> I have read "http://www.parashift.com/c++-faq-lite/how-to-
> post.html#faq-5.4" and disagree with it.
In that case it seems you have the following choices:
1) Respect the netiquette of this group and learn to live with bottom-
posting
2) Continue to top-post here (but prepare to be ignored, or to attract
hostility from users of this group who prefer that the netiquette be
respected)
3) Don't post here anymore
--
Lionel B
I think you are confusing his signature tag line with a message
directly targeting you. No where in his response did Victor insult
you.
REH
I personally would never withhold assistance to someone who posts in a style
that I dislike nor would I comment on my dislike of that persons style. I'm
just guessing, but I bet lots of people in this newsgroup feel the same. I
am wary of "thought police".
Regards
Chris Saunders
"REH" <spam...@stny.rr.com> wrote in message
news:934f8919-e0a6-4f52...@r18g2000vbi.googlegroups.com...
Could people please stop feeding this troll.
TIA.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!
> I personally would never withhold assistance to someone who posts in a
> style that I dislike
Nor would I.
But I would, and do, choose to ignore those who think that the rules for
etiquette and manners are mere "style," and don't apply to them.
*plonk*
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
I am bottom posting this message because, although I am not convinced, the
argument given is somewhat convincing to me. I do not know the meaning of
"*plonk*" but it seems to me that it has a negative connotation. I would
not have continued posting on this subject had I not received what I
considered to be an insulting comment regarding the fact that I suffer from
sugar diabetes. Your argument deserves further thought and I will give it
some. Please note that I have attempted to remain polite even I considered
myself to have been offended.
Regards
Chris Saunders
Try Wikipedia. Hint: it's not the wine.
Look again. The line quoted was taken from his signature.
REH