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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    Output:

    5.5 islessequal nan: 0
    

    Examples:

    • Program 1:
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // 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;
      }

      chevron_right

      
      

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

    • Program 2:
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // 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;
      }

      chevron_right

      
      

      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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    Output:

    1 2 3 4 5 6 7 8 9 10 
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.