MQL4 Book  Standard Functions  Mathematical Functions

Mathematical Functions

Mathematical and trigonometric functions are included in MQL4. There are no difficulties in using most of them. For example, the MathMax() function returns the maximum value of two values specified in the list of parameters of the function. The usage of other functions claims certain attention and thoughtfulness. Let's examine one of the functions of this kind.

MathFloor() Function

 double MathFloor(double x)

The function returns a numeric value that corresponds the greatest integer that is less or equal to x.


x - numeric value.

Note that a value returned by the function is the real number (double type), at the same time, it is written that the function returns an integer. It should be realized that the function returns a real number that has all the positions equal to zero after the decimal point. For example, the MathFloor() function may return 37.0 (positive number of the double type) or -4.0 (negative number of the double type).

The description says also that the function returns the maximum of possible numbers that is less than a specified one. For example, if the value of the given x parameter is 13.5 then the maximum number that has zeros after the decimal point is 13.0. Or if the -13.5 negative value is specified in the function, then the maximum smallest integer is equal to -14.0. In such a manner, modification of the sign of the passed to the function value leads to the different results, namely the received values are not equal to each other in absolute magnitude.

Usage of such functions is very convenient, in some cases. Let's examine the fragment of the lots amount calculation for new orders as an example:

int Percent   =30;                                  // % of free margin
double Free =AccountFreeMargin(); // Free margin
double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);// 1 lot price
double Step =MarketInfo(Symb,MODE_LOTSTEP); // Size step changed

double Lots_New=MathFloor(Free*Percent/100/One_Lot/Step)*Step;

The value of the Percent parameter is set by user. In this case, the user specified 30% of free margin for new orders. According to the rules that are specified by the dealing center, the correctly calculated amount of lots must be divisible by the minimum step of lots changing size (Step). The values of free margin (Free) and 1 lot price (One_Lot) are necessary for calculation too.

Let's examine the logic of reasoning of the programmer that compiled the expression to calculate the required amount of lots Lots_New for new orders. Let's use the numeric values of the variables for better visualization. Let Free=5000.0, One_Lot=1360.0 (In most of the dealing centers the cost of 1 lot of currency pair is in proportion to the cost of the symbol), Step=0.1. In this case, the expression to calculate Lots_New can be written as follows:

Lots_New = MathFloor(5000.0*30/100/1360.0/0.1)*0.1;

The 5000.0*30/100 expression is the value of money the user laid out to open a new order. In this case, the price of a new order may reach the 1500.0. Spending all these money you can open one new order that has the 1500.0 / 1360.0 = 1.102941 amount of lots. However, the dealing center won't accept the order with this amount of lots, because the minimum Step=0.1 (in the most dealing centers). To calculate the desired amount of lots you should throw away all the "needless" digits in the decimal part and replace them with zeros.

In order to do it you can use the considered mathematical function:

Lots_New = MathFloor(1.102941/0.1)*0.1;

The calculated value of MathFloor(1.102941/0.1) will be 11.0, and the calculated value of the Lots_New variable will be 1.1 lots. This value meets the requirements of the dealing center and so can be used as the declared amount of lots in new orders.

Mathematical Functions

Function Summary Info
MathAbs The function returns the absolute value (in absolute magnitude) of a given number.
MathArccos The function returns the arccosine value of x in the 0 to π radians range. If x is lesser than -1 or greater than 1, the function returns NaN (undefined value).
MathArcsin The function returns arcsine value of x in the -π/2 to π/2 radians range. If x i less than -1 or greater than 1, the function returns NaN (undefined value).
MathArctan The function returns arctangent of x. If x is equal to 0, then the function returns 0. MathArctan returns the value in the -π/2 to π/2 radians range.
MathCeil The function returns the numeric value that is the smallest integer bigger or equal to x.
MathCos The function returns the cosine of the angle.
MathExp The function returns the value of e to the power of d. At overflow, the function returns INF (infinity), and it returns 0 at underflow.
MathFloor The function returns the numeric value representing the largest integer that is less than or equal to x.
MathLog The function returns the natural logarithm of x if successful. If x is negative, this function returns NaN (undefined value). If x is 0, it returns INF (infinity).
MathMax The function returns the maximum value of two numeric values.
MathMin The function returns the minimum value of two numeric values.
MathMod The function returns the floating-point remainder of division of two numbers. The MathMod function calculates the floating-point remainder f of x / y such that 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.
MathPow Returns the value of the base expression raised to the specified power (exponent value).
MathRand The function returns a pseudorandom integer within the range of 0 to 32767. The MathSrand function must be used to seed the pseudorandom-number generator before calling MathRand.
MathRound The function returns value rounded to the nearest integer of the specified numeric value.
MathSin The function returns the sine of the specified angle.
MathSqrt The function returns the square root of x. If x is negative, MathSqrt returns an indefinite (same as a quiet NaN).
MathSrand The function sets the starting point for generating a series of pseudorandom integers.
MathTan MathTan returns the tangent of x. If x is greater than or equal to 263, or less than or equal to -263, a loss of significance in the result occurs. In this case, the function returns an indefinite value.

To get the detailed information about these and other functions, please refer to the Documentation at, at MetaQuotes Ltd. website or at the "Help" section of MetaEditor.