I have 2 equations written in my code (actually more but just to keep it simple), both should have exactly the same result. When I tested it though it turned out not to be true because when I did :
if(value1==value2){
.......
}
The code was not executed. So I decided to Print these two values and this showed me the following:
5.57792442477872 (value1)
5.577924424778722 (value2)
Now I could see that , as it should be, the 2 values are equal. The only problem is that the computer for some reason decided to round value2 up to and additional decimal which led to them being different. I did in fact use 'double' for both of them so I don't really understand how there can therefore be 2 values rounded up differently. Can anyone who knows something of this please explain me why this is the case and how you can solve this.
https://www.mql5.com/en/docs/basis/operations/relation
- www.mql5.com
Look up double in the documentation.
You cannot compare 2 doubles for equality.
- www.mql5.com
hello,
one should be cautious when comparing double, floats and so on. It is not a matter of binary versus decimal or whatever. Even in decimal we can not expect 4 ==(4/3)*3 when we are talking about finite calculations, like those implemented in a computer language, which generally map to the hardware. One could need a mathematical library to do so, but as far as basic machine calculations go, 4/3 will be 1.3333 with a finite amount of 3s, say 15, and then 1.3333()*3 will be 3.9999() and not 4.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have 2 equations written in my code (actually more but just to keep it simple), both should have exactly the same result. When I tested it though it turned out not to be true because when I did :
if(value1==value2){
.......
}
The code was not executed. So I decided to Print these two values and this showed me the following:
5.57792442477872 (value1)
5.577924424778722 (value2)
Now I could see that , as it should be, the 2 values are equal. The only problem is that the computer for some reason decided to round value2 up to and additional decimal which led to them being different. I did in fact use 'double' for both of them so I don't really understand how there can therefore be 2 values rounded up differently. Can anyone who knows something of this please explain me why this is the case and how you can solve this.