Math Utils (MT4) - MetaTrader 4 Library | MT4 EA Download - MetaTrader 4 Resources
integer comparison( double one, double b, integer number = 8); boolean EQ( double one, double b, integer number = 8 ); boolean NE( double one, double b, integer number = 8 ); boolean gt( double one, double b, integer number = 8 ); boolean LT( double one, double b, integer number = 8 ) ); boolean GTE( double a, double b, integer number = 8 ); boolean LTE( double a, double b, integer number = 8 ); boolean almost equal to ( double a, double b, integer significant digits = 8); boolean equal to double( double a, double b, integer significant digits = 15 ); boolean OFF( double a, double b, integer max distinct significant digits = 2 ) ); integer gets equal numbers ( double one, double b); integer gets EqualSigDigits ( double one, double b);
Convenient double precision rounding function
double ceiling( const double five); double ceiling( const double value , constant integer number); double ceiling( const double value , constant double step); double floor( const double value) double ground( const double value , const integer number); double ground( const double value ,const double step);double round(const double five ) ; double round( const double five) double the value , constant integer number); double rounded ( const double the value , constant double the step); double truncation ( const double the value, constant integer number); double truncation ( const double the value , constant integer number) ; double truncation ( const double value , const double step); double round to significant figures ( const double value , integer number); double round price ( double p price, string pSymbol = NULL); double round volume ( double p volume, string pSymbol = NULL); double strip error ( const double value );
Various convenient functions for doubles
// Determine whether the passed in value is an integer. Boolean is an integer ( double the value ); // Checks whether a number has the specified number of decimal places. boolean is round ( double value , integer number); // Checks if a number is an integer multiple of a certain increment. Boolean is round ( double value , double steps); // Get the fractional part of x (always has the same sign as x) Double the fracturing ( double the value ); // Get the number of decimal places after the decimal point. Integer Gets the number ( double the value ); // Gets the number of integer digits to the left of the decimal point. Integer Gets an integer number ( double the value ); // Gets the number of significant digits, excluding leading and trailing zeros. Integer Gets significant digits ( double the value ); // Convert a value in the range [min, max] to y in another range [destMin, destMax]. double remap ( double value , double minutes, double max, double destination minutes, double target max) // Remap values on scale [min, max] to normalized scale [0, 1]. Double normalization ( double value , double minutes, double maximum); // Remap normalized values [0, 1] to scales [destMin, destMax]. Double denormalization ( double the value , double the destination minutes, double the destination max) // Clamp (limit) the number to a boundary (range). T clamp(T value , T min value, T max value); // Clamp (limit) the number to the maximum value. T ClampTop(T value , T maximum); // Clamp (limit) the number to the minimum value. T ClampBot(T value , T minutes); // Wraps a value in a range between [min, max]. T wrap(T value , T min value, T max value); // Avoid divide-by-zero errors that force the mql program to stop. Double safe Div( double a, double b); // Returns the scientific notation exponent of the number. integer floor log 10 ( double the value ); // Returns pow(10, (int)power), using a fast lookup table to find the power. Double the power obtained by 10 ( integer power); // The sign of the calculated value is 1, 0, -1 Double mathematical symbols ( double value );
// Returns the bit representation corresponding to a double value. long double to long bit ( double the value ); // Returns the double value corresponding to the bit representation. Double LongBitsToDouble( long bits); // Returns the original encoding of the exponent in the bit representation. integer raw exponent ( double the value ); // Returns the raw encoding of the significand in bit representation. long raw significant digits ( double's value ); // Returns the unbiased (adjusted) exponent of a double value. integer Gets the exponent ( the value of a double ); // Returns the significand of a double value. long Gets the significant digit ( double's value ); // Determines whether the number can be represented exactly as a double. Boolean IsExactDouble( double's value ); // Returns the next representable value away from zero after x. Double Next After ( double the value ); // Advance the float by the specified number of ULPs. Double double advance ( double value , long distance); // Return the size of the ulp parameter. double ulp( double the value ); // Returns the difference between two floating point numbers in ulp units. long UlpDiff( double value 1, double value 2);
// Convert the numeric value to a raw hexadecimal text string. String double to hexadecimal ( double the value ); // Convert a numeric value to a hexadecimal floating point constant string. String DoubleToHexFloatConstant( double's value ); // Convert a numeric value to an exact decimal text string. String DoubleToStringExact( double's value ); // Convert a numeric value to a string in scientific notation. StringDoubleToExponential ( const doubleValue , integer number = - 1 ); // Convert a numeric value to its shortest round-trip string representation. string represents ( double's value ); string represents ( float's value ); // Format a double using the thousands separator and the specified decimal. string format double( const double number, const integer number, const string separator = "," );
Sometimes, when comparing two double-precision numbers that are assumed to be equal but were calculated differently, the comparison goes wrong. In fact, the difference between A and B is very small (16th decimal place) due to binary rounding error, so exact comparisons ( == , != , > , >= , < , <= operators) fail.
Compare doubles derived from different calculations:
if (0.1+0.2 == 0.3) // falseExperts with Trailing Stop:
if (new_sl> Order Stop ()) if (new_sl< Order Stop ())
Expert with hidden take profit function:
if ( bid >= hide take profit) if ( ask <= hide take profit)
Compare price levels in a grid strategy:
if ( ask < virtual stop) if ( bid > virtual stop)
To avoid unexpected results, it is better to replace exact comparisons ( == , != , > , >= , < , <= ) with loose comparisons to overcome floating point inaccuracies. This small library provides the required functionality.
//--- Exact comparison may fail if (required < virtual_stopLoss) if (bid > virtual_stopLoss) if ( 0.1 + 0.2 == 0.3 ) // false //--- Using library functions, the results are as expected if (LT(ask, virtual_stopLoss)) if (GT(bid, virtual_stopLoss)) if (EQ ( 0.1 + 0.2 , 0.3 )) // true
Attachment download
📎 debug_demo.mq4 (4.63 KB)
📎 math_test.mq4 (29.21 KB)
📎 normalizedouble_errors.mq4 (2.51 KB)
📎 pitfalls.mq4 (3.91 KB)
📎unnormalized_quotes.mq4 (6.17 KB)
📎 math_utils.mqh (72.99 KB)
Source: MQL5 #25723
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •