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!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.