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?
I have an excellent understanding of the question, so I wrote both versions of MathMin to show that mathematically identical functions in programming languages produce different results.
template <typename T>
T ToType( constdouble Num )
{
union UNION
{
T Num1;
double Num2;
} Union;
Union.Num2 = Num;
return(Union.Num1);
}
voidOnStart()
{
Print(ToType<long>(MathMin(-0.0, 0.0))); // 0Print(ToType<long>(MathMin(0.0, -0.0))); // -9223372036854775808
}
It seems to be a bug.
It works fine in C++:
#include <iostream>
using namespace std;
//template <typename T>
T ToType(constdouble 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();
}
in the console:
Long negative zero = 0
Question in passing to the developers. Is it normal that in the debugging window the Union variable does not expand and does not react at all to a click?
The fields Union.Num1 and Union.Num2 were added manually. At least this way you can see the values...
//+------------------------------------------------------------------+//| Returns the bit representation corresponding to a double value . |//+------------------------------------------------------------------+long DoubleToLongBits(constdoublevalue)
{
union _d {doublevalue; long bits;} dbl;
dbl.value = value;
return dbl.bits;
}
or the shorter version
long DoubleToLongBits(constdoublevalue)
{
union _d {doublevalue; long bits;} dbl = { value };
return dbl.bits;
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
an old feature of Min/Max in MQL - they work the way the developers think, i.e. correctly :-)
it's the standards that are wrong.
ahhh..., then why was it so confusing about " Mathematically the same expression (highlighted) " and union
was enough:
but since 0.0 == -0.0, then I can't imagine a situation where this could be a problem and, in fact, affect anything at all
If it wasn't zero, then it would be a problem. And at zero it doesn't affect anything.
For:
c++ - What is the difference between -0 and 0? - Stack Overflow
So, all you need to know is that 0 = -0
That's the whole feature that doesn't affect calculations and logic
I have an excellent understanding of the question, so I wrote both versions of MathMin to show that mathematically identical functions in programming languages produce different results.
It seems to be a bug.
It works fine in C++:
in the console:
Long negative zero = 0Question in passing to the developers. Is it normal that in the debugging window the Union variable does not expand and does not react at all to a click?
The fields Union.Num1 and Union.Num2 were added manually. At least this way you can see the values...
Works as expected! The first bit (bit index 0) is the sign bit which is set for -0.0.
These are also more special cases in ieee 754 format. https://www. wikiwand.com/en/IEEE%20754#Special_values
It seems to be a bug.
It works fine in C++:
in the console:
there is no bug. In C++ it is the same.
you just made a mistake with formatting and type
try this:
In the same context, this can examine the 64 bit representation of doubles https://www.mql5.com/en/code/20822
or the shorter version
there is no bug. It is the same in C++.
just made a mistake with formatting and type
try this:
Yes, I was in a hurry. Thanks for the science ))
Sometimes you need to quickly put an EA into the Tester. You can do it like this.
Select a date in the future and press CTRL+F5 in ME.