about summary refs log tree commit diff
path: root/lang/cpptour/mycomplex.cc
blob: fade98f9355ca0fd52e07e14e9dbc6ba03d50485 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "mycomplex.h"

constexpr double
square (double x)
{
  return x * x;
}

complex&
operator*= (complex& a, complex b)
{
  double r = a.real() * b.real() - a.imag() * b.imag();
  double i = a.real() * b.imag() + a.imag() * b.real();
  a.real(r);
  a.imag(i);
  return a;
}

complex&
operator/= (complex& a, complex b)
{
  double d = square(b.real()) + square(b.imag());
  complex c {b.real() / d, -b.imag() / d};
  return a *= c;
}

complex
operator+ (complex a, complex b)
{
  return a += b;
}

complex
operator- (complex a, complex b)
{
  return a -= b;
}

complex
operator- (complex a)
{
  return {-a.real(), -a.imag()};
}

complex
operator* (complex a, complex b)
{
  return a *= b;
}

complex
operator/ (complex a, complex b)
{
  return a /= b;
}

bool
operator== (complex a, complex b)
{
  return a.real() == b.real() && a.imag() == b.imag();
}

bool
operator!= (complex a, complex b)
{
  return !(a == b);
}