0

In my assignment I had been asked to say why the following code prints mis-match the code checks for a == square (sqrt(a))

#include <iostream>
#include <cmath>
using namespace std;
#define sqr(x) ((x) * (x))
int main() {
        double a = 4.0*atan(1.0); // pi
        double b = sqrt(a); // square-root of pi
        if (a == sqr(b)) // pi is equal to
        // square of square-root of pi?
                cout << "Match" << endl;
        else
                cout << "Mis-Match" << endl;
        return 0;

}

I know that this type of problem are do to the fact that floating point numbers are approximate representation and for checking equality one should check for almost equality case i.e. |a-b|<epsilon, where epsilon is taken to be 1e-8 or any other suitable value.

But when I replace double with long double the code executed to print Match.

So I want to know when == works and when it does not.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Aryan
  • 67
  • 1
  • 5
  • 2
    _"I want to know when == works and when it does not."_ It would require **a lot** of reading to be able to think like a floating point processor. Very good programmers will assume that they aren't certain. – Drew Dormann Jan 20 '21 at 15:11
  • Because: more precision for `long double`? – Sam Varshavchik Jan 20 '21 at 15:11
  • 2
    same code, same issue: https://stackoverflow.com/questions/65810428/whats-wrong-with-my-code-its-showing-mismatch-even-when-it-should-show-match/65810501?noredirect=1#comment116360159_65810501 – 463035818_is_not_an_ai Jan 20 '21 at 15:13
  • `==` always "works", it just happens rarely that two floating point numbers that are a result of some calcualtion happen to be equal – 463035818_is_not_an_ai Jan 20 '21 at 15:15
  • 1
    @largest_prime_is_463035818 hm... sometimes a teacher will tell an _entire class_ to bring the same question here. – Drew Dormann Jan 20 '21 at 15:15
  • " case i.e. |a-b| – Bathsheba Jan 20 '21 at 15:50
  • `4.0*atan(1.0)` is not guaranteed to equal *pi* either. `atan` is not required by any common standard to return the best floating point value possible. (Interestingly `std::sqrt` must return the best floating point value possible under IEEE754.( – Bathsheba Jan 20 '21 at 15:52
  • Don't use `==` to compare floating point values. Must read: [*What every computer scientist should know about floating-point arithmetic*](https://dl.acm.org/doi/10.1145/103162.103163). Most floating point values are only approximation. – Thomas Matthews Jan 20 '21 at 16:06
  • we are so much used to real numbers as they are used in maths that sometimes one forgets that any number that one can write down in reality has limited precision. In some sense real numbers is just a nice trick so we can do maths with numbers, but in reality I have not seen a real number yet – 463035818_is_not_an_ai Jan 20 '21 at 16:09
  • `==` always works; `a == b` evaluates to true if and only if `a` equals `b`. The question you want to ask is when are two values computed with floating-point equal. The answer is that depends on how they were computed, and there is, in general, no simple way of telling because error analysis of numerical computations is very complicated. – Eric Postpischil Jan 20 '21 at 18:40
  • Detail: "floating point numbers are approximate representation" --> All finite floating point values are _exact_ - each FP values has an exact decimal and exact binary representation. Often an operation forms a value that is approximate to math result, but the FP result is exact. – chux - Reinstate Monica Jan 20 '21 at 23:41

0 Answers0