Which code is better or optimized or efficient?
double a;
double b;
if (a == b)
return true;
or
if (a - b == 0)
return true;
Which code is better or optimized or efficient?
double a;
double b;
if (a == b)
return true;
or
if (a - b == 0)
return true;
a % b == 0
does not imply that a
equals b
. So the compiler cannot optimise this out.
As for the other two, you ought to use a == b
as it's clearer, and leave the optimisations to the compiler.