View source code
Display the source code in std/complex.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.complex.Complex/complex - multiple declarations

Function complex

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

auto auto complex(R) (
  R re
) pure nothrow @nogc @safe
if (is(R : double));

auto auto complex(R, I) (
  R re,
  I im
) pure nothrow @nogc @safe
if (is(R : double) && is(I : double));


R (template parameter) type of real part of complex number
I (template parameter) type of imaginary part of complex number
re real part of complex number to be constructed
im (optional) imaginary part of complex number, 0 if omitted.


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 CommonType!(R, I).


auto a = complex(1.0);
static assert(is(typeof(a) == Complex!double));
writeln(; // 1.0
writeln(; // 0.0

auto b = complex(2.0L);
static assert(is(typeof(b) == Complex!real));
writeln(; // 2.0L
writeln(; // 0.0L

auto c = complex(1.0, 2.0);
static assert(is(typeof(c) == Complex!double));
writeln(; // 1.0
writeln(; // 2.0

auto d = complex(3.0, 4.0L);
static assert(is(typeof(d) == Complex!real));
writeln(; // 3.0
writeln(; // 4.0L

auto e = complex(1);
static assert(is(typeof(e) == Complex!double));
writeln(; // 1
writeln(; // 0

auto f = complex(1L, 2);
static assert(is(typeof(f) == Complex!double));
writeln(; // 1L
writeln(; // 2

auto g = complex(3, 4.0L);
static assert(is(typeof(g) == Complex!real));
writeln(; // 3
writeln(; // 4.0L

Struct Complex

A complex number parametrised by a type T, which must be either float, double or real.

struct Complex(T)
if (isFloatingPoint!T);


this Construct a complex number with the specified real and imaginary parts. In the case where a single argument is passed that is not complex, the imaginary part of the result will be zero.


im TThe imaginary part of the number.
re TThe real part of the number.


toString Converts the complex number to a string representation.


Lars Tandle Kyllingstad, Don Clugston


Boost License 1.0