home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _complex.h < prev    next >
C/C++ Source or Header  |  2001-07-05  |  29KB  |  959 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18. #ifndef _STLP_internal_complex_h
  19. #define _STLP_internal_complex_h
  20.  
  21. // This header declares the template class complex, as described in 
  22. // in the draft C++ standard.  Single-precision complex numbers
  23. // are complex<float>, double-precision are complex<double>, and
  24. // quad precision are complex<long double>.
  25.  
  26. // Note that the template class complex is declared within namespace
  27. // std, as called for by the draft C++ standard.  
  28.  
  29. #include <cmath>
  30. #include <iosfwd>
  31.  
  32. _STLP_BEGIN_NAMESPACE
  33.  
  34. #if !defined(_STLP_NO_COMPLEX_SPECIALIZATIONS)        //*TY 02/25/2000 - added for MPW compiler workaround
  35.  
  36. template <class _Tp> struct complex;
  37.  
  38. _STLP_TEMPLATE_NULL  struct _STLP_CLASS_DECLSPEC complex<float>;
  39. _STLP_TEMPLATE_NULL  struct _STLP_CLASS_DECLSPEC complex<double>;
  40. # ifndef _STLP_NO_LONG_DOUBLE
  41. _STLP_TEMPLATE_NULL  struct _STLP_CLASS_DECLSPEC complex<long double>;
  42. # endif
  43. # endif
  44.  
  45. template <class _Tp>
  46. struct complex {
  47.   typedef _Tp value_type;
  48.   typedef complex<_Tp> _Self;
  49.  
  50.   // Constructors, destructor, assignment operator.
  51.   complex() : _M_re(0), _M_im(0) {}
  52.   complex(const value_type& __x)
  53.     : _M_re(__x), _M_im(0) {}
  54.   complex(const value_type& __x, const value_type& __y)
  55.     : _M_re(__x), _M_im(__y) {}
  56.   complex(const _Self& __z)
  57.     : _M_re(__z._M_re), _M_im(__z._M_im) {}
  58.  
  59.   _Self& operator=(const _Self& __z) {
  60.     _M_re = __z._M_re;
  61.     _M_im = __z._M_im;
  62.     return *this;
  63.   }
  64.  
  65. #if defined (_STLP_MEMBER_TEMPLATES) && ( defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) || defined(_STLP_NO_COMPLEX_SPECIALIZATIONS))
  66.   template <class _Tp2>
  67.   explicit complex(const complex<_Tp2>& __z)
  68.     : _M_re(__z._M_re), _M_im(__z._M_im) {}
  69.  
  70.   template <class _Tp2>
  71.   _Self& operator=(const complex<_Tp2>& __z) {
  72.     _M_re = __z._M_re;
  73.     _M_im = __z._M_im;
  74.     return *this;
  75.   }
  76. #endif /* _STLP_MEMBER_TEMPLATES */
  77.  
  78.   // Element access.
  79.   value_type real() const { return _M_re; }
  80.   value_type imag() const { return _M_im; }
  81.  
  82.   // Arithmetic op= operations involving one real argument.
  83.  
  84.   _Self& operator= (const value_type& __x) {
  85.     _M_re = __x;
  86.     _M_im = 0;
  87.     return *this;
  88.   }
  89.   _Self& operator+= (const value_type& __x) {
  90.     _M_re += __x;
  91.     return *this;
  92.   }
  93.   _Self& operator-= (const value_type& __x) {
  94.     _M_re -= __x;
  95.     return *this;
  96.   }
  97.   _Self& operator*= (const value_type& __x) {
  98.     _M_re *= __x;
  99.     _M_im *= __x;
  100.     return *this;
  101.   }
  102.   _Self& operator/= (const value_type& __x) {
  103.     _M_re /= __x;
  104.     _M_im /= __x;
  105.     return *this;
  106.   }
  107.  
  108.   // Arithmetic op= operations involving two complex arguments.
  109.  
  110.   static void  _STLP_CALL _div(const value_type& __z1_r, const value_type& __z1_i,
  111.                    const value_type& __z2_r, const value_type& __z2_i,
  112.                    value_type& __res_r, value_type& __res_i);
  113.  
  114.   static void _STLP_CALL _div(const value_type& __z1_r, 
  115.                    const value_type& __z2_r, const value_type& __z2_i,
  116.                    value_type& __res_r, value_type& __res_i);
  117.  
  118. #if defined ( _STLP_MEMBER_TEMPLATES ) // && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  119.  
  120.   template <class _Tp2> _Self& operator+= (const complex<_Tp2>& __z) {
  121.     _M_re += __z._M_re;
  122.     _M_im += __z._M_im;
  123.     return *this;
  124.   }
  125.  
  126.   template <class _Tp2> _Self& operator-= (const complex<_Tp2>& __z) {
  127.     _M_re -= __z._M_re;
  128.     _M_im -= __z._M_im;
  129.     return *this;
  130.   }
  131.  
  132.   template <class _Tp2> _Self& operator*= (const complex<_Tp2>& __z) {
  133.     value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  134.     value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  135.     _M_re = __r;
  136.     _M_im = __i;
  137.     return *this;
  138.   }
  139.  
  140.   template <class _Tp2> _Self& operator/= (const complex<_Tp2>& __z) {
  141.     value_type __r;
  142.     value_type __i;
  143.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  144.     _M_re = __r;
  145.     _M_im = __i;
  146.     return *this;
  147.   }
  148.  
  149. #endif /* _STLP_MEMBER_TEMPLATES */
  150.  
  151.   _Self& operator+= (const _Self& __z) {
  152.     _M_re += __z._M_re;
  153.     _M_im += __z._M_im;
  154.     return *this;
  155.   }
  156.  
  157.   _Self& operator-= (const _Self& __z) {
  158.     _M_re -= __z._M_re;
  159.     _M_im -= __z._M_im;
  160.     return *this;
  161.   }
  162.   
  163.   _Self& operator*= (const _Self& __z) {
  164.     value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  165.     value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  166.     _M_re = __r;
  167.     _M_im = __i;
  168.     return *this;
  169.   }
  170.  
  171.   _Self& operator/= (const _Self& __z) {
  172.     value_type __r;
  173.     value_type __i;
  174.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  175.     _M_re = __r;
  176.     _M_im = __i;
  177.     return *this;
  178.   }
  179.  
  180.   // Data members.
  181.   value_type _M_re;
  182.   value_type _M_im;
  183. };
  184.  
  185. #if !defined(_STLP_NO_COMPLEX_SPECIALIZATIONS)        //*TY 02/25/2000 - added for MPW compiler workaround
  186. // Explicit specializations for float, double, long double.  The only
  187. // reason for these specializations is to enable automatic conversions
  188. // from complex<float> to complex<double>, and complex<double> to
  189. // complex<long double>.
  190.  
  191.  
  192. _STLP_TEMPLATE_NULL
  193. struct _STLP_CLASS_DECLSPEC complex<float> {
  194.   typedef float value_type;
  195.   typedef complex<float> _Self;
  196.   // Constructors, destructor, assignment operator.
  197.  
  198.   complex(value_type __x = 0.0, value_type __y = 0.0)
  199.     : _M_re(__x), _M_im(__y) {}
  200.  
  201.   complex(const complex<float>& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {} 
  202.  
  203.   inline explicit complex(const complex<double>& __z);
  204. # ifndef _STLP_NO_LONG_DOUBLE
  205.   inline explicit complex(const complex<long double>& __z);
  206. # endif
  207.   // Element access.
  208.   value_type real() const { return _M_re; }
  209.   value_type imag() const { return _M_im; }
  210.  
  211.   // Arithmetic op= operations involving one real argument.
  212.  
  213.   _Self& operator= (value_type __x) {
  214.     _M_re = __x;
  215.     _M_im = 0;
  216.     return *this;
  217.   }
  218.   _Self& operator+= (value_type __x) {
  219.     _M_re += __x;
  220.     return *this;
  221.   }
  222.   _Self& operator-= (value_type __x) {
  223.     _M_re -= __x;
  224.     return *this;
  225.   }
  226.   _Self& operator*= (value_type __x) {
  227.     _M_re *= __x;
  228.     _M_im *= __x;
  229.     return *this;
  230.   }
  231.   _Self& operator/= (value_type __x) {
  232.     _M_re /= __x;
  233.     _M_im /= __x;
  234.     return *this;
  235.   }
  236.  
  237.   // Arithmetic op= operations involving two complex arguments.
  238.  
  239.   static void _STLP_CALL _div(const float& __z1_r, const float& __z1_i,
  240.                               const float& __z2_r, const float& __z2_i,
  241.                               float& __res_r, float& __res_i);
  242.     
  243.   static void _STLP_CALL _div(const float& __z1_r, 
  244.                               const float& __z2_r, const float& __z2_i,
  245.                               float& __res_r, float& __res_i);
  246.     
  247. #if defined (_STLP_MEMBER_TEMPLATES)
  248.  
  249.   template <class _Tp2>
  250.   complex<float>& operator=(const complex<_Tp2>& __z) {
  251.     _M_re = __z._M_re;
  252.     _M_im = __z._M_im;
  253.     return *this;
  254.   }
  255.  
  256.   template <class _Tp2>
  257.   complex<float>& operator+= (const complex<_Tp2>& __z) {
  258.     _M_re += __z._M_re;
  259.     _M_im += __z._M_im;
  260.     return *this;
  261.   }
  262.  
  263.   template <class _Tp2>
  264.   complex<float>& operator-= (const complex<_Tp2>& __z) {
  265.     _M_re -= __z._M_re;
  266.     _M_im -= __z._M_im;
  267.     return *this;
  268.   }
  269.  
  270.   template <class _Tp2>
  271.   complex<float>& operator*= (const complex<_Tp2>& __z) {
  272.     float __r = _M_re * __z._M_re - _M_im * __z._M_im;
  273.     float __i = _M_re * __z._M_im + _M_im * __z._M_re;
  274.     _M_re = __r;
  275.     _M_im = __i;
  276.     return *this;
  277.   }
  278.  
  279.   template <class _Tp2>
  280.   complex<float>& operator/= (const complex<_Tp2>& __z) {
  281.     float __r;
  282.     float __i;
  283.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  284.     _M_re = __r;
  285.     _M_im = __i;
  286.     return *this;
  287.   }
  288.  
  289. #endif /* _STLP_MEMBER_TEMPLATES */
  290.  
  291.   _Self& operator=(const _Self& __z) {
  292.     _M_re = __z._M_re;
  293.     _M_im = __z._M_im;
  294.     return *this;
  295.   }
  296.  
  297.   _Self& operator+= (const _Self& __z) {
  298.     _M_re += __z._M_re;
  299.     _M_im += __z._M_im;
  300.     return *this;
  301.   }
  302.  
  303.   _Self& operator-= (const _Self& __z) {
  304.     _M_re -= __z._M_re;
  305.     _M_im -= __z._M_im;
  306.     return *this;
  307.   }
  308.   
  309.   _Self& operator*= (const _Self& __z) {
  310.     value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  311.     value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  312.     _M_re = __r;
  313.     _M_im = __i;
  314.     return *this;
  315.   }
  316.  
  317.   _Self& operator/= (const _Self& __z) {
  318.     value_type __r;
  319.     value_type __i;
  320.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  321.     _M_re = __r;
  322.     _M_im = __i;
  323.     return *this;
  324.   }
  325.  
  326.   // Data members.
  327.   value_type _M_re;
  328.   value_type _M_im;
  329. };
  330.  
  331. _STLP_TEMPLATE_NULL struct _STLP_CLASS_DECLSPEC complex<double> {
  332.   typedef double value_type;
  333.   typedef complex<double> _Self;
  334.  
  335.   // Constructors, destructor, assignment operator.
  336.  
  337.   complex(value_type __x = 0.0, value_type __y = 0.0)
  338.     : _M_re(__x), _M_im(__y) {}
  339.  
  340.   complex(const complex<double>& __z)
  341.     : _M_re(__z._M_re), _M_im(__z._M_im) {}
  342.   inline complex(const complex<float>& __z);
  343. # ifndef _STLP_NO_LONG_DOUBLE
  344.   explicit inline complex(const complex<long double>& __z);
  345. # endif
  346.   // Element access.
  347.   value_type real() const { return _M_re; }
  348.   value_type imag() const { return _M_im; }
  349.  
  350.   // Arithmetic op= operations involving one real argument.
  351.  
  352.   _Self& operator= (value_type __x) {
  353.     _M_re = __x;
  354.     _M_im = 0;
  355.     return *this;
  356.   }
  357.   _Self& operator+= (value_type __x) {
  358.     _M_re += __x;
  359.     return *this;
  360.   }
  361.   _Self& operator-= (value_type __x) {
  362.     _M_re -= __x;
  363.     return *this;
  364.   }
  365.   _Self& operator*= (value_type __x) {
  366.     _M_re *= __x;
  367.     _M_im *= __x;
  368.     return *this;
  369.   }
  370.   _Self& operator/= (value_type __x) {
  371.     _M_re /= __x;
  372.     _M_im /= __x;
  373.     return *this;
  374.   }
  375.  
  376.   // Arithmetic op= operations involving two complex arguments.
  377.  
  378.   static void _STLP_CALL _div(const double& __z1_r, const double& __z1_i,
  379.                               const double& __z2_r, const double& __z2_i,
  380.                               double& __res_r, double& __res_i);
  381.   static void _STLP_CALL _div(const double& __z1_r, 
  382.                               const double& __z2_r, const double& __z2_i,
  383.                               double& __res_r, double& __res_i);
  384.     
  385. #if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  386.  
  387.   template <class _Tp2>
  388.   complex<double>& operator=(const complex<_Tp2>& __z) {
  389.     _M_re = __z._M_re;
  390.     _M_im = __z._M_im;
  391.     return *this;
  392.   }
  393.  
  394.   template <class _Tp2>
  395.   complex<double>& operator+= (const complex<_Tp2>& __z) {
  396.     _M_re += __z._M_re;
  397.     _M_im += __z._M_im;
  398.     return *this;
  399.   }
  400.  
  401.   template <class _Tp2>
  402.   complex<double>& operator-= (const complex<_Tp2>& __z) {
  403.     _M_re -= __z._M_re;
  404.     _M_im -= __z._M_im;
  405.     return *this;
  406.   }
  407.  
  408.   template <class _Tp2>
  409.   complex<double>& operator*= (const complex<_Tp2>& __z) {
  410.     double __r = _M_re * __z._M_re - _M_im * __z._M_im;
  411.     double __i = _M_re * __z._M_im + _M_im * __z._M_re;
  412.     _M_re = __r;
  413.     _M_im = __i;
  414.     return *this;
  415.   }
  416.  
  417.   template <class _Tp2>
  418.   complex<double>& operator/= (const complex<_Tp2>& __z) {
  419.     double __r;
  420.     double __i;
  421.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  422.     _M_re = __r;
  423.     _M_im = __i;
  424.     return *this;
  425.   }
  426.  
  427. #endif /* _STLP_MEMBER_TEMPLATES */
  428.  
  429.   _Self& operator=(const _Self& __z) {
  430.     _M_re = __z._M_re;
  431.     _M_im = __z._M_im;
  432.     return *this;
  433.   }
  434.  
  435.   _Self& operator+= (const _Self& __z) {
  436.     _M_re += __z._M_re;
  437.     _M_im += __z._M_im;
  438.     return *this;
  439.   }
  440.  
  441.   _Self& operator-= (const _Self& __z) {
  442.     _M_re -= __z._M_re;
  443.     _M_im -= __z._M_im;
  444.     return *this;
  445.   }
  446.   
  447.   _Self& operator*= (const _Self& __z) {
  448.     value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  449.     value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  450.     _M_re = __r;
  451.     _M_im = __i;
  452.     return *this;
  453.   }
  454.  
  455.   _Self& operator/= (const _Self& __z) {
  456.     value_type __r;
  457.     value_type __i;
  458.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  459.     _M_re = __r;
  460.     _M_im = __i;
  461.     return *this;
  462.   }
  463.  
  464.   // Data members.
  465.   value_type _M_re;
  466.   value_type _M_im;
  467. };
  468.  
  469. # ifndef _STLP_NO_LONG_DOUBLE
  470.  
  471. _STLP_TEMPLATE_NULL struct _STLP_CLASS_DECLSPEC complex<long double> {
  472.   typedef long double value_type;
  473.   typedef complex<long double> _Self;
  474.  
  475.   // Constructors, destructor, assignment operator.
  476.   complex(value_type __x = 0.0, value_type __y = 0.0)
  477.     : _M_re(__x), _M_im(__y) {}
  478.  
  479.   complex(const complex<long double>& __z)
  480.     : _M_re(__z._M_re), _M_im(__z._M_im) {}
  481.   inline complex(const complex<float>& __z);
  482.   inline complex(const complex<double>& __z);
  483.  
  484.   // Element access.
  485.   value_type real() const { return _M_re; }
  486.   value_type imag() const { return _M_im; }
  487.  
  488.   // Arithmetic op= operations involving one real argument.
  489.  
  490.   _Self& operator= (value_type __x) {
  491.     _M_re = __x;
  492.     _M_im = 0;
  493.     return *this;
  494.   }
  495.   _Self& operator+= (value_type __x) {
  496.     _M_re += __x;
  497.     return *this;
  498.   }
  499.   _Self& operator-= (value_type __x) {
  500.     _M_re -= __x;
  501.     return *this;
  502.   }
  503.   _Self& operator*= (value_type __x) {
  504.     _M_re *= __x;
  505.     _M_im *= __x;
  506.     return *this;
  507.   }
  508.   _Self& operator/= (value_type __x) {
  509.     _M_re /= __x;
  510.     _M_im /= __x;
  511.     return *this;
  512.   }
  513.  
  514.   // Arithmetic op= operations involving two complex arguments.
  515.  
  516.   static void _STLP_CALL _div(const long double& __z1_r, const long double& __z1_i,
  517.                               const long double& __z2_r, const long double& __z2_i,
  518.                               long double& __res_r, long double& __res_i);
  519.  
  520.   static void _STLP_CALL _div(const long double& __z1_r, 
  521.                               const long double& __z2_r, const long double& __z2_i,
  522.                               long double& __res_r, long double& __res_i);
  523.  
  524. #if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  525.  
  526.   template <class _Tp2>
  527.   complex<long double>& operator=(const complex<_Tp2>& __z) {
  528.     _M_re = __z._M_re;
  529.     _M_im = __z._M_im;
  530.     return *this;
  531.   }
  532.  
  533.   template <class _Tp2>
  534.   complex<long double>& operator+= (const complex<_Tp2>& __z) {
  535.     _M_re += __z._M_re;
  536.     _M_im += __z._M_im;
  537.     return *this;
  538.   }
  539.  
  540.   template <class _Tp2>
  541.   complex<long double>& operator-= (const complex<_Tp2>& __z) {
  542.     _M_re -= __z._M_re;
  543.     _M_im -= __z._M_im;
  544.     return *this;
  545.   }
  546.  
  547.   template <class _Tp2>
  548.   complex<long double>& operator*= (const complex<_Tp2>& __z) {
  549.     long double __r = _M_re * __z._M_re - _M_im * __z._M_im;
  550.     long double __i = _M_re * __z._M_im + _M_im * __z._M_re;
  551.     _M_re = __r;
  552.     _M_im = __i;
  553.     return *this;
  554.   }
  555.  
  556.   template <class _Tp2>
  557.   complex<long double>& operator/= (const complex<_Tp2>& __z) {
  558.     long double __r;
  559.     long double __i;
  560.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  561.     _M_re = __r;
  562.     _M_im = __i;
  563.     return *this;
  564.   }
  565.  
  566. #endif /* _STLP_MEMBER_TEMPLATES */
  567.  
  568.   _Self& operator=(const _Self& __z) {
  569.     _M_re = __z._M_re;
  570.     _M_im = __z._M_im;
  571.     return *this;
  572.   }
  573.  
  574.   _Self& operator+= (const _Self& __z) {
  575.     _M_re += __z._M_re;
  576.     _M_im += __z._M_im;
  577.     return *this;
  578.   }
  579.  
  580.   _Self& operator-= (const _Self& __z) {
  581.     _M_re -= __z._M_re;
  582.     _M_im -= __z._M_im;
  583.     return *this;
  584.   }
  585.   
  586.   _Self& operator*= (const _Self& __z) {
  587.     value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  588.     value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  589.     _M_re = __r;
  590.     _M_im = __i;
  591.     return *this;
  592.   }
  593.  
  594.   _Self& operator/= (const _Self& __z) {
  595.     value_type __r;
  596.     value_type __i;
  597.     _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  598.     _M_re = __r;
  599.     _M_im = __i;
  600.     return *this;
  601.   }
  602.  
  603.   // Data members.
  604.   value_type _M_re;
  605.   value_type _M_im;
  606. };
  607.  
  608. # endif /* _STLP_NO_LONG_DOUBLE */
  609.  
  610. // Converting constructors from one of these three specialized types
  611. // to another.
  612.  
  613. inline complex<float>::complex(const complex<double>& __z)
  614.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  615. inline complex<double>::complex(const complex<float>& __z)
  616.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  617. # ifndef _STLP_NO_LONG_DOUBLE
  618. inline complex<float>::complex(const complex<long double>& __z)
  619.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  620. inline complex<double>::complex(const complex<long double>& __z)
  621.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  622. inline complex<long double>::complex(const complex<float>& __z)
  623.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  624. inline complex<long double>::complex(const complex<double>& __z)
  625.   : _M_re(__z._M_re), _M_im(__z._M_im) {}
  626. # endif
  627.  
  628. # endif /* SPECIALIZATIONS */
  629.  
  630. // Unary non-member arithmetic operators.
  631.  
  632. template <class _Tp>
  633. inline complex<_Tp> _STLP_CALL operator+(const complex<_Tp>& __z) {
  634.   return __z;
  635. }
  636.  
  637. template <class _Tp>
  638. inline complex<_Tp> _STLP_CALL  operator-(const complex<_Tp>& __z) {
  639.   return complex<_Tp>(-__z._M_re, -__z._M_im);
  640. }
  641.  
  642. // Non-member arithmetic operations involving one real argument.
  643.  
  644. template <class _Tp> 
  645. inline complex<_Tp> _STLP_CALL operator+(const _Tp& __x, const complex<_Tp>& __z) {
  646.   return complex<_Tp>(__x + __z._M_re, __z._M_im);
  647. }
  648.  
  649. template <class _Tp> 
  650. inline complex<_Tp> _STLP_CALL operator+(const complex<_Tp>& __z, const _Tp& __x) {
  651.   return complex<_Tp>(__z._M_re + __x, __z._M_im);
  652. }
  653.  
  654. template <class _Tp> 
  655. inline complex<_Tp> _STLP_CALL operator-(const _Tp& __x, const complex<_Tp>& __z) {
  656.   return complex<_Tp>(__x - __z._M_re, -__z._M_im);
  657. }
  658.  
  659. template <class _Tp> 
  660. inline complex<_Tp> _STLP_CALL operator-(const complex<_Tp>& __z, const _Tp& __x) {
  661.   return complex<_Tp>(__z._M_re - __x, __z._M_im);
  662. }
  663.  
  664. template <class _Tp> 
  665. inline complex<_Tp> _STLP_CALL operator*(const _Tp& __x, const complex<_Tp>& __z) {
  666.   return complex<_Tp>(__x * __z._M_re, __x * __z._M_im);
  667. }
  668.  
  669. template <class _Tp> 
  670. inline complex<_Tp> _STLP_CALL operator*(const complex<_Tp>& __z, const _Tp& __x) {
  671.   return complex<_Tp>(__z._M_re * __x, __z._M_im * __x);
  672. }
  673.  
  674. template <class _Tp> 
  675. inline complex<_Tp> _STLP_CALL operator/(const _Tp& __x, const complex<_Tp>& __z) {
  676.   complex<_Tp> __result;
  677.   complex<_Tp>::_div(__x,
  678.                      __z._M_re, __z._M_im,
  679.                      __result._M_re, __result._M_im);
  680.   return __result;
  681. }
  682.  
  683. template <class _Tp> 
  684. inline complex<_Tp> _STLP_CALL operator/(const complex<_Tp>& __z, const _Tp& __x) {
  685.   return complex<_Tp>(__z._M_re / __x, __z._M_im / __x);
  686. }
  687.  
  688. // Non-member arithmetic operations involving two complex arguments
  689.  
  690. template <class _Tp> 
  691. inline complex<_Tp> _STLP_CALL 
  692. operator+(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  693.   return complex<_Tp>(__z1._M_re + __z2._M_re, __z1._M_im + __z2._M_im);
  694. }
  695.  
  696. template <class _Tp> 
  697. inline complex<_Tp> _STLP_CALL 
  698. operator-(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  699.   return complex<_Tp>(__z1._M_re - __z2._M_re, __z1._M_im - __z2._M_im);
  700. }
  701.  
  702. template <class _Tp> 
  703. inline complex<_Tp> _STLP_CALL 
  704. operator*(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  705.   return complex<_Tp>(__z1._M_re * __z2._M_re - __z1._M_im * __z2._M_im,
  706.                       __z1._M_re * __z2._M_im + __z1._M_im * __z2._M_re);
  707. }
  708.  
  709. template <class _Tp> 
  710. inline complex<_Tp> _STLP_CALL 
  711. operator/(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  712.   complex<_Tp> __result;
  713.   complex<_Tp>::_div(__z1._M_re, __z1._M_im,
  714.                      __z2._M_re, __z2._M_im,
  715.                      __result._M_re, __result._M_im);
  716.   return __result;
  717. }
  718.  
  719. // Comparison operators.
  720.  
  721. template <class _Tp> 
  722. inline bool _STLP_CALL operator==(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  723.   return __z1._M_re == __z2._M_re && __z1._M_im == __z2._M_im;
  724. }
  725.  
  726. template <class _Tp> 
  727. inline bool _STLP_CALL operator==(const complex<_Tp>& __z, const _Tp& __x) {
  728.   return __z._M_re == __x && __z._M_im == 0;
  729. }
  730.  
  731. template <class _Tp> 
  732. inline bool _STLP_CALL operator==(const _Tp& __x, const complex<_Tp>& __z) {
  733.   return __x == __z._M_re && 0 == __z._M_im;
  734. }
  735.  
  736. #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
  737.  
  738. template <class _Tp> 
  739. inline bool _STLP_CALL operator!=(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  740.   return __z1._M_re != __z2._M_re || __z1._M_im != __z2._M_im;
  741. }
  742.  
  743. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  744.  
  745. template <class _Tp> 
  746. inline bool _STLP_CALL operator!=(const complex<_Tp>& __z, const _Tp& __x) {
  747.   return __z._M_re != __x || __z._M_im != 0;
  748. }
  749.  
  750. template <class _Tp> 
  751. inline bool _STLP_CALL operator!=(const _Tp& __x, const complex<_Tp>& __z) {
  752.   return __x != __z._M_re || 0 != __z._M_im;
  753. }
  754.  
  755. // Other basic arithmetic operations
  756.  
  757. template <class _Tp> 
  758. inline _Tp _STLP_CALL real(const complex<_Tp>& __z) {
  759.   return __z._M_re;
  760. }
  761.  
  762. template <class _Tp> 
  763. inline _Tp _STLP_CALL imag(const complex<_Tp>& __z) {
  764.   return __z._M_im;
  765. }
  766.  
  767. _STLP_DECLSPEC float  _STLP_CALL abs(const complex<float>&);
  768. _STLP_DECLSPEC double  _STLP_CALL abs(const complex<double>&);
  769. _STLP_DECLSPEC float  _STLP_CALL arg(const complex<float>&);
  770. _STLP_DECLSPEC double  _STLP_CALL arg(const complex<double>&);
  771. _STLP_DECLSPEC complex<float> _STLP_CALL polar(const float& __rho, const float& __phi);
  772. _STLP_DECLSPEC complex<double> _STLP_CALL polar(const double& __rho, const double& __phi);
  773.  
  774.  
  775. # ifndef _STLP_NO_LONG_DOUBLE
  776. _STLP_DECLSPEC long double  _STLP_CALL arg(const complex<long double>&);
  777. _STLP_DECLSPEC long double  _STLP_CALL abs(const complex<long double>&);
  778. _STLP_DECLSPEC complex<long double>  _STLP_CALL polar(const long double&, const long double&);
  779. # endif
  780.  
  781. template <class _Tp>
  782. _Tp _STLP_CALL abs(const complex<_Tp>& __z) {
  783.   return _Tp(abs(complex<double>(double(__z.real()), double(__z.imag()))));
  784. }
  785.  
  786. template <class _Tp>
  787. _Tp _STLP_CALL arg(const complex<_Tp>& __z) {
  788.   return _Tp(arg(complex<double>(double(__z.real()), double(__z.imag()))));
  789. }
  790.  
  791. template <class _Tp>
  792. inline _Tp _STLP_CALL norm(const complex<_Tp>& __z) {
  793.   return __z._M_re * __z._M_re + __z._M_im * __z._M_im;
  794. }
  795.  
  796. template <class _Tp>
  797. inline complex<_Tp> _STLP_CALL conj(const complex<_Tp>& __z) {
  798.   return complex<_Tp>(__z._M_re, -__z._M_im);
  799. }
  800.  
  801. template <class _Tp>
  802. complex<_Tp> _STLP_CALL polar(const _Tp& __rho) {
  803.   return complex<_Tp>(__rho, 0);
  804. }
  805.  
  806. template <class _Tp>
  807. complex<_Tp> _STLP_CALL polar(const _Tp& __rho, const _Tp& __phi) {
  808.   complex<double> __tmp = polar(double(__rho), double(__phi));
  809.   return complex<_Tp>(_Tp(__tmp.real()), _Tp(__tmp.imag()));
  810. }
  811.  
  812. #ifdef _STLP_USE_NEW_IOSTREAMS
  813.  
  814. // Complex output, in the form (re,im).  We use a two-step process 
  815. // involving stringstream so that we get the padding right.  
  816. template <class _Tp, class _CharT, class _Traits>
  817. basic_ostream<_CharT, _Traits>&  _STLP_CALL 
  818. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z);
  819.  
  820. template <class _Tp, class _CharT, class _Traits>
  821. basic_istream<_CharT, _Traits>& _STLP_CALL 
  822. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z);
  823.  
  824. // Specializations for narrow characters; lets us avoid widen.
  825.  
  826. _STLP_OPERATOR_TEMPLATE
  827. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL 
  828. operator>>(basic_istream<char, char_traits<char> >& __is, complex<float>& __z);
  829.  
  830. _STLP_OPERATOR_TEMPLATE
  831. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL 
  832. operator>>(basic_istream<char, char_traits<char> >& __is, complex<double>& __z);
  833.  
  834.  
  835. _STLP_OPERATOR_TEMPLATE
  836. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL 
  837. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<float>& __z);
  838.  
  839. _STLP_OPERATOR_TEMPLATE
  840. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL 
  841. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<double>& __z);
  842.  
  843. #  if ! defined (_STLP_NO_LONG_DOUBLE)
  844. _STLP_OPERATOR_TEMPLATE
  845. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL 
  846. operator>>(basic_istream<char, char_traits<char> >& __is, complex<long double>& __z);
  847.  
  848. _STLP_OPERATOR_TEMPLATE
  849. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL 
  850. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<long double>& __z);
  851.  
  852. # endif
  853.  
  854. # if defined (_STLP_USE_TEMPLATE_EXPORT) && ! defined (_STLP_NO_WCHAR_T)
  855.  
  856. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator>>(
  857.         basic_istream<wchar_t, char_traits<wchar_t> >&, complex<double>&);
  858. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator<<(
  859.         basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<double>&);
  860. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator>>(
  861.         basic_istream<wchar_t, char_traits<wchar_t> >&, complex<float>&);
  862. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator<<(
  863.         basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<float>&);
  864.  
  865. #  ifndef _STLP_NO_LONG_DOUBLE
  866. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator>>(
  867.         basic_istream<wchar_t, char_traits<wchar_t> >&, complex<long double>&);
  868. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL operator<<(
  869.         basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<long double>&);
  870. #  endif
  871.  
  872. # endif /* USE_TEMPLATE_EXPORT */
  873.  
  874. #else /* _STLP_USE_NEW_IOSTREAMS */
  875.  
  876. template <class _Tp>
  877. ostream& _STLP_CALL operator<<(ostream& s, const complex<_Tp>& __z);
  878.  
  879. template <class _Tp>
  880. istream& _STLP_CALL  operator>>(istream& s, complex<_Tp>& a);
  881.  
  882. #endif /* _STLP_USE_NEW_IOSTREAMS */
  883.  
  884.  
  885. // Transcendental functions.  These are defined only for float, 
  886. //  double, and long double.  (Sqrt isn't transcendental, of course,
  887. //  but it's included in this section anyway.)
  888.  
  889. _STLP_DECLSPEC complex<float> _STLP_CALL sqrt(const complex<float>&);
  890.  
  891. _STLP_DECLSPEC complex<float> _STLP_CALL exp(const complex<float>&);
  892. _STLP_DECLSPEC complex<float> _STLP_CALL  log(const complex<float>&);
  893. _STLP_DECLSPEC complex<float> _STLP_CALL log10(const complex<float>&);
  894.  
  895. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, int);
  896. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, const float&);
  897. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const float&, const complex<float>&);
  898. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, const complex<float>&);
  899.  
  900. _STLP_DECLSPEC complex<float> _STLP_CALL sin(const complex<float>&);
  901. _STLP_DECLSPEC complex<float> _STLP_CALL cos(const complex<float>&);
  902. _STLP_DECLSPEC complex<float> _STLP_CALL tan(const complex<float>&);
  903.  
  904. _STLP_DECLSPEC complex<float> _STLP_CALL sinh(const complex<float>&);
  905. _STLP_DECLSPEC complex<float> _STLP_CALL cosh(const complex<float>&);
  906. _STLP_DECLSPEC complex<float> _STLP_CALL tanh(const complex<float>&);
  907.  
  908. _STLP_DECLSPEC complex<double> _STLP_CALL sqrt(const complex<double>&);
  909.  
  910. _STLP_DECLSPEC complex<double> _STLP_CALL exp(const complex<double>&);
  911. _STLP_DECLSPEC complex<double> _STLP_CALL log(const complex<double>&);
  912. _STLP_DECLSPEC complex<double> _STLP_CALL log10(const complex<double>&);
  913.  
  914. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, int);
  915. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, const double&);
  916. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const double&, const complex<double>&);
  917. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, const complex<double>&);
  918.  
  919. _STLP_DECLSPEC complex<double> _STLP_CALL sin(const complex<double>&);
  920. _STLP_DECLSPEC complex<double> _STLP_CALL cos(const complex<double>&);
  921. _STLP_DECLSPEC complex<double> _STLP_CALL tan(const complex<double>&);
  922.  
  923. _STLP_DECLSPEC complex<double> _STLP_CALL sinh(const complex<double>&);
  924. _STLP_DECLSPEC complex<double> _STLP_CALL cosh(const complex<double>&);
  925. _STLP_DECLSPEC complex<double> _STLP_CALL tanh(const complex<double>&);
  926.  
  927. # ifndef _STLP_NO_LONG_DOUBLE
  928. _STLP_DECLSPEC complex<long double> _STLP_CALL sqrt(const complex<long double>&);
  929. _STLP_DECLSPEC complex<long double> _STLP_CALL exp(const complex<long double>&);
  930. _STLP_DECLSPEC complex<long double> _STLP_CALL log(const complex<long double>&);
  931. _STLP_DECLSPEC complex<long double> _STLP_CALL log10(const complex<long double>&);
  932.  
  933. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&, int);
  934. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&, const long double&);
  935. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const long double&, const complex<long double>&);
  936. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&,
  937.                                                    const complex<long double>&);
  938.  
  939. _STLP_DECLSPEC complex<long double> _STLP_CALL sin(const complex<long double>&);
  940. _STLP_DECLSPEC complex<long double> _STLP_CALL cos(const complex<long double>&);
  941. _STLP_DECLSPEC complex<long double> _STLP_CALL tan(const complex<long double>&);
  942.  
  943. _STLP_DECLSPEC complex<long double> _STLP_CALL sinh(const complex<long double>&);
  944. _STLP_DECLSPEC complex<long double> _STLP_CALL cosh(const complex<long double>&);
  945. _STLP_DECLSPEC complex<long double> _STLP_CALL tanh(const complex<long double>&);
  946. # endif
  947.  
  948. _STLP_END_NAMESPACE
  949.  
  950. # ifndef _STLP_LINK_TIME_INSTANTIATION
  951. #  include <stl/_complex.c>
  952. # endif
  953.  
  954. #endif /* _STLP_template_complex */
  955.  
  956. // Local Variables:
  957. // mode:C++
  958. // End:
  959.