Question about casting and function signature of MathMin/MathMax

 

It is obvious, documentation has not all details in this case, I am trying to get behind whats actually coded here.

I have written following test to find out... now I am maximal confused.

float f_in = NULL;
ulong ul_in = NULL;
float out1 = MathMin(f_in, ul_in);      // No error/warnint
float out2 = MathMin(ul_in, f_in);      // No error/warning
ulong out3 = MathMin(f_in, ul_in);      // Warning: float to ulong
int   out4 = MathMin(ul_in, f_in);      // Warning: float to int

Could anyone please shed some light on this?

Files:
 
Dominik Christian Egert: MathMin

In the case of both warnings, it is referring to the implicit type-casting of the value returned by MathMin, which returns a double, into a ulong or int which will suffer the loss of the decimal part of the returned value.

double  MathMin(
   double  value1,     // first value
   double  value2      // second value
   );
 
Fernando Carreiro #:

In the case of both warnings, it is referring to the type-casting of the value returned by MathMin, which returns a double, into a ulong or int which will suffer the loss of the decimal part of the returned value.

Yes, actually thats what I expected, but that is not sufficient enough - Take a look at the attached file please....

I dont understand when the return value is double and when it is not... 

 
Dominik Christian Egert #: Yes, actually thats what I expected, but that is not sufficient enough - Take a look at the attached file please.... I dont understand when the return value is double and when it is not... 

You will need to fix the file. It does not compile because the OnInit function declaration is incomplete. You need to close the parameter bracket.

void OnInit() // ← Was missing the closing round bracket
{

For the functions that don't give a typecasting warning, it is probably because that function is overloaded, even if not mentioned in the documentation.

For example MathAbs(), although not documented, when one is typing it out, the auto-substitution shows that the function is overloaded ...


 
Fernando Carreiro #:

You will need to fix the file. It does not compile because the OnInit function declaration is incomplete. You need to close the parameter bracket.

For the functions that don't give a typecasting warning, it is probably because that function is overloaded, even if not mentioned in the documentation.

For example MathAbs(), although not documented, when one is typing it out, the intelisense shows that the function is overloaded ...

Sorry for the missing bracet, I typed it as a wrapper...

I see, yes, thats what I suspected, but as you can see, MathAbs(short) returns int, and MathAbs(ushort) returns ushort....

My question is, what type of rules are used to determine the return type... - Background is, I am currently trying to replicate these function-signatures and I would like to have an exact copy, but without blindly copying them, but rather understanding the logic behind it....

 
Dominik Christian Egert #:

Sorry for the missing bracet, I typed it as a wrapper...

I see, yes, thats what I suspected, but as you can see, MathAbs(short) returns int, and MathAbs(ushort) returns ushort....

My question is, what type of rules are used to determine the return type... - Background is, I am currently trying to replicate these function-signatures and I would like to have an exact copy, but without blindly copying them, but rather understanding the logic behind it....

The basic typecasting rules are ...

A short fits in int, so there is no warning, but and int does not fit in a short, so a warning will be given.

As for the available overloading combinations of the MathAbs, it all depends on what overloaded declarations were defined by MetaQuotes "under the hood", given that they did not document them.

You will have to look at the auto-substitution or run multiple tests to find out (trial-and-error method).

 
Fernando Carreiro #:

The basic typecasting rules are ...

A short fits in int, so there is no warning, but and int does not fit in a short, so a warning will be given.

As for the available overloading combinations of the MathAbs, it all depends on what overloaded declarations were defined by MetaQuotes "under the hood", given that they did not document them.

You will have to look at the auto-substitution or run multiple tests to find out (trial-and-error method).

Reverse engineering....
I just defined all possible return types in straight to the inputs, now I am error correcting to find the valid combinations.
It's actually faster than intellisense...
 
Thank you for your support.
 
Dominik Christian Egert #: Thank you for your support.

You are welcome!

 
Fernando Carreiro #:

You are welcome!

Intellisense is not as conform as I expected...


It suggests, this would be possible:

string test_str1 = "";
string test_str2 = "";

MathMax(test_str1, test_str2);

But of course its not....

 
Dominik Christian Egert #: Intellisense is not as conform as I expected... It suggests, this would be possible: But of course its not....

Yes, it is ...

void OnStart()
{
   string sA = "AAA", sB = "BBB";
   string sC = MathMax( sA, sB );
   Print( "sC = ", sC );
};
2023.05.04 22:03:06.241 TestString (EURUSD,H1)  sC = BBB
Reason: