Libraries: Math Utils - page 4

 

fxsaber #:

You need to learn how to make proper performance measurements.

You are drawing wrong conclusions.

Unfortunately, you do not understand what you are measuring.

So, tell me what I do not understand please :-)

 
Till now, I understand that I should replace the code with your optimized (!) function, then put a big warning for users to disable compiler optimizations before using the library, in order to get the same performance they would get without using all of this shit!
 
amrali #:
Till now, I understand that I should replace the code with your optimized (!) function, then put a big warning for users to disable compiler optimizations before using the library, in order to get the same performance they would get without using all of this shit!

If, indeed, you have a desire to figure out how to correctly measure the performance of any functions, go to the Russian-language forum and ask there. The developers talked about it in detail with examples. I don't have any links.

 
Thanks 
 

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     |
//+------------------------------------------------------------------+
double GetPower10(const int power);

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

 
fxsaber #:

Faster.

@fxsaber Thanks!

Forum on trading, automated trading systems and testing trading strategies

Libraries: Math Utils

amrali, 2023.03.08 15:32

Update 8 March 2023

Added new miscellaneous function.

//+------------------------------------------------------------------+
//| Returns pow(10, (int)power), uses fast lookup table for powers.  |
//+------------------------------------------------------------------+
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%.


 

Update 24 April 2023

Added new function:

//+------------------------------------------------------------------+
//| Wrap a value between min (inclusive) and max (exclusive).        |
//+------------------------------------------------------------------+
template<typename T>
T Wrap(const T value, const T min, const T max);

Renamed the rounding functions to Ceil(), Floor(), Round() and Trunc() to avoid name conflicts with <Math\Stat\Math.mqh>.

Fixed the GetDigits() function to avoid an infinite loop in rare situations.

 
These are three handy functions for comparison and rounding of floating-point numbers and formatting money:

1. `bool DoubleEquals(double x, double y, double eps)` compares two double values `x` and `y` with a given epsilon value `eps` and returns a boolean value indicating if they are equal within the given tolerance.

2. `double RoundTo(double value, int digits)` rounds a double value `value` to the given number of decimal `digits`.

3. `string FormatMoney(double amount)` formats a double value `amount` as a string representing a currency amount. It formats the amount with two decimal places, replaces the decimal point with a comma, and inserts spaces every three digits for readability. It also adds the currency symbol obtained from `AccountInfoString(ACCOUNT_CURRENCY)` at the end.

  1. DoubleEquals(double x, double y, double eps) - This function compares two double values x and y with a tolerance eps . It returns true if x is within eps of y . This is useful for comparing floating-point numbers that may not be exactly equal due to rounding errors.

bool DoubleEquals(double x, double y, double eps) { return fabs(x - y) < eps; } 

  1. RoundTo(double value, int digits) - This function rounds a double value value to a specified number of decimal digits . It uses the MathRound() function to perform the rounding.

double RoundTo(double value, int digits) { double factor = pow(10, digits); return MathRound(value * factor) / factor; }

  1. FormatMoney(double amount) - This function formats a double value amount as a string with currency symbols, commas for thousands separators, and two decimal places. It is useful for displaying monetary values in a user-friendly way.
string FormatMoney(double amount) {
    string currency = AccountInfoString(ACCOUNT_CURRENCY);
    string formatted = StringFormat("%.2f", amount);
    formatted = StringReplace(formatted, ".", ",");
    int len = StringLen(formatted);
    for (int i = len - 6; i > 0; i -= 3) {
       formatted = StringSubstr(formatted, 0, i) + " " + StringSubstr(formatted, i);
    }
    return formatted + " " + currency;
}

Post formatted by moderator