Two doubles rounded up differently

 

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.



 
Ade Bijon:

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


Documentation on MQL5: Language Basics / Operations and Expressions / Operations of Relation
Documentation on MQL5: Language Basics / Operations and Expressions / Operations of Relation
  • www.mql5.com
True if a is equal to b                      a == b; True if a is not equal to b                  a != b; True if a is less than b                     a  b; True if a is less than or equal to b         a <= b;
 

Look up double in the documentation.

You cannot compare 2 doubles for equality.

https://www.mql5.com/en/docs/basis/types/double

Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
  • www.mql5.com
Real types (or floating-point types) represent values with a fractional part. In the MQL5 language there are two types for floating point numbers.The method of representation of real numbers in the computer memory is defined by the IEEE 754 standard and is independent of platforms, operating systems or programming languages. Floating-point...
 

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.

Reason: