13

I get this code snippet from some where else. According to the webmaster, the code is picked from The art of computer programming by Knuth

Since I do not have a copy of that book, may I know what is the difference among the two functions?

bool approximatelyEqual(float a, float b, float epsilon)
{
    return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool essentiallyEqual(float a, float b, float epsilon)
{
    return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
palswim
  • 10,910
  • 6
  • 47
  • 72
Cheok Yan Cheng
  • 49,649
  • 117
  • 410
  • 768

3 Answers3

14

To give an example:

double a = 95.1, b = 100.0;
assert( approximatelyEqual( a, b, 0.05 ) );
assert( !essentiallyEqual( a, b, 0.05 ) );

That is, with epsilon being a 5%, 95.1 is approximately 100, as it falls within the 5% margin of the 100 value (biggest). On the other hand, 95.1 is not essentially 100, as 100 is not within a 5% difference from 95.1 (smallest value).

David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
  • Can I say, essentiallyEqual will always need a `closer` value than approximatelyEqual? – Cheok Yan Cheng Sep 16 '10 at 17:03
  • Yes, `essentiallyEqual` values will always be "closer" than `approximatelyEqual` values. – palswim Sep 16 '10 at 17:08
  • 3
    Think on the offers at your regular store and what percentages mean there. What is more valuable 33% discount in the price or 33% extra free product? The solution is that you should prefer the 33% discount as that is equivalent to a 50% extra free product offer. The same occurs here, depending on whether you take the epsilon around the greater or smallest of the two values the result will differ. 66.6 is aproximately equal to 100 with a 33% epsilon, but only essentially equal with a 50% epsilon. – David Rodríguez - dribeas Sep 16 '10 at 17:24
11

approximatelyEqual gives whether the difference between a and b is smaller than the acceptable error (epsilon), determined by the larger of a or b. This means that the two values are "close enough", and we can say that they're approximately equal.

essentiallyEqual gives whether the difference between a and b is smaller than the acceptable error (epsilon), determined by the smaller of a or b. This means that the values differ less than the acceptable difference in any calculation, so that perhaps they're not actually equal, but they're "essentially equal" (given the epsilon).

This has applications in issues where we have data and "acceptable error" rates and such. This code just gives you an algorithmic definition of those terms.

palswim
  • 10,910
  • 6
  • 47
  • 72
  • Can you give a real world example, how we should choose among the 2 functions? – Cheok Yan Cheng Sep 16 '10 at 16:56
  • `This means that the values differ less than the acceptable difference in any calculation` - if the values differ is lesser, this also mean `close enough`? So, what will it different from `approximatelyEqual` ? – Cheok Yan Cheng Sep 16 '10 at 16:59
  • Here is a [medical article](http://www.pnas.org/content/95/3/811.full) which uses both terms. It's not necessarily how to choose, but it's also how you describe data. – palswim Sep 16 '10 at 17:05
  • 3
    These are the two most basic, of many, methods to measure whether a calculation produces good results. If you rely on a human-understood concept of approximation - the calculation for approximatelyEqual is useful in error measurements which depend on human interpretation (when you care about whether a human thinks you produced an error). Essentially equal deals with limitations in software and hardware - and is useful when you're measuring whether a computer will treat your results as the same (would a computer be able to tell the difference between the two results?) – blueberryfields Sep 16 '10 at 17:09
  • 2
    Are "approximately" and "essentially" terms d'art, or are they just a couple of words that Knuth decided to assign meaning to for the purposes of his book? Do other people commonly use those same words to refer to this same distinction? – Rob Kennedy Sep 16 '10 at 17:32
  • 2
    The terms are used in fields which deal with margins of error, with the same general distinction - essentially equal measurements are treated as equivalent by the system being analyzed, while approximately equal measurements are treated as equivalent by humans. The distinction is not always relevant. – blueberryfields Sep 16 '10 at 17:47
5

The difference is that essential equality implies approximate equality, but not vice versa. So essential equality is stronger than approximate equality.

Also essential equality is not transitive, but if a is essentially equal to b, and b is essentially equal to c, then a is approximately equal to c (for another value of epsilon).

Marat Salikhov
  • 5,819
  • 4
  • 29
  • 33