complex

Helper function that returns a complex number with the specified real and imaginary parts.

  1. auto complex(R re)
    @safe pure nothrow @nogc
    complex
    (
    R
    )
    (
    const R re
    )
    if (
    is(R : double)
    )
  2. auto complex(R re, I im)

Parameters

R

(template parameter) type of real part of complex number

re R

real part of complex number to be constructed

Return Value

Type: auto

Complex instance with real and imaginary parts set to the values provided as input. If neither re nor im are floating-point numbers, the return type will be Complex!double. Otherwise, the return type is deduced using std.traits.CommonType!(R, I).

Examples

1 auto a = complex(1.0);
2 static assert(is(typeof(a) == Complex!double));
3 assert(a.re == 1.0);
4 assert(a.im == 0.0);
5 
6 auto b = complex(2.0L);
7 static assert(is(typeof(b) == Complex!real));
8 assert(b.re == 2.0L);
9 assert(b.im == 0.0L);
10 
11 auto c = complex(1.0, 2.0);
12 static assert(is(typeof(c) == Complex!double));
13 assert(c.re == 1.0);
14 assert(c.im == 2.0);
15 
16 auto d = complex(3.0, 4.0L);
17 static assert(is(typeof(d) == Complex!real));
18 assert(d.re == 3.0);
19 assert(d.im == 4.0L);
20 
21 auto e = complex(1);
22 static assert(is(typeof(e) == Complex!double));
23 assert(e.re == 1);
24 assert(e.im == 0);
25 
26 auto f = complex(1L, 2);
27 static assert(is(typeof(f) == Complex!double));
28 assert(f.re == 1L);
29 assert(f.im == 2);
30 
31 auto g = complex(3, 4.0L);
32 static assert(is(typeof(g) == Complex!real));
33 assert(g.re == 3);
34 assert(g.im == 4.0L);

Meta

Suggestion Box / Bug Report