summaryrefslogtreecommitdiff
path: root/gcc/d/dmd/root/complex.d
blob: d9a396d9677b7885c532cc77ba102c168d1c97e5 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * Implements a complex number type.
 *
 * Copyright:   Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
 * Authors:     $(LINK2 https://www.digitalmars.com, Walter Bright)
 * License:     $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
 * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/complex.d, _complex.d)
 * Documentation:  https://dlang.org/phobos/dmd_root_complex.html
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/complex.d
 */

module dmd.root.complex;

import dmd.root.ctfloat;

extern (C++) struct complex_t
{
    real_t re;
    real_t im;

    this() @disable;

    this(real_t re)
    {
        this(re, CTFloat.zero);
    }

    this(real_t re, real_t im)
    {
        this.re = re;
        this.im = im;
    }

    extern (D) complex_t opBinary(string op)(complex_t y)
        if (op == "+")
    {
        return complex_t(re + y.re, im + y.im);
    }

    extern (D) complex_t opBinary(string op)(complex_t y)
        if (op == "-")
    {
        return complex_t(re - y.re, im - y.im);
    }

    extern (D) complex_t opUnary(string op)()
        if (op == "-")
    {
        return complex_t(-re, -im);
    }

    extern (D) complex_t opBinary(string op)(complex_t y)
        if (op == "*")
    {
        return complex_t(re * y.re - im * y.im, im * y.re + re * y.im);
    }

    extern (D) complex_t opBinaryRight(string op)(real_t x)
        if (op == "*")
    {
        return complex_t(x) * this;
    }

    extern (D) complex_t opBinary(string op)(real_t y)
        if (op == "*")
    {
        return this * complex_t(y);
    }

    extern (D) complex_t opBinary(string op)(real_t y)
        if (op == "/")
    {
        return this / complex_t(y);
    }

    extern (D) complex_t opBinary(string op)(complex_t y)
        if (op == "/")
    {
        if (CTFloat.fabs(y.re) < CTFloat.fabs(y.im))
        {
            const r = y.re / y.im;
            const den = y.im + r * y.re;
            return complex_t((re * r + im) / den, (im * r - re) / den);
        }
        else
        {
            const r = y.im / y.re;
            const den = y.re + r * y.im;
            return complex_t((re + r * im) / den, (im - r * re) / den);
        }
    }

    extern (D) bool opCast(T : bool)() const
    {
        return re || im;
    }

    int opEquals(complex_t y) const
    {
        return re == y.re && im == y.im;
    }
}

extern (C++) real_t creall(complex_t x)
{
    return x.re;
}

extern (C++) real_t cimagl(complex_t x)
{
    return x.im;
}