Sync usage with man page.
[netbsd-mini2440.git] / gnu / lib / libg++ / g++-include / Complex.h
bloba16a274a1b5b46fa2e06cd26bf1ebfc0190805e2
1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
6 This file is part of GNU CC.
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU CC General Public
13 License for full details.
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License. A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies.
24 #ifndef _Complex_h
25 #ifdef __GNUG__
26 #pragma once
27 #pragma interface
28 #endif
29 #define _Complex_h 1
32 #include <stream.h>
33 #include <math.h>
35 class Complex
37 #ifdef __ATT_complex__
38 public:
39 #else
40 protected:
41 #endif
43 double re;
44 double im;
46 public:
48 double real() const;
49 double imag() const;
51 Complex();
52 Complex(const Complex& y);
53 Complex(double r, double i=0);
55 ~Complex();
57 Complex& operator = (const Complex& y);
59 Complex& operator += (const Complex& y);
60 Complex& operator += (double y);
61 Complex& operator -= (const Complex& y);
62 Complex& operator -= (double y);
63 Complex& operator *= (const Complex& y);
64 Complex& operator *= (double y);
66 Complex& operator /= (const Complex& y);
67 Complex& operator /= (double y);
69 void error(const char* msg) const;
73 // error handlers
75 extern void default_Complex_error_handler(const char*);
76 extern one_arg_error_handler_t Complex_error_handler;
78 extern one_arg_error_handler_t
79 set_Complex_error_handler(one_arg_error_handler_t f);
82 // non-inline functions
84 Complex operator / (const Complex& x, const Complex& y);
85 Complex operator / (const Complex& x, double y);
86 Complex operator / (double x, const Complex& y);
88 Complex cos(const Complex& x);
89 Complex sin(const Complex& x);
91 Complex cosh(const Complex& x);
92 Complex sinh(const Complex& x);
94 Complex exp(const Complex& x);
95 Complex log(const Complex& x);
97 Complex pow(const Complex& x, long p);
98 Complex pow(const Complex& x, const Complex& p);
99 Complex pow(const Complex& x, double y);
100 Complex sqrt(const Complex& x);
102 istream& operator >> (istream& s, Complex& x);
103 ostream& operator << (ostream& s, const Complex& x);
105 // other functions defined as inlines
107 int operator == (const Complex& x, const Complex& y);
108 int operator == (const Complex& x, double y);
109 int operator != (const Complex& x, const Complex& y);
110 int operator != (const Complex& x, double y);
112 Complex operator - (const Complex& x);
113 Complex conj(const Complex& x);
114 Complex operator + (const Complex& x, const Complex& y);
115 Complex operator + (const Complex& x, double y);
116 Complex operator + (double x, const Complex& y);
117 Complex operator - (const Complex& x, const Complex& y);
118 Complex operator - (const Complex& x, double y);
119 Complex operator - (double x, const Complex& y);
120 Complex operator * (const Complex& x, const Complex& y);
121 Complex operator * (const Complex& x, double y);
122 Complex operator * (double x, const Complex& y);
124 double real(const Complex& x);
125 double imag(const Complex& x);
126 double abs(const Complex& x);
127 double norm(const Complex& x);
128 double arg(const Complex& x);
130 Complex polar(double r, double t = 0.0);
132 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
134 // inline members
136 inline double Complex::real() const { return re; }
137 inline double Complex::imag() const { return im; }
139 inline Complex::Complex() {}
140 inline Complex::Complex(const Complex& y) :re(y.real()), im(y.imag()) {}
141 inline Complex::Complex(double r, double i) :re(r), im(i) {}
143 inline Complex::~Complex() {}
145 inline Complex& Complex::operator = (const Complex& y)
147 re = y.real(); im = y.imag(); return *this;
150 inline Complex& Complex::operator += (const Complex& y)
152 re += y.real(); im += y.imag(); return *this;
155 inline Complex& Complex::operator += (double y)
157 re += y; return *this;
160 inline Complex& Complex::operator -= (const Complex& y)
162 re -= y.real(); im -= y.imag(); return *this;
165 inline Complex& Complex::operator -= (double y)
167 re -= y; return *this;
170 inline Complex& Complex::operator *= (const Complex& y)
172 double r = re * y.real() - im * y.imag();
173 im = re * y.imag() + im * y.real();
174 re = r;
175 return *this;
178 inline Complex& Complex::operator *= (double y)
180 re *= y; im *= y; return *this;
184 // functions
186 inline int operator == (const Complex& x, const Complex& y)
188 return x.real() == y.real() && x.imag() == y.imag();
191 inline int operator == (const Complex& x, double y)
193 return x.imag() == 0.0 && x.real() == y;
196 inline int operator != (const Complex& x, const Complex& y)
198 return x.real() != y.real() || x.imag() != y.imag();
201 inline int operator != (const Complex& x, double y)
203 return x.imag() != 0.0 || x.real() != y;
206 inline Complex operator - (const Complex& x)
208 return Complex(-x.real(), -x.imag());
211 inline Complex conj(const Complex& x)
213 return Complex(x.real(), -x.imag());
216 inline Complex operator + (const Complex& x, const Complex& y)
218 return Complex(x.real() + y.real(), x.imag() + y.imag());
221 inline Complex operator + (const Complex& x, double y)
223 return Complex(x.real() + y, x.imag());
226 inline Complex operator + (double x, const Complex& y)
228 return Complex(x + y.real(), y.imag());
231 inline Complex operator - (const Complex& x, const Complex& y)
233 return Complex(x.real() - y.real(), x.imag() - y.imag());
236 inline Complex operator - (const Complex& x, double y)
238 return Complex(x.real() - y, x.imag());
241 inline Complex operator - (double x, const Complex& y)
243 return Complex(x - y.real(), -y.imag());
246 inline Complex operator * (const Complex& x, const Complex& y)
248 return Complex(x.real() * y.real() - x.imag() * y.imag(),
249 x.real() * y.imag() + x.imag() * y.real());
252 inline Complex operator * (const Complex& x, double y)
254 return Complex(x.real() * y, x.imag() * y);
257 inline Complex operator * (double x, const Complex& y)
259 return Complex(x * y.real(), x * y.imag());
262 inline double real(const Complex& x)
264 return x.real();
267 inline double imag(const Complex& x)
269 return x.imag();
272 inline double abs(const Complex& x)
274 return hypot(x.real(), x.imag());
277 inline double norm(const Complex& x)
279 return (x.real() * x.real() + x.imag() * x.imag());
282 inline double arg(const Complex& x)
284 return atan2(x.imag(), x.real());
287 inline Complex polar(double r, double t)
289 return Complex(r * cos(t), r * sin(t));
292 #endif __OPTIMIZE__
293 #endif