mql5语言的特点、微妙之处以及技巧 - 页 242

 
Alain Verleyen #:

它希望 MathMin() 函数是确定的。因此,当两个参数相同时,总是给出相同的结果。而不是根据第一个或第二个参数的不同而给出不同的结果。

fxsaber 说得没错,这是个问题。

啊......,那为什么 "数学上相同的表达式(突出显示) " 会让人如此困惑,而联合
就足够了:

Print(MathMin(-0.0, 0.0)); // 0.0
Print(MathMin(0.0, -0.0)); // -0.0

但既然 0.0 == -0.0,那么我无法想象在什么情况下这可能会成为一个问题,并影响任何事情

 

MQL 中最小值/最大值的老功能 - 它们按开发人员的想法工作,即正确工作 :-)

是标准出了问题。

 
Nikolai Semko #:

啊哈......,那为什么 "数学上相同的表达式(突出显示) " 会如此令人困惑,而联合
就足够了:

但既然 0.0 == -0.0,那我就无法想象在什么情况下这可能会成为一个问题,事实上,根本不会影响任何事情

如果不是零,那就会有问题。
For:

x * 0.0 = 0.0
x * -0.0 = -0.0
x + -0.0 = x
x - -0.0 = x
pow(x,-0.0) = 1
log(0.0)  // -inf
log(-0.0)  // -inf
 

c++ - -0和0有什么区别?- Stack Overflow




因此,您只需知道 0 = -0
这就是不影响计算和逻辑的全部特征

What is the difference between -0 and 0?
What is the difference between -0 and 0?
  • 2010.09.14
  • Danvil Danvil 22.5k 20 20 gold badges 66 66 silver badges 90 90 bronze badges
  • stackoverflow.com
In C++, for example returns . The expression is true, but the bits are different. What is the purpose of having something like which should be but is represented differently? Is used exactly the same way as in any computations?
 
fxsaber #:

我对这个问题的理解非常透彻,因此我编写了两个版本的 MathMin,以说明编程语言中数学上相同的函数会产生不同的结果。

template <typename T>
T ToType( const double Num )
{
  union UNION
  {
    T Num1;
    double Num2;
  } Union;
  
  Union.Num2 = Num;
  
  return(Union.Num1);
}

void OnStart()
{
  Print(ToType<long>(MathMin(-0.0, 0.0))); // 0
  Print(ToType<long>(MathMin(0.0, -0.0))); // -9223372036854775808
}

这似乎是一个错误

在 C++ 中运行正常:

#include <iostream>

using namespace std;
//
template <typename T>
T ToType(const double Num)
{
    union UNION
    {
        T Num1;
        double Num2;
    } Union;
    Union.Num2 = Num;
    return(Union.Num1);
}
//
int main()
{
    double positive_zero_val, negative_zero_val;
    positive_zero_val = 0.0;
    negative_zero_val = -0.0;
    //---
    long long_negative_zero_val = ToType<long>(negative_zero_val);
    printf("\nLong negative zero = %d", long_negative_zero_val);
    cin.get();
}

在控制台中:

Long negative zero = 0


顺便向开发人员提个问题。在调试窗口中,Union 变量不展开,点击后也没有任何反应,这正常吗?


Union.Num1 和 Union.Num2 字段是手动添加的。至少这样你可以看到值...

 
string DoubleToHexadecimal(const double value)
  {
   return StringFormat("0x%.16llX", value);
  }

void OnStart()
  {
   Print(DoubleToHexadecimal(0.0) );    // 0x0000000000000000   (i.e, long integer 0)
   Print(DoubleToHexadecimal(-0.0) );   // 0x8000000000000000   (i,e, LONG_MIN -9223372036854775808)
  }
  

与预期的一样!第一位(位索引 0)是符号位,设置为 -0.0。

https:// www.wikiwand.com/en/IEEE%20754#Special_values

void OnStart()
  {
   const double Nan = (double)"nan";
   const double Inf = (double)"inf";

   Print( MathMin(Nan, Inf));   // inf
   Print( MathMin(Inf, Nan));   // nan

   Print( MathMin(Nan, 0));     // 0.0
   Print( MathMin(0, Nan));     // nan
  }
 
Denis Kirichenko #:

这似乎是一个错误

在 C++ 中运行正常:

在控制台中

没有错误。在 C++ 中也是一样。
你只是在格式和类型上犯了错误

试试这个:

#include <iostream>

using namespace std;
//
template <typename T>
T ToType(const double Num)
{
    union UNION
    {
        T Num1;
        double Num2;
    } Union;
    Union.Num2 = Num;
    return(Union.Num1);
}
//
int main()
{
    double positive_zero_val, negative_zero_val;
    positive_zero_val = 0.0;
    negative_zero_val = -0.0;
    //---
    long long  long_negative_zero_val = ToType<long long>(negative_zero_val);
    printf("\nLong negative zero = %ll d", long_negative_zero_val);
}
 

在同样的上下文中,这可以检查双倍运算的 64 位表示https://www.mql5.com/en/code/20822

//+------------------------------------------------------------------+
//| Returns the bit representation corresponding to a double value . |
//+------------------------------------------------------------------+
long DoubleToLongBits(const double value)
  {
   union _d {double value; long bits;} dbl;
   dbl.value = value;

   return dbl.bits;
  }

或更简短的版本

long DoubleToLongBits(const double value)
  {
   union _d {double value; long bits;} dbl = { value };

   return dbl.bits;
  }
 
Nikolai Semko #:

没有错误。
只是在格式和类型上犯了一个错误。

试试这个:

是的,我太着急了。谢谢你的科普 ))

 

有时,您需要快速将 EA 放入测试器。您可以这样做。

选择未来的某个日期,然后在 ME 中按 CTRL+F5。