
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
That's what the computer teacher at school said
to fire.
firstly, it is not faster, secondly, it is not more obvious to the reader, finally it is up to the compiler to make such micro-optimisations.
Well, and to boot, the "subtraction" operation is not allowed (makes sense) for all domains and classes. By replacing the == operator with a-b==0 you can make a programme invalid
Because direct comparison of real numbers often leads to wrong results. And because of this, some people thoughtlessly apply this method wherever necessary and wherever not necessary.
And in fact, the original variant, if it differs, is very insignificant.
Comparison
I don't know which is faster, but we also need reliability of conditions
a = 0.30000000000000004
b = 0.3
That’s exactly why a - b == 0 is not safe for doubles.
Why a != b? Because neither 0.1, nor 0.2, nor 0.3 can be represented exactly in binary with the double type (64-bit IEEE 754).
Comparison
I don't know which is faster, but we also need reliability of conditions
In this particular example, this is even more reliable:
In fact, in all other double numbers.
If two double numbers are really equal in value, then the comparison "==" will be true, and the comparison a == b makes sense absolutely and is equal to the comparison a - b == 0.0.
So you just have to realise that writing a value in number code is not always equivalent to the very value of the number that the variable stores. I have never had any problems with direct comparison of two double numbers.
Another thing is that when we want to make a comparison with a specified precision, rather than a comparison of variable values (for example, two quotes), it is useful to compare the difference with tickSize rather than with point.
The code demonstrating the above:
If two double numbers are really equal in value, then the comparison "==" will be true, and the comparison a == b makes sense absolutely and is equal to the comparison a - b == 0.0.
So you just have to realise that writing a value in number code is not always equivalent to the very value of the number that the variable stores. I have never had any problems with direct comparison of two double numbers.
Another thing is that when we want to make a comparison with a specified precision, rather than a comparison of variable values (for example, two quotes), it is useful to compare the difference with tickSize rather than with point.
The code demonstrating the above:
It is true that a == b works if both values are exactly the same in memory, as in your examples with constants.
But in practice, most doubles come from operations that introduce rounding errors, and that's where == (and also a - b == 0.0) become unreliable.
That's why it is recommended to use MathAbs(a - b) < ε, where ε depends on the context ( _Point, tick size or DBL_EPSILON).
Here, it is because of the highlighted that I wrote the post above. Recommended by whom? - For what cases is it recommended?
We want to compare variables by their value - we use their direct comparison. We want to compare with a given precision - we compare the difference of numbers with a given precision. There is no sorcery here, you can and should use both, it depends on the specific task.
Here, it is because of the highlighted that I wrote the post above. Recommended by whom? - For what cases is it recommended?
We want to compare variables by their value - we use their direct comparison. We want to compare with a given precision - we compare the difference of numbers with a given precision. There is no sorcery here, you can and should use both, it depends on the specific task.
I totally agree, Andrey. It's not a matter of one form being "good" and the other "bad", but of understanding when to use each one.
The use of == is perfectly valid if we know that the values are exact (e.g. constants or controlled data). The use of MathAbs(a - b) < ε simply adds a useful tolerance when there are previous operations that may introduce small inaccuracies, as often happens in intermediate calculations with double.
In the end, as you say, it depends on the specific case and the level of precision we need.