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]

this seems to lost (complex norm, abs)


hi,
this bug report seems to forgotten
http://gcc.gnu.org/ml/libstdc++/2001-08/msg00165.html
and I thought I know a few things about maths (since I've got an MS),
but can someone explain me this abs:
--------------------------
template<typename _Tp>
    inline _Tp
    abs(const complex<_Tp>& __z)
    {
      _Tp __x = __z.real();
      _Tp __y = __z.imag();
      const _Tp __s = abs(__x) + abs(__y);
      if (__s == _Tp())  // well ...
        return __s;
      __x /= __s; 
      __y /= __s;
      return __s * sqrt(__x * __x + __y * __y);
    }
--------------------------
it contains 2 abs, 2 addition, 2 division, 3 multilication and 1 sqrt
may be a few cosine and tan help a bit more:-)
while the trivial "grammar school implementation":
sqrt(norm(__z))
contains 1 addition, 2 multilication, 1 sqrt
so why we use it? may be in case of __z == 0 it's faster a bit, so an
"advanced" implementetion can be:
--------------------------
      const _Tp __s = abs(__x) + abs(__y);
      if (__s == _Tp())  // well ...
        return __s;
      else
        return sqrt(norm(__z));
--------------------------
which use 2 more abs plus an addition (assume the compare is not expensive).
actualy I prefer the first "grammar school implementation" (and may be 
another additional isZero() or something like that) but the "advanced"
version can be better...may be...
I don't look into other complex function, but may be useful to revise them.
yours.

 -- Levente                               "Si vis pacem para bellum!"


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