Libraries: Math Utils (MT4) - page 2

 
Update 17 June 2021

Added new functions:
// Determines whether the passed value is an integer.
bool   IsInteger(double num);

// Get decimal part (always positive)
double GetFraction(double num);

// Clipping (limiting) a number to boundaries (range)
template<typename T>
T      Clip(T value, T low, T high);

// In mql, zero divide error forces the mql program to stop.
double safeDiv(double x, double y);

// Converting numeric value into the hex float constant string.
string DoubleToHexFloatConstant(double value);
 

Update 20 June 2021

Added new function:

// Advances a floating-point number by a specified number of ULPs.
double DoubleAdvance(double value, long distance);
 

Update 21 June 2021

Added new function-like macro:

// Print doubles using the round-trip ("%.17g") format specifier.
#define PRINT_R(A) Print(#A + " = ", Repr(A))


 

Update 27 January 2023

Renamed the rounding functions to be more consistent:

double MathRound(const double value, const int digits);

double MathRound(const double value, const double step);

double MathFloor(const double value, const int digits);

double MathFloor(const double value, const double step);

double MathCeil(const double value, const int digits);

double MathCeil(const double value, const double step);

double MathTrunc(const double value, const int digits);

double MathTrunc(const double value, const double step);

Added two new functions for comparing doubles:

//+------------------------------------------------------------------+
//| Check almost equality (ignore tiny rounoff error)                |
//| If binary representations of two doubles differ by more than     |
//| one least significant bit (ulp), the function returns false.     |
//+------------------------------------------------------------------+
bool AlmostEqual(const double a, const double b);

//+------------------------------------------------------------------+
//| Check whether two floating point numbers are close in value.     |
//| Where, n: is the number of significant digits we have lost       |
//| through rounding errors (Max allowed precision difference).      |
//| Something like n = 2 or 3 usually works in practice.             |
//+------------------------------------------------------------------+
bool IsClose(const double a, const double b, const int maxDifferentSignificantDigits = 2);
 
Update 21 February 2023

Added a new function for comparing doubles:

//+------------------------------------------------------------------+
//| Test whether two numbers are close to within "n" significant     |
//| digits of precision.                                             |
//+------------------------------------------------------------------+
bool EqualDoubles(const double a, const double b, const int significantDigits = 15)
  {
//--- https://stackoverflow.com/a/17382806
   return MathAbs(a - b) < MathPow(10, -significantDigits) * MathMax(MathAbs(a), MathAbs(b));
  }


 

Update 1 March 2023

Added functions for comparison of doubles:

//+------------------------------------------------------------------+
//| Get number of fractional digits that agree after the decimal     |
//| point of two numbers.  GetEqualDigits(3.124, 3.122) => 2         |
//+------------------------------------------------------------------+
int GetEqualDigits(const double a, const double b);

//+------------------------------------------------------------------+
//| Get number of significant digits that agree of two numbers.      |
//| For example, GetEqualSignificantDigits(3.124, 3.122) => 3        |
//+------------------------------------------------------------------+
int GetEqualSignificantDigits(const double a, const double b);

Added miscellaneous functions:

//+------------------------------------------------------------------+
//| Get number of significant digits. Significant digits or figures  |
//| is the sum of integer and decimal digits (left and right of the  |
//| decimal point), excluding leading and trailing zeros.            |
//| For example, the number 1.23 has 2 decimal places and 3 s.f.     |
//| Hint: Change number to scientific notation. It is easier to see. |
//+------------------------------------------------------------------+
int GetSignificantDigits(double value);

//+------------------------------------------------------------------+
//| Returns the exponent of the scientific notation of a number.     |
//| In scientific notation a number is converted to a decimal number |
//| between 1.0 and 10, multiplied by 10 raised to some power.       |
//| It computes shift the decimal point to keep only one non-zero    |
//| digit before the decimal point.                                  |
//+------------------------------------------------------------------+
int GetExponent10(const double value)l

//+------------------------------------------------------------------+
//| Computes the sign of a value as 1, 0, -1                         |
//+------------------------------------------------------------------+
double MathSign(const double value);

Added function for formatting of doubles to string:

//+------------------------------------------------------------------+
//| Converting numeric value into a string in scientific notation    |
//| with one digit before the decimal point (e.g., 6.22e-23).        |
//| Digits : Optional. The number of digits after the decimal point. |
//| Defaults to as many digits as necessary to represent the value.  |
//+------------------------------------------------------------------+
string DoubleToExponential(const double value, int digits = -1);


 
Update 8 March 2023

Added new miscellaneous function.

//+------------------------------------------------------------------+
//| Returns pow(10, (int)power), uses fast lookup table for powers.  |
//| https://github.com/php/php-src/blob/master/ext/standard/math&nbsp;    |
//+------------------------------------------------------------------+
double GetPower10(const int power);

The function has a much faster performance than MathPow(10, power).

 

Added faster rounding functions.

//+------------------------------------------------------------------+
//| Fast ceil, floor, and round using arithmetic operators.          |
//+------------------------------------------------------------------+
inline long Ceil (const double v);
inline long Floor(const double v);
inline long Round(const double v);
inline long Trunc(const double v);
the speed advantage over built-in MathCeil, MathFloor and MathRound is about 160-240%.


 
amrali #:

Added faster rounding functions.

the speed advantage over built-in MathCeil, MathFloor and MathRound is about 160-240%.


Is the "inline" keyword now doing something ?
 
Alain Verleyen #: Is the "inline" keyword now doing something ?
I don't think so! It's probably "future proofing" for the day it becomes a "reality". 😅