Open In App

islessequal() in C/C++

In C++, islessequal() is a predefined function in math.h. It is used to check whether the 1st floating point number is less than or equal to the 2nd floating point. The advantage it provides over simple comparison is, it does comparison without raising floating point exceptions. For example, if one of the two arguments is NaN, then it returns false without raising exception.

Syntax:



bool isgreaterequal(a, b)

Parameters:

  • a, b => These two are the parameters whose value we want to compare.

Result:



  • The function will return the true if a <= b else it returns false.

Error:

  • No error occurs with this function.
  • Exception:

    • If a or b or both is NaN then the function raised an exception and return false(0).

    Illustration of this is given below




    // CPP code to illustrate
    // the exception of function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Take any values
        float a = 5.5;
        double f1 = nan("1");
        bool result;
      
        // Since f1 value is NaN so
        // with any value of a, the function
        // always return false(0)
        result = islessequal(a, f1);
        cout << a << " islessequal " << f1
             << ": " << result;
      
        return 0;
    }
    
    

    Output:

    5.5 islessequal nan: 0
    

    Examples:

    Applications:
    There are variety of applications where we can use islessequal() function to compare two values like to use in while loop to print the first 10 natural number.




    // CPP code to illustrate
    // the use of islessequal function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        int i = 1;
        while (islessequal(i, 10)) {
            cout << i << " ";
            i++;
        }
        return 0;
    }
    
    

    Output:

    1 2 3 4 5 6 7 8 9 10 
    

    Article Tags :