From 9a356f094bfc599903cc39c58cdfffe0452ce4ef Mon Sep 17 00:00:00 2001 From: bkoz Date: Mon, 20 Nov 2000 21:13:45 +0000 Subject: [PATCH] 2000-11-20 Benjamin Kosnik * include/bits/std_complex.h: Tweaks, include cmath for abs overloads. * src/complex.cc: Remove cmath include, formatting tweaks, remove dead code. * include/c/bits/std_cmath.h: Formatting tweaks. * testsuite/26_numerics/complex_value.cc: New file, for catching bits gleaned from libstdc++/106. * testsuite/23_containers/vector_ctor.cc (test02): Add test from libstdc++/102. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@37591 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 14 + libstdc++-v3/include/bits/std_complex.h | 1250 ++++++++++---------- libstdc++-v3/include/c/bits/std_cmath.h | 2 +- libstdc++-v3/src/complex.cc | 67 -- .../testsuite/23_containers/vector_ctor.cc | 17 +- .../testsuite/26_numerics/complex_value.cc | 64 + 6 files changed, 698 insertions(+), 716 deletions(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/complex_value.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index bda9b45..fff54a6 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2000-11-20 Benjamin Kosnik + + * include/bits/std_complex.h: Tweaks, include cmath for abs overloads. + * src/complex.cc: Remove cmath include, formatting tweaks, remove + dead code. + * include/c/bits/std_cmath.h: Formatting tweaks. + * testsuite/26_numerics/complex_value.cc: New file, for catching + bits gleaned from libstdc++/106. + + * testsuite/23_containers/vector_ctor.cc (test02): Add test from + libstdc++/102. + + * src/string-inst.cc: Tweaks. + 2000-11-20 Joseph S. Myers * include/bits/c++config, include/bits/ios_base.h, diff --git a/libstdc++-v3/include/bits/std_complex.h b/libstdc++-v3/include/bits/std_complex.h index ce14dee..35ca378 100644 --- a/libstdc++-v3/include/bits/std_complex.h +++ b/libstdc++-v3/include/bits/std_complex.h @@ -28,7 +28,7 @@ // the GNU General Public License. // -// ISO 14882/26.2.1 +// ISO C++ 14882: 26.2 Complex Numbers // Note: this is not a conforming implementation. // Initially implemented by Ulrich Drepper // Improved by Gabriel Dos Reis @@ -38,6 +38,7 @@ #define _CPP_COMPLEX 1 #include +#include #include namespace std @@ -50,13 +51,13 @@ namespace std template<> class complex; template _Tp abs(const complex<_Tp>&); - template _Tp arg(const complex<_Tp>&); + template _Tp arg(const complex<_Tp>&); template _Tp norm(const complex<_Tp>&); template complex<_Tp> conj(const complex<_Tp>&); template complex<_Tp> polar(const _Tp&, const _Tp&); - // Transcendentals: + // Transcendentals: template complex<_Tp> cos(const complex<_Tp>&); template complex<_Tp> cosh(const complex<_Tp>&); template complex<_Tp> exp(const complex<_Tp>&); @@ -64,7 +65,7 @@ namespace std template complex<_Tp> log10(const complex<_Tp>&); template complex<_Tp> pow(const complex<_Tp>&, int); template complex<_Tp> pow(const complex<_Tp>&, const _Tp&); - template complex<_Tp> pow (const complex<_Tp>&, + template complex<_Tp> pow(const complex<_Tp>&, const complex<_Tp>&); template complex<_Tp> pow(const _Tp&, const complex<_Tp>&); template complex<_Tp> sin(const complex<_Tp>&); @@ -74,10 +75,8 @@ namespace std template complex<_Tp> tanh(const complex<_Tp>&); - // // 26.2.2 Primary template class complex - // - template + template class complex { public: @@ -87,7 +86,7 @@ namespace std // Let's the compiler synthetize the copy constructor // complex (const complex<_Tp>&); - template + template complex(const complex<_Up>&); _Tp real() const; @@ -102,15 +101,15 @@ namespace std // Let's the compiler synthetize the // copy and assignment operator // complex<_Tp>& operator= (const complex<_Tp>&); - template + template complex<_Tp>& operator=(const complex<_Up>&); - template + template complex<_Tp>& operator+=(const complex<_Up>&); - template + template complex<_Tp>& operator-=(const complex<_Up>&); - template + template complex<_Tp>& operator*=(const complex<_Up>&); - template + template complex<_Tp>& operator/=(const complex<_Up>&); private: @@ -125,312 +124,396 @@ namespace std inline _Tp complex<_Tp>::imag() const { return _M_imag; } - - // - // 26.2.3 complex specializations - // - - // - // complex specialization - // - template<> class complex - { - public: - typedef float value_type; - - complex(float = 0.0f, float = 0.0f); -#ifdef _GLIBCPP_BUGGY_COMPLEX - complex(const complex& __z) : _M_value(__z._M_value) { } -#endif - explicit complex(const complex&); - explicit complex(const complex&); - - float real() const; - float imag() const; + template + inline + complex<_Tp>::complex(const _Tp& __r, const _Tp& __i) + : _M_real(__r), _M_imag(__i) { } - complex& operator=(float); - complex& operator+=(float); - complex& operator-=(float); - complex& operator*=(float); - complex& operator/=(float); + template + template + inline + complex<_Tp>::complex(const complex<_Up>& __z) + : _M_real(__z.real()), _M_imag(__z.imag()) { } - // Let's the compiler synthetize the copy and assignment - // operator. It always does a pretty good job. - // complex& operator= (const complex&); - template - complex&operator=(const complex<_Tp>&); - template - complex& operator+=(const complex<_Tp>&); - template - complex& operator-=(const complex<_Tp>&); - template - complex& operator*=(const complex<_Tp>&); - template - complex&operator/=(const complex<_Tp>&); - - private: - typedef __complex__ float _ComplexT; - _ComplexT _M_value; + template + complex<_Tp>& + complex<_Tp>::operator=(const _Tp& __t) + { + _M_real = __t; + _M_imag = _Tp(); + return *this; + } - complex(_ComplexT __z) : _M_value(__z) { } - - friend class complex; - friend class complex; + // 26.2.5/1 + template + inline complex<_Tp>& + complex<_Tp>::operator+=(const _Tp& __t) + { + _M_real += __t; + return *this; + } - friend complex pow<>(const complex&, int); - friend complex pow<>(const complex&, const float&); - friend complex pow<>(const complex&, - const complex&); - friend complex pow<>(const float&, const complex&); - friend complex sqrt<>(const complex&); - friend complex tan<>(const complex&); - friend complex tanh<>(const complex&); - }; + // 26.2.5/3 + template + inline complex<_Tp>& + complex<_Tp>::operator-=(const _Tp& __t) + { + _M_real -= __t; + return *this; + } - inline float - complex::real() const - { return __real__ _M_value; } + // 26.2.5/5 + template + complex<_Tp>& + complex<_Tp>::operator*=(const _Tp& __t) + { + _M_real *= __t; + _M_imag *= __t; + return *this; + } - inline float - complex::imag() const - { return __imag__ _M_value; } + // 26.2.5/7 + template + complex<_Tp>& + complex<_Tp>::operator/=(const _Tp& __t) + { + _M_real /= __t; + _M_imag /= __t; + return *this; + } + template + template + complex<_Tp>& + complex<_Tp>::operator=(const complex<_Up>& __z) + { + _M_real = __z.real(); + _M_imag = __z.imag(); + return *this; + } - // - // complex specialization - // - template<> class complex - { - public: - typedef double value_type; + // 26.2.5/9 + template + template + complex<_Tp>& + complex<_Tp>::operator+=(const complex<_Up>& __z) + { + _M_real += __z.real(); + _M_imag += __z.imag(); + return *this; + } - complex(double =0.0, double =0.0); -#ifdef _GLIBCPP_BUGGY_COMPLEX - complex(const complex& __z) : _M_value(__z._M_value) { } -#endif - complex(const complex&); - explicit complex(const complex&); - - double real() const; - double imag() const; - - complex& operator=(double); - complex& operator+=(double); - complex& operator-=(double); - complex& operator*=(double); - complex& operator/=(double); + // 26.2.5/11 + template + template + complex<_Tp>& + complex<_Tp>::operator-=(const complex<_Up>& __z) + { + _M_real -= __z.real(); + _M_imag -= __z.imag(); + return *this; + } - // The compiler will synthetize this, efficiently. - // complex& operator= (const complex&); - template - complex& operator=(const complex<_Tp>&); - template - complex& operator+=(const complex<_Tp>&); - template - complex& operator-=(const complex<_Tp>&); - template - complex& operator*=(const complex<_Tp>&); - template - complex& operator/=(const complex<_Tp>&); + // 26.2.5/13 + // XXX: This is a grammar school implementation. + template + template + complex<_Tp>& + complex<_Tp>::operator*=(const complex<_Up>& __z) + { + const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); + _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); + _M_real = __r; + return *this; + } - private: - typedef __complex__ double _ComplexT; - _ComplexT _M_value; + // 26.2.5/15 + // XXX: This is a grammar school implementation. + template + template + complex<_Tp>& + complex<_Tp>::operator/=(const complex<_Up>& __z) + { + const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); + const _Tp __n = norm(__z); + _M_imag = (_M_real * __z.imag() - _M_imag * __z.real()) / __n; + _M_real = __r / __n; + return *this; + } + + // Operators: + template + inline complex<_Tp> + operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) += __y; } - complex(_ComplexT __z) : _M_value(__z) { } - - friend class complex; - friend class complex; + template + inline complex<_Tp> + operator+(const complex<_Tp>& __x, const _Tp& __y) + { return complex<_Tp> (__x) += __y; } - friend complex pow<>(const complex&, int); - friend complex pow<>(const complex&, const double&); - friend complex pow<>(const complex&, - const complex&); - friend complex pow<>(const double&, const complex&); - friend complex sqrt<>(const complex&); - friend complex tan<>(const complex&); - friend complex tanh<>(const complex&); - }; + template + inline complex<_Tp> + operator+(const _Tp& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__y) += __x; } - inline double - complex::real() const - { return __real__ _M_value; } + template + inline complex<_Tp> + operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) -= __y; } + + template + inline complex<_Tp> + operator-(const complex<_Tp>& __x, const _Tp& __y) + { return complex<_Tp> (__x) -= __y; } - inline double - complex::imag() const - { return __imag__ _M_value; } + template + inline complex<_Tp> + operator-(const _Tp& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) -= __y; } + template + inline complex<_Tp> + operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) *= __y; } - // - // complex specialization - // - template<> class complex - { - public: - typedef long double value_type; + template + inline complex<_Tp> + operator*(const complex<_Tp>& __x, const _Tp& __y) + { return complex<_Tp> (__x) *= __y; } - complex(long double = 0.0L, long double = 0.0L); -#ifdef _GLIBCPP_BUGGY_COMPLEX - complex(const complex& __z) : _M_value(__z._M_value) { } -#endif - complex(const complex&); - complex(const complex&); + template + inline complex<_Tp> + operator*(const _Tp& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__y) *= __x; } - long double real() const; - long double imag() const; - - complex& operator= (long double); - complex& operator+= (long double); - complex& operator-= (long double); - complex& operator*= (long double); - complex& operator/= (long double); - - // The compiler knows how to do this efficiently - // complex& operator= (const complex&); - - template - complex& operator=(const complex<_Tp>&); - template - complex& operator+=(const complex<_Tp>&); - template - complex& operator-=(const complex<_Tp>&); - template - complex& operator*=(const complex<_Tp>&); - template - complex& operator/=(const complex<_Tp>&); + template + inline complex<_Tp> + operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) /= __y; } + + template + inline complex<_Tp> + operator/(const complex<_Tp>& __x, const _Tp& __y) + { return complex<_Tp> (__x) /= __y; } - private: - typedef __complex__ long double _ComplexT; - _ComplexT _M_value; + template + inline complex<_Tp> + operator/(const _Tp& __x, const complex<_Tp>& __y) + { return complex<_Tp> (__x) /= __y; } - complex(_ComplexT __z) : _M_value(__z) { } + template + inline complex<_Tp> + operator+(const complex<_Tp>& __x) + { return __x; } - friend class complex; - friend class complex; + template + inline complex<_Tp> + operator-(const complex<_Tp>& __x) + { return complex<_Tp>(-__x.real(), -__x.imag()); } - friend complex pow<>(const complex&, int); - friend complex pow<>(const complex&, - const long double&); - friend complex pow<>(const complex&, - const complex&); - friend complex pow<>(const long double&, - const complex&); - friend complex sqrt<>(const complex&); - friend complex tan<>(const complex&); - friend complex tanh<>(const complex&); - }; + template + inline bool + operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return __x.real() == __y.real() && __x.imag() == __y.imag(); } - inline - complex::complex(long double __r, long double __i) - { - __real__ _M_value = __r; - __imag__ _M_value = __i; - } + template + inline bool + operator==(const complex<_Tp>& __x, const _Tp& __y) + { return __x.real() == __y && __x.imag() == _Tp(); } - inline - complex::complex(const complex& __z) - : _M_value(_ComplexT(__z._M_value)) { } + template + inline bool + operator==(const _Tp& __x, const complex<_Tp>& __y) + { return __x == __y.real() && _Tp() == __y.imag(); } - inline - complex::complex(const complex& __z) - : _M_value(_ComplexT(__z._M_value)) { } + template + inline bool + operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) + { return __x.real() != __y.real() || __x.imag() != __y.imag(); } - inline long double - complex::real() const - { return __real__ _M_value; } + template + inline bool + operator!=(const complex<_Tp>& __x, const _Tp& __y) + { return __x.real() != __y || __x.imag() != _Tp(); } - inline long double - complex::imag() const - { return __imag__ _M_value; } + template + inline bool + operator!=(const _Tp& __x, const complex<_Tp>& __y) + { return __x != __y.real() || _Tp() != __y.imag(); } - inline complex& - complex::operator=(long double __r) - { - __real__ _M_value = __r; - __imag__ _M_value = 0.0L; - return *this; - } + template + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>&, complex<_Tp>&); - inline complex& - complex::operator+=(long double __r) - { - __real__ _M_value += __r; - return *this; - } + template + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>&, const complex<_Tp>&); - inline complex& - complex::operator-=(long double __r) - { - __real__ _M_value -= __r; - return *this; - } + // Values + template + inline _Tp + real(const complex<_Tp>& __z) + { return __z.real(); } + + template + inline _Tp + imag(const complex<_Tp>& __z) + { return __z.imag(); } - inline complex& - complex::operator*=(long double __r) - { - __real__ _M_value *= __r; - return *this; - } + template + 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); + } - inline complex& - complex::operator/=(long double __r) - { - __real__ _M_value /= __r; - return *this; - } + template + inline _Tp + arg(const complex<_Tp>& __z) + { return atan2(__z.imag(), __z.real()); } template - inline complex& - complex::operator=(const complex<_Tp>& __z) + inline _Tp + norm(const complex<_Tp>& __z) { - __real__ _M_value = __z.real(); - __imag__ _M_value = __z.imag(); - return *this; + _Tp __res = abs(__z); + return __res * __res; } template - inline complex& - complex::operator+=(const complex<_Tp>& __z) + inline complex<_Tp> + polar(const _Tp& __rho, const _Tp& __theta) + { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } + + template + inline complex<_Tp> + conj(const complex<_Tp>& __z) + { return complex<_Tp>(__z.real(), -__z.imag()); } + + // Transcendentals + template + inline complex<_Tp> + cos(const complex<_Tp>& __z) { - __real__ _M_value += __z.real(); - __imag__ _M_value += __z.imag(); - return *this; + const _Tp __x = __z.real(); + const _Tp __y = __z.imag(); + return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); } template - inline complex& - complex::operator-=(const complex<_Tp>& __z) + inline complex<_Tp> + cosh(const complex<_Tp>& __z) { - __real__ _M_value -= __z.real(); - __imag__ _M_value -= __z.imag(); - return *this; + const _Tp __x = __z.real(); + const _Tp __y = __z.imag(); + return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); } - + template - inline complex& - complex::operator*=(const complex<_Tp>& __z) + inline complex<_Tp> + exp(const complex<_Tp>& __z) + { return polar(exp(__z.real()), __z.imag()); } + + template + inline complex<_Tp> + log(const complex<_Tp>& __z) + { return complex<_Tp>(log(abs(__z)), arg(__z)); } + + template + inline complex<_Tp> + log10(const complex<_Tp>& __z) + { return log(__z) / log(_Tp(10.0)); } + + template + inline complex<_Tp> + sin(const complex<_Tp>& __z) { - _ComplexT __t; - __real__ __t = __z.real(); - __imag__ __t = __z.imag(); - _M_value *= __t; - return *this; + const _Tp __x = __z.real(); + const _Tp __y = __z.imag(); + return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); } template - inline complex& - complex::operator/=(const complex<_Tp>& __z) + inline complex<_Tp> + sinh(const complex<_Tp>& __z) { - _ComplexT __t; - __real__ __t = __z.real(); - __imag__ __t = __z.imag(); - _M_value /= __t; - return *this; + const _Tp __x = __z.real(); + const _Tp __y = __z.imag(); + return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); } - // - // complex continued. - // + // 26.2.3 complex specializations + // complex specialization + template<> class complex + { + public: + typedef float value_type; + + complex(float = 0.0f, float = 0.0f); +#ifdef _GLIBCPP_BUGGY_COMPLEX + complex(const complex& __z) : _M_value(__z._M_value) { } +#endif + explicit complex(const complex&); + explicit complex(const complex&); + + float real() const; + float imag() const; + + complex& operator=(float); + complex& operator+=(float); + complex& operator-=(float); + complex& operator*=(float); + complex& operator/=(float); + + // Let's the compiler synthetize the copy and assignment + // operator. It always does a pretty good job. + // complex& operator= (const complex&); + template + complex&operator=(const complex<_Tp>&); + template + complex& operator+=(const complex<_Tp>&); + template + complex& operator-=(const complex<_Tp>&); + template + complex& operator*=(const complex<_Tp>&); + template + complex&operator/=(const complex<_Tp>&); + + private: + typedef __complex__ float _ComplexT; + _ComplexT _M_value; + + complex(_ComplexT __z) : _M_value(__z) { } + + friend class complex; + friend class complex; + + friend complex pow<>(const complex&, int); + friend complex pow<>(const complex&, const float&); + friend complex pow<>(const complex&, + const complex&); + friend complex pow<>(const float&, const complex&); + friend complex sqrt<>(const complex&); + friend complex tan<>(const complex&); + friend complex tanh<>(const complex&); + }; + + inline float + complex::real() const + { return __real__ _M_value; } + + inline float + complex::imag() const + { return __imag__ _M_value; } + inline complex::complex(float r, float i) { @@ -438,14 +521,6 @@ namespace std __imag__ _M_value = i; } - inline - complex::complex(const complex& __z) - : _M_value(_ComplexT(__z._M_value)) { } - - inline - complex::complex(const complex& __z) - : _M_value(_ComplexT(__z._M_value)) { } - inline complex& complex::operator=(float __f) { @@ -491,50 +566,109 @@ namespace std return *this; } - template - inline complex& - complex::operator+=(const complex<_Tp>& __z) - { - __real__ _M_value += __z.real(); - __imag__ _M_value += __z.imag(); - return *this; - } - - template - inline complex& - complex::operator-=(const complex<_Tp>& __z) - { - __real__ _M_value -= __z.real(); - __imag__ _M_value -= __z.real(); - return *this; - } + template + inline complex& + complex::operator+=(const complex<_Tp>& __z) + { + __real__ _M_value += __z.real(); + __imag__ _M_value += __z.imag(); + return *this; + } + + template + inline complex& + complex::operator-=(const complex<_Tp>& __z) + { + __real__ _M_value -= __z.real(); + __imag__ _M_value -= __z.real(); + return *this; + } + + template + inline complex& + complex::operator*=(const complex<_Tp>& __z) + { + _ComplexT __t; + __real__ __t = __z.real(); + __imag__ __t = __z.imag(); + _M_value *= __t; + return *this; + } + + template + inline complex& + complex::operator/=(const complex<_Tp>& __z) + { + _ComplexT __t; + __real__ __t = __z.real(); + __imag__ __t = __z.imag(); + _M_value /= __t; + return *this; + } + + // 26.2.3 complex specializations + // complex specialization + template<> class complex + { + public: + typedef double value_type; + + complex(double =0.0, double =0.0); +#ifdef _GLIBCPP_BUGGY_COMPLEX + complex(const complex& __z) : _M_value(__z._M_value) { } +#endif + explicit complex(const complex&); + explicit complex(const complex&); + + double real() const; + double imag() const; + + complex& operator=(double); + complex& operator+=(double); + complex& operator-=(double); + complex& operator*=(double); + complex& operator/=(double); + + // The compiler will synthetize this, efficiently. + // complex& operator= (const complex&); + template + complex& operator=(const complex<_Tp>&); + template + complex& operator+=(const complex<_Tp>&); + template + complex& operator-=(const complex<_Tp>&); + template + complex& operator*=(const complex<_Tp>&); + template + complex& operator/=(const complex<_Tp>&); + + private: + typedef __complex__ double _ComplexT; + _ComplexT _M_value; + + complex(_ComplexT __z) : _M_value(__z) { } + + friend class complex; + friend class complex; - template - inline complex& - complex::operator*=(const complex<_Tp>& __z) - { - _ComplexT __t; - __real__ __t = __z.real(); - __imag__ __t = __z.imag(); - _M_value *= __t; - return *this; - } + friend complex pow<>(const complex&, int); + friend complex pow<>(const complex&, const double&); + friend complex pow<>(const complex&, + const complex&); + friend complex pow<>(const double&, const complex&); + friend complex sqrt<>(const complex&); + friend complex tan<>(const complex&); + friend complex tanh<>(const complex&); + }; - template - inline complex& - complex::operator/=(const complex<_Tp>& __z) - { - _ComplexT __t; - __real__ __t = __z.real(); - __imag__ __t = __z.imag(); - _M_value /= __t; - return *this; - } + inline double + complex::real() const + { return __real__ _M_value; } + inline double + complex::imag() const + { return __imag__ _M_value; } - // - // complex continued. - // inline complex::complex(double __r, double __i) { @@ -542,17 +676,6 @@ namespace std __imag__ _M_value = __i; } - inline - complex::complex(const complex& __z) - : _M_value(_ComplexT(__z._M_value)) { } - - inline - complex::complex(const complex& __z) - { - __real__ _M_value = __z.real(); - __imag__ _M_value = __z.imag(); - } - inline complex& complex::operator=(double __d) { @@ -636,357 +759,198 @@ namespace std __imag__ __t = __z.imag(); _M_value /= __t; return *this; - } - - // - // Primary template class complex continued. - // - // 26.2.4 - template - inline - complex<_Tp>::complex(const _Tp& __r, const _Tp& __i) - : _M_real(__r), _M_imag(__i) { } - - template - template - inline - complex<_Tp>::complex(const complex<_Up>& __z) - : _M_real(__z.real()), _M_imag(__z.imag()) { } - - // 26.2.7/4 - template - inline _Tp - norm(const complex<_Tp>& __z) - { - // XXX: Grammar school computation - return __z.real() * __z.real() + __z.imag() * __z.imag(); - } - - template - complex<_Tp>& - complex<_Tp>::operator=(const _Tp& __t) - { - _M_real = __t; - _M_imag = _Tp(); - return *this; - } - - // 26.2.5/1 - template - inline complex<_Tp>& - complex<_Tp>::operator+=(const _Tp& __t) - { - _M_real += __t; - return *this; - } - - // 26.2.5/3 - template - inline complex<_Tp>& - complex<_Tp>::operator-=(const _Tp& __t) - { - _M_real -= __t; - return *this; - } - - // 26.2.5/5 - template - complex<_Tp>& - complex<_Tp>::operator*=(const _Tp& __t) - { - _M_real *= __t; - _M_imag *= __t; - return *this; - } - - // 26.2.5/7 - template - complex<_Tp>& - complex<_Tp>::operator/=(const _Tp& __t) - { - _M_real /= __t; - _M_imag /= __t; - return *this; - } - - template - template - complex<_Tp>& - complex<_Tp>::operator=(const complex<_Up>& __z) - { - _M_real = __z.real(); - _M_imag = __z.imag(); - return *this; - } - - // 26.2.5/9 - template - template - complex<_Tp>& - complex<_Tp>::operator+=(const complex<_Up>& __z) - { - _M_real += __z.real(); - _M_imag += __z.imag(); - return *this; - } - - // 26.2.5/11 - template - template - complex<_Tp>& - complex<_Tp>::operator-=(const complex<_Up>& __z) - { - _M_real -= __z.real(); - _M_imag -= __z.imag(); - return *this; - } - - // 26.2.5/13 - // XXX: this is a grammar school implementation. - template - template - complex<_Tp>& - complex<_Tp>::operator*=(const complex<_Up>& __z) - { - const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); - _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); - _M_real = __r; - return *this; - } - - // 26.2.5/15 - // XXX: this is a grammar school implementation. - template - template - complex<_Tp>& - complex<_Tp>::operator/=(const complex<_Up>& __z) - { - const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); - const _Tp __n = norm(__z); - _M_imag = (_M_real * __z.imag() - _M_imag * __z.real()) / __n; - _M_real = __r / __n; - return *this; - } - - // Operators: - template - inline complex<_Tp> - operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) += __y; } - - template - inline complex<_Tp> - operator+(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) += __y; } - - template - inline complex<_Tp> - operator+(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) += __x; } - - template - inline complex<_Tp> - operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } - - template - inline complex<_Tp> - operator-(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) -= __y; } - - template - inline complex<_Tp> - operator-(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } - - template - inline complex<_Tp> - operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) *= __y; } - - template - inline complex<_Tp> - operator*(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) *= __y; } - - template - inline complex<_Tp> - operator*(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) *= __x; } - - template - inline complex<_Tp> - operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } - - template - inline complex<_Tp> - operator/(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) /= __y; } - - template - inline complex<_Tp> - operator/(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } - - template - inline complex<_Tp> - operator+(const complex<_Tp>& __x) - { return __x; } + } - template - inline complex<_Tp> - operator-(const complex<_Tp>& __x) - { return complex<_Tp>(-__x.real(), -__x.imag()); } + // 26.2.3 complex specializations + // complex specialization + template<> class complex + { + public: + typedef long double value_type; - template - inline bool - operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return __x.real() == __y.real() && __x.imag() == __y.imag(); } + complex(long double = 0.0L, long double = 0.0L); +#ifdef _GLIBCPP_BUGGY_COMPLEX + complex(const complex& __z) : _M_value(__z._M_value) { } +#endif + explicit complex(const complex&); + explicit complex(const complex&); - template - inline bool - operator==(const complex<_Tp>& __x, const _Tp& __y) - { return __x.real() == __y && __x.imag() == _Tp(); } + long double real() const; + long double imag() const; - template - inline bool - operator==(const _Tp& __x, const complex<_Tp>& __y) - { return __x == __y.real() && _Tp() == __y.imag(); } + complex& operator= (long double); + complex& operator+= (long double); + complex& operator-= (long double); + complex& operator*= (long double); + complex& operator/= (long double); - template - inline bool - operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return __x.real() != __y.real() || __x.imag() != __y.imag(); } + // The compiler knows how to do this efficiently + // complex& operator= (const complex&); + template + complex& operator=(const complex<_Tp>&); + template + complex& operator+=(const complex<_Tp>&); + template + complex& operator-=(const complex<_Tp>&); + template + complex& operator*=(const complex<_Tp>&); + template + complex& operator/=(const complex<_Tp>&); - template - inline bool - operator!=(const complex<_Tp>& __x, const _Tp& __y) - { return __x.real() != __y || __x.imag() != _Tp(); } + private: + typedef __complex__ long double _ComplexT; + _ComplexT _M_value; - template - inline bool - operator!=(const _Tp& __x, const complex<_Tp>& __y) - { return __x != __y.real() || _Tp() != __y.imag(); } + complex(_ComplexT __z) : _M_value(__z) { } - template - basic_istream<_CharT, _Traits>& - operator>>(basic_istream<_CharT, _Traits>&, complex<_Tp>&); + friend class complex; + friend class complex; - template - basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>&, const complex<_Tp>&); + friend complex pow<>(const complex&, int); + friend complex pow<>(const complex&, + const long double&); + friend complex pow<>(const complex&, + const complex&); + friend complex pow<>(const long double&, + const complex&); + friend complex sqrt<>(const complex&); + friend complex tan<>(const complex&); + friend complex tanh<>(const complex&); + }; + inline + complex::complex(long double __r, long double __i) + { + __real__ _M_value = __r; + __imag__ _M_value = __i; + } - // Values: - template - inline _Tp - real(const complex<_Tp>& __z) - { return __z.real(); } - - template - inline _Tp - imag(const complex<_Tp>& __z) - { return __z.imag(); } + inline long double + complex::real() const + { return __real__ _M_value; } - template - 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); - } + inline long double + complex::imag() const + { return __imag__ _M_value; } - template - inline _Tp - arg(const complex<_Tp>& __z) - { return atan2(__z.imag(), __z.real()); } + inline complex& + complex::operator=(long double __r) + { + __real__ _M_value = __r; + __imag__ _M_value = 0.0L; + return *this; + } + + inline complex& + complex::operator+=(long double __r) + { + __real__ _M_value += __r; + return *this; + } + inline complex& + complex::operator-=(long double __r) + { + __real__ _M_value -= __r; + return *this; + } - template - inline complex<_Tp> - polar(const _Tp& __rho, const _Tp& __theta) - { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } + inline complex& + complex::operator*=(long double __r) + { + __real__ _M_value *= __r; + return *this; + } - template - inline complex<_Tp> - conj(const complex<_Tp>& __z) - { return complex<_Tp>(__z.real(), -__z.imag()); } - -// // We use here a few more specializations. -// template<> -// inline complex -// conj(const complex &__x) -// #ifdef _GLIBCPP_BUGGY_FLOAT_COMPLEX -// { -// complex __tmpf(~__x._M_value); -// return __tmpf; -// } -// #else -// { return complex(~__x._M_value); } -// #endif - -// template<> -// inline complex -// conj(const complex &__x) -// { return complex (~__x._M_value); } + inline complex& + complex::operator/=(long double __r) + { + __real__ _M_value /= __r; + return *this; + } - // Transcendentals: template - inline complex<_Tp> - cos(const complex<_Tp>& __z) + inline complex& + complex::operator=(const complex<_Tp>& __z) { - const _Tp __x = __z.real(); - const _Tp __y = __z.imag(); - return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); + __real__ _M_value = __z.real(); + __imag__ _M_value = __z.imag(); + return *this; } template - inline complex<_Tp> - cosh(const complex<_Tp>& __z) + inline complex& + complex::operator+=(const complex<_Tp>& __z) { - const _Tp __x = __z.real(); - const _Tp __y = __z.imag(); - return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); + __real__ _M_value += __z.real(); + __imag__ _M_value += __z.imag(); + return *this; } template - inline complex<_Tp> - exp(const complex<_Tp>& __z) - { return polar(exp(__z.real()), __z.imag()); } - - template - inline complex<_Tp> - log(const complex<_Tp>& __z) - { return complex<_Tp>(log(abs(__z)), arg(__z)); } - - template - inline complex<_Tp> - log10(const complex<_Tp>& __z) - { return log(__z) / log(_Tp(10.0)); } - + inline complex& + complex::operator-=(const complex<_Tp>& __z) + { + __real__ _M_value -= __z.real(); + __imag__ _M_value -= __z.imag(); + return *this; + } + template - inline complex<_Tp> - sin(const complex<_Tp>& __z) + inline complex& + complex::operator*=(const complex<_Tp>& __z) { - const _Tp __x = __z.real(); - const _Tp __y = __z.imag(); - return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); + _ComplexT __t; + __real__ __t = __z.real(); + __imag__ __t = __z.imag(); + _M_value *= __t; + return *this; } template - inline complex<_Tp> - sinh(const complex<_Tp>& __z) + inline complex& + complex::operator/=(const complex<_Tp>& __z) { - const _Tp __x = __z.real(); - const _Tp __y = __z.imag(); - return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); + _ComplexT __t; + __real__ __t = __z.real(); + __imag__ __t = __z.imag(); + _M_value /= __t; + return *this; } + + // These bits have to be at the end of this file, so that the + // specializations have all been defined. + inline + complex::complex(const complex& __z) + : _M_value(_ComplexT(__z._M_value)) { } + + inline + complex::complex(const complex& __z) + : _M_value(_ComplexT(__z._M_value)) { } + + inline + complex::complex(const complex& __z) + : _M_value(_ComplexT(__z._M_value)) { } + + inline + complex::complex(const complex& __z) + { + __real__ _M_value = __z.real(); + __imag__ _M_value = __z.imag(); + } + + inline + complex::complex(const complex& __z) + : _M_value(_ComplexT(__z._M_value)) { } + + inline + complex::complex(const complex& __z) + : _M_value(_ComplexT(__z._M_value)) { } } // namespace std #endif /* _CPP_COMPLEX */ + + + + + diff --git a/libstdc++-v3/include/c/bits/std_cmath.h b/libstdc++-v3/include/c/bits/std_cmath.h index eee7649..307f618 100644 --- a/libstdc++-v3/include/c/bits/std_cmath.h +++ b/libstdc++-v3/include/c/bits/std_cmath.h @@ -296,7 +296,7 @@ namespace std abs(double __x) { return __builtin_fabs(__x); } #else inline double - abs(double __x) { return fabs (__x); } + abs(double __x) { return fabs(__x); } #endif extern "C" double floor(double __x); diff --git a/libstdc++-v3/src/complex.cc b/libstdc++-v3/src/complex.cc index f57aa75..33280e5 100644 --- a/libstdc++-v3/src/complex.cc +++ b/libstdc++-v3/src/complex.cc @@ -27,7 +27,6 @@ // invalidate any other reasons why the executable file might be covered by // the GNU General Public License. -#include #include // This is a ISO C 9X header. @@ -45,62 +44,6 @@ namespace std { -// template<> -// FLT -// abs(const complex& __x) -// { -// // We don't use cabs here because some systems (IRIX 6.5, for -// // example) define their own incompatible version. -// return hypot (__real__ __x._M_value, __imag__ __x._M_value); -// } - -// template<> -// FLT -// arg(const complex& __x) -// { return carg(__x._M_value); } - -// template<> -// complex -// polar(const FLT& __rho, const FLT& __theta) -// { -// #if 0 -// // XXX -// // defined(_GLIBCPP_HAVE_SINCOS) && !defined(__osf__) -// // Although sincos does exist on OSF3.2 and OSF4.0 we cannot use it -// // since the necessary types are not defined in the headers. -// FLT __sinx, __cosx; -// sincos(__theta, &__sinx, &__cosx); -// return complex(__rho * __cosx, __rho * __sinx); -// #else -// return complex(__rho * cos(__theta), __rho * sin(__theta)); -// #endif -// } - -// template<> -// complex -// cos(const complex& __x) -// { return complex(ccos(__x._M_value)); } - -// template<> -// complex -// cosh(const complex& __x) -// { return complex(ccosh(__x._M_value)); } - -// template<> -// complex -// exp(const complex& __x) -// { return complex(cexp(__x._M_value)); } - -// template<> -// complex -// log(const complex& __x) -// { return complex(c_log(__x._M_value)); } - -// template<> -// complex -// log10(const complex& __x) -// { return complex(clog10(__x._M_value)); } - template<> complex pow(const complex& __x, int __n) @@ -121,16 +64,6 @@ namespace std pow(const FLT& __x, const complex& __y) { return complex(cexp(__y._M_value * log(__x))); } -// template<> -// complex -// sin(const complex& __x) -// { return complex(csin(__x._M_value)); } - -// template<> -// complex -// sinh(const complex& __x) -// { return complex(csinh(__x._M_value)); } - template<> complex sqrt(const complex& __x) diff --git a/libstdc++-v3/testsuite/23_containers/vector_ctor.cc b/libstdc++-v3/testsuite/23_containers/vector_ctor.cc index aa9b94e..02f9bdf 100644 --- a/libstdc++-v3/testsuite/23_containers/vector_ctor.cc +++ b/libstdc++-v3/testsuite/23_containers/vector_ctor.cc @@ -1,7 +1,7 @@ // 1999-06-29 // bkoz -// Copyright (C) 1999 Free Software Foundation, Inc. +// Copyright (C) 1999, 2000 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -29,7 +29,7 @@ template struct B { }; -bool test01() +void test01() { // 1 @@ -43,18 +43,25 @@ bool test01() #ifdef DEBUG_ASSERT assert(test); #endif - - return test; } // 2 template class std::vector; template class std::vector< A >; + +// libstdc++/102 +void test02 +{ + std::vector v1; + std::vector v2 (v1); +} + + int main() { test01(); - + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/26_numerics/complex_value.cc b/libstdc++-v3/testsuite/26_numerics/complex_value.cc new file mode 100644 index 0000000..c6105c3 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/complex_value.cc @@ -0,0 +1,64 @@ +// 2000-11-20 +// Benjamin Kosnik bkoz@redhat.com + +// Copyright (C) 2000 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +#include +#include + +void test01() +{ + using namespace std; + bool test = true; + typedef complex complex_type; + const double cd1 = -11.451; + const double cd2 = -442.1533; + + complex_type a(cd1, cd2); + double d; + d = a.real(); + VERIFY( d == cd1); + + d = a.imag(); + VERIFY(d == cd2); + + complex_type c(cd1, cd2); + double d6 = abs(c); + VERIFY( d6 >= 0); + + double d7 = arg(c); + double d8 = atan2(c.imag(), c.real()); + VERIFY( d7 == d8); + + double d9 = norm(c); + double d10 = d6 * d6; + VERIFY(d9 - d10 == 0); + + complex_type e = conj(c); + + complex_type f = polar(c.imag(), 0.0); + VERIFY(f.real() != 0); +} + + +int main() +{ + test01(); + return 0; +} -- 2.7.4