MQL4 mathematics and trigonometric function (2)

  • MathAbs()- absolute value
  • MathArccos()- find inverse cosine
  • MathArcsin()- find the inverse sine
  • MathArctan()- find arctangent
  • MathCeil()- take the smallest integer
  • MathCos()- find cosine
  • MathExp()- find the power of e
  • MathFloor()- take the largest integer
  • MathLog()- find natural logarithm
  • MathMax()- Maximum
  • MathMin()- Minimum
  • MathMod()- modulus
  • MathPow()- exponentiation
  • MathRand()- get random integer
  • MathRound()- rounding value
  • MathSin()- find sine
  • MathSqrt()- square root
  • MathSrand()- random number setting
  • MathTan()- tangent
Mathmax() – maximum
double MathMax(double value1, double value2)

Returns the maximum of two values.


Value1 - the first value. Value2 - the second value.


double result=MathMax(1.08,Bid);

Mathmin() – minimum
double MathMin(double value1, double value2)

Returns the smallest of two values.


Value1 - the first value. Value2 - the second value.


double result=MathMin(1.08,Ask);

Mathmod() – modulus
double MathMod(double value, double value2)

This function returns the floating-point remainder of dividing two numbers.

The mathmod function calculates the floating-point remainder F of X / y, so x = I * y + F, where I is an integer, f has the same sign as X, and the absolute value of F is less than the absolute value of Y.


Value1 - dividend. Value2 - divisor.


double x=-10.0,y=3.0,z;  z=MathMod(x,y);

Print ("remainder", x, "/", y, "is", z);

//Output data - the remainder of - 10 / 3 is - 1

Mathpow() - exponentiation
double MathPow(double base, double exponent)

Returns the value of the radix to the specified power.


Base - cardinality. Exponent - exponential value.


double x=2.0,y=3.0,z;  z=MathPow(x,y);

Printf (x, "of", y, "the power is", z);

//Output data: the third power of 2 is 8

Mathrand() – gets a random integer
int MathRand()

The mathrand function returns a pseudo-random integer in the range of 0 to 32767. You need to use the mathsrand() function before calling mathrand.



//Display 10 numbers. For (int i = 0; I < 10; I + +) print ("random number", mathrand());

Mathround() – round off
double MathRound(double value)

Returns an integer rounded to the nearest specified value.


Value - the value to be rounded.


double y=MathRound(2.8);

Print("The round of 2.8 is ",y);


Print("The round of -2.4 is ",y);

//Output: the round of 2.8 is 3 / / the round of - 2.4 is - 2

Mathsin () - find sine
double MathSin(double value)

Returns the sine of the specified angle.


Value - the angle value in radians.


double pi=3.1415926535;  double x, y;  x=pi/2;  y=MathSin(x);

Print("MathSin(",x,") = ",y);


Print("MathCos(",x,") = ",y);

//Output: mathsin (1.5708) = 1 / / mathcos (1.5708) = 0

Mathsqrt() – square root
double MathSqrt(double x)

The mathsgrt function returns the square root of X. If x is negative, infinity (Nan) is returned.


X - the value of the square root.


double question=45.35, answer;  answer=MathSqrt(question);

if(question<0) Print("Error: MathSqrt returns ",answer," answer");

else Print("The square root of ",question," is ", answer);

//Output: the square root of 45.35 is 6.73

Mathsrand() – random number setting
void MathSrand(int seed)

The mathsrand() function sets a starting point for generating a set of pseudo-random integers. To reinitialize the random number generator, use 1 as the seed. With other values as seeds, the generator can be set to a random starting point. Use mathrand to retrieve the generated pseudo-random number. Before calling mathrand(), calling the mathrand function and calling the mathrand function with 1 as the seed generate the same random number.


Seed - seeds that generate random numbers.



//Display 10 random numbers. For (int i = 0; I < 10; I + +) print ("random value", mathrand());

Mathtan() – tangent
double MathTan(double x)

Mathtan returns the tangent of X. If x is greater than or equal to 263, or X is less than or equal to - 263, the function returns infinity (Nan)


X - the angle value in radians.


double pi=3.1415926535;  double x,y;  x=MathTan(pi/4);

Print("MathTan(",pi/4," = ",x);

//Output: mathtan (0.7856) = 1

    Was this article helpful?

    1 out of 1 found this helpful