Libraries: Math Utils

 

Math Utils:

Handy functions for comparison and rounding of floating-point numbers (prices, lots and money).

Author: amrali

 

Update 19 May 2021

Added functions for debugging

// Converting numeric value into the exact decimal text string.
string DoubleToStringExact(double value);

// Converting numeric value into the raw hexadecimal text string.
string DoubleToHexadecimal(double value);
 
Update 21 May 2021

Added miscellaneous functions

// Convert x in range [min, max] to y in another range [new_min, new_max]. 
// This can be used to rescale indicator values or graphic objects.
double map_range(double x, double min, double max, double new_min, double new_max);

// Get number of digits after the decimal point.
int GetDigits(double number);
 

Update 27 May 2021

Added functions for debugging

// Returns int diff between two floats expressed in epsilon (ulps).
long UlpDiff(const double num1, const double num2);

// Returns the next representable value after x
double nextafter(double num);

Added miscellaneous functions

bool IsRound(const double number, const int digits);

bool IsRoundToStep(const double number, const double step);
Added 'dotnet_cross_check.mq5' script and 'RoundSharp.dll' written in C#.

 
Update 30 May 2021

Added miscellaneous functions

//Get number of integer digits to the left of decimal point.
int    IntegerDigits(double number);

//map x on a scale [min, max] to normalized scale [0, 1].
double normalize(double x, double min, double max);

Handy functions for the binary representation of doubles

//Returns the double value corresponding to a bit representation.
double LongBitsToDouble(long bits);

//Returns the bit representation corresponding to a double value .
long   DoubleToLongBits(double value);

//Returns the unbiased exponent used in the bit representation.
int    GetExponent(double value);

//Returns the mantissa used in the bit representation.
long   GetMantissa(double value);

//Returns the size of an ulp of the argument.
double Ulp(double value);

Handy functions for the string representation of doubles

// Converting numeric value into the shortest string representation
// that round-trips into the same numeric value.
string Repr(double value);
 

Update 1 June 2021

Added new functions:

// Returns the raw encoding of exponent in the bit representation.
int    RawExponent(double value);

// Returns raw encoding of significand in the bit representation.
long   RawSignificand(double value);

// Returns the unbiased (adjusted) exponent of a double value.
int    Exponent(double value);

// Returns the significand of a double value.
double Significand(double value);

// Determine whether number is exactly representable in double.
bool   IsExactDouble(double value);

// Formats number with thousands separator.
string FormatNumber(const double number,const string separator=",");
 
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))


 
can these be added to mt4? have you tested these in mt4? (the include file i mean)
 

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);
Reason: