isfinite() function in C++
Last Updated :
14 Feb, 2023
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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
float x = 19.0;
cout<< "The value of x is = " << x << endl;
cout<< "isfinite(x) = " <<isfinite(x);
return 0;
}
|
Output: The value of x is = 19
isfinite(x) = 1
Program 2:
CPP
#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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
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
Share your thoughts in the comments
Please Login to comment...