-5

Which code is better or optimized or efficient?

double a;
double b;

if (a == b)
  return true;

or

if (a - b == 0)
   return true; 
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
  • 1
    in what language ? The first should be fastest because it's one operation, unless you work on a very specific system but I doubt that the others would be faster somewhere. – HopefullyHelpful Aug 23 '16 at 12:15
  • 2
    Note that they do very different things. Consider `a=4, b=2`... then `a%b==0`, but `a != b`. – Jon Skeet Aug 23 '16 at 12:16
  • 1
    Your title is horrible, it has nothing to do with your question, anyone searching for it will never find it. Rename it to something regarding comparison or equallity. – HopefullyHelpful Aug 23 '16 at 12:16
  • `a % b` isn't necessarily `a == b` or `a - b` – Andrew Li Aug 23 '16 at 12:17
  • on another note worrying about the performance of 1 line is overkill unless you know this line/method is executed most of your computation time. – HopefullyHelpful Aug 23 '16 at 12:20
  • 1
    Always think twice before comparing floats or doubles using `==`. Rounding may cause values that should have been the same not to be equal. For most purposes, test whether the difference is within some suitably narrow margin. – Ole V.V. Aug 23 '16 at 12:34
  • @OleV.V. thanks for your reply.. then whats should be done if == is not suitable for rounding purpose. margin check needs more code and memory i think. – Mahmud Riad Aug 23 '16 at 13:03
  • 1
    A bit more code, yes, if any more memory, it’s neglectable. One option is `if (Math.abs(a - b)) < THRESHOLD)` … Last time I used `1e-11`, but the correct choice of threshold will depend on the context. – Ole V.V. Aug 23 '16 at 13:10
  • should you really use a threshold like that ? Isn't a multiple of the ulp better ? – HopefullyHelpful Aug 23 '16 at 14:24
  • 1
    *Floating point math is hard. You just won’t believe how vastly, hugely, mind-bogglingly hard it is. I mean, you may think it’s difficult to calculate when trains from Chicago and Los Angeles will collide, but that’s just peanuts to floating-point math.* See [Bruce Dawson's excellent article](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) about FP comparisons. @HopefullyHelpful and Ole, you folks should have a look; Bruce discusses the merits of absolute thresholds vs. epsilon vs. relative ULP. – Peter Cordes Aug 25 '16 at 01:01

1 Answers1

7

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • More importantly, any decent compiler can and *will* optimize `a == b` to the same code as `a - b == 0`, if those expressions are equivalent. (e.g. they're not for floating point, where `a-b` can raise an FP inexact result exception, but `a==b` can't, at least in ISO C.) – Peter Cordes Aug 25 '16 at 00:56