Need some help to sort out an error

garyfritz

Re: Need some help to sort out an error

Post by garyfritz »

FWIW: I tested MQL's double-to-int conversions with the code below. I checked double-to-int assignments, passing doubles for int parameters, and returning doubles from an int function. I tested those cases with both constants and double vars. I think that hits just about all the important cases. If you wanted to get really thorough you could also test all the combinations of ints and doubles in arithmetic expressions, but if it handles these cases right, I'd say it's pretty unlikely it doesn't handle math right.

Everything worked like it should. The only surprise is that MQL truncates integers instead of rounding them, but that's a reasonable definition.

So I don't think the DoubleToInt() function should be necessary at all, unless you want to round instead of truncating. And then you could just use MathRound() instead of DoubleToInt().

Code: Select all

#property indicator_chart_window

string com;
string NL = "\n";

int init()
{
	com = "";
	
	int ival1, ival2;
	double dval = 34.567;
	
	ival1 = dval;
	ival2 = 98.765;
	com = StringConcatenate(com, "double-to-int assignment values:  ",ival1,", ",ival2,NL);
	
	ival1 = IntParam(dval, 98.765);
	com = StringConcatenate(com, "IntParam return = ",ival1,NL);
	
	ival1 = ReturnConst();
	com = StringConcatenate(com, "ReturnConst value = ",ival1,NL);
	
	com = StringConcatenate(com, "IntParam return w/o int assignment = ",IntParam(dval, 98.765),NL);
	com = StringConcatenate(com, "ReturnConst return w/o int assignment = ",ReturnConst(),NL);
	
	Comment(com);
}

int IntParam(int iparam1, int iparam2) {
	com = StringConcatenate(com, "iparam values = ",iparam1,", ",iparam2,NL);
	double e = 2.71828;
	return(e);
	}
	
int ReturnConst() {
	return(24.68);
	}
	
int start()
{
}
Post Reply

Return to “Indicators”