1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//
5// complex.h
6//
7
8//
9// Defines a basic complex number data type. We cannot use the standard C++ library's
10// complex implementation, because the CLR links to the wrong CRT.
11//
12
13#ifndef _COMPLEX_H_
14#define _COMPLEX_H_
15
16#include <math.h>
17
18//
19// Default compilation mode is /fp:precise, which disables fp intrinsics. This causes us to pull in FP stuff (sqrt,etc.) from
20// The CRT, and increases our download size. We don't need the extra precision this gets us, so let's switch to
21// the intrinsic versions.
22//
23#ifdef _MSC_VER
24#pragma float_control(precise, off, push)
25#endif
26
27
28class Complex
29{
30public:
31 double r;
32 double i;
33
34 Complex() : r(0), i(0) {}
35 Complex(double real) : r(real), i(0) {}
36 Complex(double real, double imag) : r(real), i(imag) {}
37 Complex(const Complex& other) : r(other.r), i(other.i) {}
38};
39
40inline Complex operator+(Complex left, Complex right)
41{
42 LIMITED_METHOD_CONTRACT;
43 return Complex(left.r + right.r, left.i + right.i);
44}
45
46inline Complex operator-(Complex left, Complex right)
47{
48 LIMITED_METHOD_CONTRACT;
49 return Complex(left.r - right.r, left.i - right.i);
50}
51
52inline Complex operator*(Complex left, Complex right)
53{
54 LIMITED_METHOD_CONTRACT;
55 return Complex(
56 left.r * right.r - left.i * right.i,
57 left.r * right.i + left.i * right.r);
58}
59
60inline Complex operator/(Complex left, Complex right)
61{
62 LIMITED_METHOD_CONTRACT;
63 double denom = right.r * right.r + right.i * right.i;
64 return Complex(
65 (left.r * right.r + left.i * right.i) / denom,
66 (-left.r * right.i + left.i * right.r) / denom);
67}
68
69inline double abs(Complex c)
70{
71 LIMITED_METHOD_CONTRACT;
72 return sqrt(c.r * c.r + c.i * c.i);
73}
74
75#ifdef _MSC_VER
76#pragma float_control(pop)
77#endif
78
79
80#endif //_COMPLEX_H_
81