NormalizeDouble() function

 
Can the result of NormalizeDouble() be saved to a variable or do you have to normalize a double variable every time it is used in a comparison operation?

Example:
double a = 0.0002;
a = NormalizeDouble(a,4);


Can variable a now be used in multiple comparisons:
b = //...
c = //...
if(a > b){
//...
}else if (a > c){
//....
}


OR would you have to normalize each time:
if(NormalizeDouble(a,4) > NormalizeDouble(b,4)){
//...
}else if(NormalizeDouble(a,4)> NormalizeDouble(c,4)){
//...
}
 
Can the result of NormalizeDouble() be saved to a variable or do you have to normalize a double variable every time it is used in a comparison operation?
Yes it can and it's safe unless you change it explicitly.

The whole 'normalise' thing comes from a rather distinctive feature of arithmetic operations with double values - they're likely to have a very small error in the result due to limited number of bits in internal (machine) representation of the double value (64 on most contemporary platforms including MT).

Hence the statement
10. / 3. * 3. != 10.

is true however paradoxically it looks.

In other hand a variable is just a named cell in the computer memory. It's not supposed to change it's content unless the CPU writes something in it, e.g. a result of an arithmetic operation, data from a file or a constant, etc. So once assigned, the variable keeps the value unchanged no matter what type it is. If it doesn't computer crashes.

More than that, the first normalisation is superfluous as the assignment is not considered as an arithmetic operation. The compiler makes sure that the variable is assigned exactly the value of the constant unless its precision is too high (not your case anyway).

All above is an adapted view of how the things work inside the computer. Should be enough for understanding that NormalizeDouble issue though.

mqlexpert AT gmail DOT com

 
Thank you for the thorough answer!
Reason: