isinf() Function with Example in C++

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

C++ isinf() function

isinf() function is a library function of cmath header, it is used to check whether the given value is infinite (negative infinity or positive infinity). It accepts a value (float, double or long double) and returns 1 if the given value is an infinity; 0, otherwise.

Syntax of isinf() function:

In C99, it has been implemented as a macro,

    macro isinf(x)

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

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

Parameter(s):

  • x – represents the value to be checked whether x is an infinity or not.

Return value:

The returns type of this function is bool, it returns 1 if x is an infinity; 0, otherwise.

Example:

    Input:
    float x = 10.0f/0.0f;
    
    Function call:
    isinf(x);
    
    Output:
    1

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

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

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

int main()
{
    cout << "isinf(1.0f/0.0f)  : " << isinf(1.0f / 0.0f) << endl;
    cout << "isinf(-1.0f/-0.0f): " << isinf(-1.0f / -0.0f) << endl;
    cout << "isinf(0.0/1.0)    : " << isinf(0.0 / 1.0) << endl;
    cout << "isinf(1.0/0.0)    : " << isinf(1.0 / 0.0) << endl;

    float x = 10.0f;

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

    x = 10.0f / 0.0f;

    if (isinf(x)) {
        cout << x << " is infinite value." << endl;
    }
    else {
        cout << x << " is not infinite value." << endl;
    }

    return 0;
}

Output

isinf(1.0f/0.0f)  : 1
isinf(-1.0f/-0.0f): 1
isinf(0.0/1.0)    : 0
isinf(1.0/0.0)    : 1
10 is not infinite value.
inf is infinite value.

Reference: C++ isinf() 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.