Open In App

islessequal() in C/C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

    • Program 1:




      // CPP code to illustrate
      // the use of islessequal function
      #include <bits/stdc++.h>
      using namespace std;
        
      int main()
      {
          // Take two any values
          float a, b;
          bool result;
          a = 5.2;
          b = 8.5;
        
          // Since 'a' is less than
          // equal to 'b' so answer
          // is true(1)
          result = islessequal(a, b);
          cout << a << " islessequal to " << b
               << ": " << result << endl;
        
          int x = 8;
          int y = 5;
        
          // Since 'x' is not less
          // than equal to 'y' so answer
          // is false(0)
          result = islessequal(x, y);
          cout << x << " islessequal to " << y
               << ": " << result;
        
          return 0;
      }

      
      

      Note: Using this function you can compare any data type with any other data type as well.

    • Program 2:




      // CPP code to illustrate
      // the use of islessequal function
      #include <bits/stdc++.h>
      using namespace std;
        
      int main()
      {
          // Take any two values
          bool result;
          float a = 80.23;
          int b = 82;
        
          // Since 'a' is less
          // than equal to 'b' so answer
          // is true(1)
          result = islessequal(a, b);
          cout << a << " islessequal to " << b
               << ": " << result << endl;
        
          char x = 'c';
        
          // Since 'c' ascii value(99) is not
          // less than variable a so answer
          // is false(0)
          result = islessequal(x, a);
          cout << x << " islessequal to " << a
               << ": " << result;
        
          return 0;
      }

      
      

      Output:

      80.23 islessequal to 82: 1
      c islessequal to 80.23: 0
      

    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 
    


    Last Updated : 28 Apr, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments