isfinite() function in C++

The isfinite() function is a builtin function in C++ and is used to determine whether a given value if finite or not. A finite value is a value that is neither infinite nor NAN. If the number is finite then the function returns 1 else returns zero.

Syntax:

bool isfinite(float x);  

or,
bool isfinite(double x);

or,  
bool isfinite(long double x); 

Parameter: This function takes only one parameter x. It represents the floating point number.

Returns: If the number is infinite or NAN then it returns 0 else if it is finite then it returns 1.

Below programs illustrate the isfinite() function in C++:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illutrate the
// isfinite() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    float x = 19.0;
      
    cout<<"The value of x is = "<< x << endl;
  
    // Here function check whether 19 is finite or not
    // if yes function returns 1, else 0
    cout<<"isfinite(x) = "<<isfinite(x);
      
    return 0;
}

chevron_right


Output:

The value of x is = 19
isfinite(x) = 1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illutrate the
// isfinite() function
  
#include <bits/stdc++.h>
using namespace std; 
  
int main() 
    float x=9.6/0.0; 
      
    cout<<"The value of x is = "<< x << endl; 
      
    cout<<"isfinite(x) = "<<isfinite(x); 
      
    return 0; 

chevron_right


Output:

The value of x is = inf
isfinite(x) = 0

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illutrate the
// isfinite() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{   
    // Value is NAN
    double x=0.0/0.0; 
      
    cout<<"Value of x is = "<< x << endl;
      
    cout<<"isfinite(x) = "<<isfinite(x); 
      
    return 0;
}

chevron_right


Output:

Value of x is = -nan
isfinite(x) = 0


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.