MQL5 question

 

I really do not understand why decimal type was not introduced in mql5....double is a mess.
A part of that, I have problem in my code with a strange behaviour.


Alert("New bar created"," At "+NormalizeDouble(avgPrice,_Digits)+" by re

It shows this result:



In this image all rows are ok except the first....first and 2° rows are related to same pair (eurgbp).
Any idea?

Thanks

 

This has been discussed many times (see search function for NormalizeDouble).

There is nothing inherently broken about the NormalizeDouble() function, you're just using it for the wrong purpose (and also you can very well avoid using the function ever at all).

If you want to output a double with a limited number of digits within a text/string (Print, Alert, Comment...), DoubleToString() usually is the way to go (or sometimes also PrintFormat). If we don't need to display the numbers, but only want to perform calculations with them (and that require rounding), solutions involving NormalizeDouble, MathRound, MathFloor or MathCeil can be used, depending on how exactly we want to limit/round the accuracy.

There simply are differences between the way the computer handles these numbers and how we're used to them being displayed.

When it comes to floating point numbers, this problem can't be avoided because of the limited number of available digits for the "mantissa", so that there always remains a potential error and there is a limit to the max. possible accuracy and some numbers therefore just can't be represented correctly, like the number PI or e.g. 1/10.

For more details I recommend the wikipedia article "floating point arithmetics".

 
Chris70:

This has been discussed many times (see search function for NormalizeDouble).

There is nothing inherently broken about the NormalizeDouble() function, you're just using it for the wrong purpose (and also you can very well avoid using the function ever at all).

If you want to output a double with a limited number of digits within a text/string (Print, Alert, Comment...), DoubleToString() usually is the way to go (or sometimes also PrintFormat). If we don't need to display the numbers, but only want to perform calculations with them (and that require rounding), solutions involving NormalizeDouble, MathRound, MathFloor or MathCeil can be used, depending on how exactly we want to limit/round the accuracy.

There simply are differences between the way the computer handles these numbers and how we're used to them being displayed.

When it comes to floating point numbers, this problem can't be avoided because of the limited number of available digits for the "mantissa", so that there always remains a potential error and there is a limit to the max. possible accuracy and some numbers therefore just can't be represented correctly, like the number PI or e.g. 1/10.

For more details I recommend the wikipedia article "floating point arithmetics".

Thanks a lot Chris,

my fault I did not search for it but the behaviour seemed really strange to me.