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


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

Re: Change definition of complex::norm


Benjamin Kosnik <bkoz@redhat.com> writes:

| If there is a problem, let's fix it, and add a test case.

Here is a first draft I'm planning to check in.

-- Gaby

Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/egcs/libstdc++-v3/ChangeLog,v
retrieving revision 1.830
diff -p -r1.830 ChangeLog
*** ChangeLog	2001/10/31 20:05:28	1.830
--- ChangeLog	2001/11/01 08:07:29
***************
*** 1,3 ****
--- 1,8 ----
+ 2001-11-01  Gabriel Dos Reis  <gdr@merlin.codesourcery.com>
+ 
+ 	* include/bits/std_complex.h (_Norm_helper): New class template.
+ 	(norm): Tweak.
+ 
  2001-10-31  Benjamin Kosnik  <bkoz@redhat.com>
  
  	libstdc++/4749
Index: include/bits/std_complex.h
===================================================================
RCS file: /cvs/gcc/egcs/libstdc++-v3/include/bits/std_complex.h,v
retrieving revision 1.20
diff -p -r1.20 std_complex.h
*** std_complex.h	2001/10/31 08:45:57	1.20
--- std_complex.h	2001/11/01 08:07:29
***************
*** 40,45 ****
--- 40,46 ----
  #pragma GCC system_header
  
  #include <bits/c++config.h>
+ #include <bits/cpp_type_traits.h>
  #include <bits/std_cmath.h>
  #include <bits/std_sstream.h>
  
*************** namespace std
*** 417,428 ****
      arg(const complex<_Tp>& __z)
      { return atan2(__z.imag(), __z.real()); }
  
    template<typename _Tp>
      inline _Tp
      norm(const complex<_Tp>& __z)
      {
!       _Tp __res = abs(__z);
!       return __res * __res;
      }
  
    template<typename _Tp>
--- 418,456 ----
      arg(const complex<_Tp>& __z)
      { return atan2(__z.imag(), __z.real()); }
  
+   // 26.2.7/5: norm(__z) returns the squared magintude of __z.
+   //     As defined, norm() is -not- a norm is the common mathematical
+   //     sens used in numerics.  The helper class _Norm_helper<> tries to
+   //     distinguish between builtin floating point and the rest, so as
+   //     to deliver an answer as close as possible to the real value.
+   template<bool>
+     struct _Norm_helper
+     {
+       template<typename _Tp>
+         static inline _Tp _S_do_it(const complex<_Tp>& __z)
+         {
+           const _Tp __x = __z.real();
+           const _Tp __y = __z.imag();
+           return __x * __x + __y * __y;
+         }
+     };
+ 
+   template<>
+     struct _Norm_helper<true>
+     {
+       template<typename _Tp>
+         static inline _Tp _S_do_it(const complex<_Tp>& __z)
+         {
+           _Tp __res = abs(__z);
+           return __res * __res;
+         }
+     };
+   
    template<typename _Tp>
      inline _Tp
      norm(const complex<_Tp>& __z)
      {
!       return _Norm_helper<__is_floating<_Tp>::_M_type>::_S_do_it(__z);
      }
  
    template<typename _Tp>


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