Boost logo

Boost :

From: Kevin Lynch (krlynch_at_[hidden])
Date: 2001-11-02 17:09:29


nbecker_at_[hidden] wrote:
>
> In the current gcc, norm of complex is defined in terms of abs. I
> believe the definition should be changed to:
>
> norm(z) = sqr(real(z)) + sqr (imag (z))
>
> My reasons are:
>
> 1. More efficient
> 2. Works for complex<int> (important in DSP)
> 3. Possibly more accurate
>
> I have started a discussion on gcc_at_[hidden] on this subject. I'm
> wondering if any of you have an opinion.
>

The implementation you are talking about looks like this, I believe:

template<typename T> inline T norm(const complex<T>& _x){
  T _t = abs(_x);
  return _t * _t;
}

Frankly, I don't see much point in this change ... norm isn't used that
often, so efficiency isn't really a concern (It's also not clear to me
that the resulting code will be any less efficient). I also don't think
there is really any issue with accuracy; for the cases where both real
and imag parts are either small or large, there is no accuracy loss, and
for the other cases, the accuracy difference would be so small as to be
negligible.

The other issue you raise is more worrisome: complex<int> There IS no
standard complex<int>:

26.2.2 [lib.complex.numbers] The effect of instantiating the template
complex for any type other than float, double, or long double is
unspecified.

In other words, you can't say complex<int> and have any idea what is
going to happen ("unspecified" means that behavior is both
implementation defined AND that there is no requirement that the
behavior even be documented (1.2.13 [defns.unspecified]) ). And this
makes perfect sense: most of the operations defined in 26.2 make no
sense when the real and imaginary parts are C++ int types: you can's say
abs(), or /, or arg(), or polar(), or pow, sqrt, log, sin, cos, sinh,
cosh, etc. etc. etc.)

So, to summarize, while your first and third points may be technically
correct, I don't see them as worth an implementation change, and your
second point has too many problems to consider.

-- 
-------------------------------------------------------------------------------
Kevin Lynch				voice:	 (617) 353-6065
Physics Department			Fax: (617) 353-6062
Boston University			office:	 PRB-565
590 Commonwealth Ave.			e-mail:	 krlynch_at_[hidden]
Boston, MA 02215 USA			http://physics.bu.edu/~krlynch
-------------------------------------------------------------------------------

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk