This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: this seems to lost (complex norm, abs)


Theodore Papadopoulo <Theodore.Papadopoulo@sophia.inria.fr> writes:

> First the norm of a complex<T> is not a T when T is integer.

Yes it is. This isn't maths, its C++. For example

  int mass = 3;
  int velocity = 4;
  int kinetic_energy = mass * velocity * velocity / 2;

might all have different dimensions but can all be integers.

That said I can't see any circumstances when the following is less
accurate or more prone to overflow, and it is always more efficient:


2001-09-20 Philip Martin <philip_martin@nltworld.com>

        * include/bits/std_complex.h: don't use abs() in norm()


--- libstdc++-v3/include/bits/std_complex.h	Wed Aug 15 14:11:42 2001
+++ libstdc++-v3-new/include/bits/std_complex.h	Thu Sep 20 19:28:54 2001
@@ -421,8 +421,7 @@
     inline _Tp
     norm(const complex<_Tp>& __z)
     {
-      _Tp __res = abs(__z);
-      return __res * __res;
+      return __z.real() * __z.real() + __z.imag() * __z.imag();
     }
 
   template<typename _Tp>




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]