isfinite() function with example in C++

C++ isfinite() function: Here, we are going to learn about the isfinite() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on May 17, 2020

C++ isfinite() function

isfinite() function is a library function of cmath header, it is used to check whether the given value is a finite value or not? It accepts a value (float, double or long double) and returns 1 if the value is finite; 0, otherwise.

Syntax of isfinite() function:

In C99, it has been implemented as a macro,

    macro isfinite(x)

In C++11, it has been implemented as a function,

    bool isfinite (float x);
    bool isfinite (double x);
    bool isfinite (long double x);

Parameter(s):

  • x – represents a value to be checked as finite value.

Return value:

The returns type of this function is bool, it returns 1 if x is a finite value; 0, otherwise.

Example:

    Input:
    float x = 10.0f;
    
    Function call:
    isfinite(x);    
    
    Output:
    1

C++ code to demonstrate the example of isfinite() function

// C++ code to demonstrate the example of
// isfinite() function

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    cout << "isfinite(0.0): " << isfinite(0.0) << endl;
    cout << "isfinite(0.0/0.0): " << isfinite(0.0 / 0.0) << endl;
    cout << "isfinite(0.0/1.0): " << isfinite(0.0 / 1.0) << endl;
    cout << "isfinite(1.0/0.0): " << isfinite(1.0 / 0.0) << endl;

    float x = 10.0f;

    // checking finite value using the condition
    if (isfinite(x)) {
        cout << x << " is a finite value." << endl;
    }
    else {
        cout << x << " is not a finite value." << endl;
    }

    x = 10.0f / 0.0f;

    if (isfinite(x)) {
        cout << x << " is a finite value." << endl;
    }
    else {
        cout << x << " is not a finite value." << endl;
    }

    return 0;
}

Output

isfinite(0.0): 1
isfinite(0.0/0.0): 0
isfinite(0.0/1.0): 1
isfinite(1.0/0.0): 0
10 is a finite value.
inf is not a finite value.

Reference: C++ isfinite() function







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.