Complex number (complex, complexf)

The built-in complex type is a structure with two double fields:

struct complex
  {
   double             real;   // Real part
   double             imag;   // Imaginary part
  };

The built-in complexf type is a structure with two float fields:

struct complexf
  {
   float              real;   // Real part
   float              imag;   // Imaginary part
  };

 

The complex and complexf types can be passed by value as parameters for MQL5 functions (in contrast to ordinary structures, which are only passed by reference). For functions imported from DLLs, the complex and complexf types must be passed only by reference.

The 'i' suffix is used to describe complex constants:

complex square(complex c)
  {
   return(c*c);
  }  
void OnStart()
  {
   Print(square(1+2i));  // A constant is passed as a parameter
  }
// "(-3,4)" will be output, which is a string representation of the complex number 

Only simple operations are currently available for complex numbers: =, +, -, *, /, +=, -=, *=, /=, ==,!=.

Support for additional mathematical functions will be added later, enabling the calculation of the absolute value, sine, cosine and others.

vectorc

One-dimensional array of complex type numbers is meant to handle complex numbers. The vector<complex> entry can be used in template functions.

matrixc

Two-dimensional array of complex type numbers is meant to handle complex numbers. The matrix<complex> entry can be used in template functions.

vectorcf

One-dimensional array of complexf type numbers is meant to handle complex numbers. The vector<complexf> entry can be used in template functions.

matrixcf

Two-dimensional array of complexf type numbers is meant to handle complex numbers. The matrix<complexf> entry can be used in template functions.