Open In App

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 . 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++: 



Time Complexity: O(1)

Auxiliary Space: O(1)



Program 1:

// C++ program to illustrate 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;
}

                    

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

 

Program 2: 
 

// C++ program to illustrate 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;
}

                    

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

 

Program 3: 
 

// C++ program to illustrate 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;
}

                    

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

 

Article Tags :
C++