거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Telegram에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

Doubles comparer - MetaTrader 5용 라이브러리

조회수:
2805
평가:
(9)
게시됨:
2020.06.10 12:54
업데이트됨:
2020.06.13 12:55
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

A tiny class to compare floating point variables the right way with desired precision.

template<typename T>class FloatingPoint
  {
public:
                     FloatingPoint() {Precision(sizeof(T));}
   int               Compare(const T x, const T y) {return (((fabs(x-y)<min))?0:((x-y>=min))?1:-1);}
   void              Precision(int digits) {min=pow(10,-fabs(digits));}
protected:
   double            min;
  };

Let's say you have two doubles with a value of 1.15.

When you compare them, you expect them to be equal.

But doubles in computer behave the strange way.

1.15 may be stored as 1.14999999999.

So comparing will fail your expectations.

The proper way to compare floating point variables, is by checking their subtraction with some minimum value.

FlatingPoint must be initialized with double or float datatype.

Comparer method returns 0 if x = y; 1 if x > y; and -1 if x < y.

Precision can be changed.

Deafult precision is 8 digits after fp for doubles, and 4 for floats.

Example of FloatingPoint class usage.

void OnStart()
  {
   FloatingPoint<double>fp;
   double a=1.15,b=1.14999999999;
   for(int i=15; i>5; i--)
     {
      fp.Precision(i);
      int compare=fp.Compare(a,b);
      string res=(compare==0)?"=":(compare==1)?">":"<";
      PrintFormat("%g %s %g at %d digit precision",a,res,b,i);
     }
  }

Output:

/**
   you do not expect this:
      1.15 > 1.15 at 15 digit precision
      1.15 > 1.15 at 14 digit precision
      1.15 > 1.15 at 13 digit precision
      1.15 > 1.15 at 12 digit precision
      1.15 > 1.15 at 11 digit precision
   
   you expect this:
      1.15 = 1.15 at 10 digit precision
      1.15 = 1.15 at 9 digit precision
      1.15 = 1.15 at 8 digit precision
      1.15 = 1.15 at 7 digit precision
      1.15 = 1.15 at 6 digit precision
/**/




    WaveMTF MT5 WaveMTF MT5

    Indicator WaveMTF Bull and Bear System with Signal and Alert for MetaTrader 5 with options to display signal on the chart.

    Basket Closer Profit/Loss Basket Closer Profit/Loss

    Basket Closer Type EA.

    Arrays class Arrays class

    Frequent array operation methods.

    Bar Time Count Down Bar Time Count Down

    This MT5 indicator is to count down the remaining time of the current bar as the format HH:MM:SS