|
Real Types (double, float)
Real types (or floating-point types) represent values with a fractional part. In the MQL5 language there are two types for floating point numbers.The method of representation of real numbers in the computer memory is defined by the IEEE 754 standard and is independent of platforms, operating systems or programming languages.
Type |
Size in bytes |
Minimal Positive Value |
Maximum Value |
C++ Analog |
---|---|---|---|---|
float |
4 |
1.175494351e-38 |
3.402823466e+38 |
float |
double |
8 |
2.2250738585072014e-308 |
1.7976931348623158e+308 |
double |
double
double real number type occupies 64 bits (1 sign bit, 11 exponent bits and 52 mantissa bits).
float
float real number type occupies 32 bits (1 sign bit, 8 exponent bits and 23 mantissa bits).
vector
One-dimensional array of double type numbers. Memory for data is allocated dynamically. Vector properties can be obtained using the methods, while the vector size can be changed. The vector<double> entry can be used in template functions.
vectorf
One-dimensional array of float type numbers can be used instead of vector if the loss of precision does not matter. The vector<float> entry can be used in template functions.
vectorc
One-dimensional array of complex type numbers is meant to handle complex numbers. The vector<complex> entry can be used in template functions. Operations on vectorc type vectors are not implemented yet.
matrix
Matrix is a two-dimensional array of double type numbers. Memory for matrix elements is distributed dynamically. Matrix properties can be obtained using the methods, while the matrix shape can be changed. The matrix<double> entry can be used in template functions.
matrixf
Two-dimensional array of float type numbers can be used instead of matrix if the loss of precision does not matter. The matrix<float> 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. Operations on matrixc type matrices are not implemented yet.
The double name means that the accuracy of these numbers is twice the accuracy of the float type numbers. In most cases, the double type is the most convenient one. In many cases the limited precision of float numbers is not enough. The reason why the float type is still used is saving the memory (this is important for large arrays of real numbers).
Floating-point constants consist of an integer part, a point (.) and the fractional part. The integer and fractional parts are sequences of decimal digits.
Examples:
double a=12.111;
|
There is a scientific way of writing real constants, often this method of recording is more compact than the traditional one.
Example:
double c1=1.12123515e-25;
|
It should be remembered that real numbers are stored in memory with some limited accuracy in the binary system, while generally the decimal notation is used. That's why many numbers that are precisely represented in the decimal system can be written only as an infinite fraction in the binary system.
For example, numbers 0.3 and 0.7 are represented in the computer as infinite fractions, while the number of 0.25 is stored exactly, because it represents the power of two.
In this regard, it is strongly recommended not to compare two real numbers for equality, because such a comparison is not correct.
Example:
void OnStart()
|
If you still need to compare the equality of two real numbers, then you can do this in two different ways. The first way is to compare the difference between two numbers with some small quantity that specifies the accuracy of comparison.
Example:
bool EqualDoubles(double d1,double d2,double epsilon)
|
Note that the value of epsilon in the above example can not be less than the predefined constant DBL_EPSILON. The value of this constant is 2.2204460492503131e-016. The constant corresponding to the float type is FLT_EPSILON = 1.192092896e-07. The meaning of these values is the following: it is the lowest value that satisfies the condition 1.0 + DBL_EPSILON! = 1.0 (for numbers of float type 1.0 + FLT_EPSILON! = 1.0).
The second way offers comparing the normalized difference of two real numbers with zero. It's meaningless to compare the difference of normalized numbers with a zero, because any mathematical operation with normalized numbers gives a non-normalized result.
Example:
bool CompareDoubles(double number1,double number2)
|
Some operations of the mathematical co-processor can result in the invalid real number, which can't be used in mathematical operations and operations of comparison, because the result of operations with invalid real numbers is undefined. For example, when trying to calculate the arcsine of 2, the result is the negative infinity.
Example:
double abnormal = MathArcsin(2.0);
|
Besides the minus infinity there is the plus infinity and NaN (not a number). To determine that this number is invalid, you can use MathIsValidNumber(). According to the IEEE standard, they have a special machine representation. For example, plus infinity for the double type has the bit representation of 0x7FF0 0000 0000 0000.
Examples:
struct str1
|
See also