isnormal() Function with Example in C++

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

C++ isnormal() function

isnormal() function is a library function of cmath header, it is used to check whether the given value is a normal value i.e. the value should not be infinity, NaN, Zero, or subnormal. It accepts a value (float, double or long double) and returns 1 if the given value is a normal value; 0, otherwise.

Syntax of isnormal() function:

In C99, it has been implemented as a macro,

    macro isnormal(x)

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

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

Parameter(s):

  • x – represents the value to be checked as normal value.

Return value:

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

Example:

    Input:
    float x = 1.0f;
    
    Function call:
    isnormal(x);    
    
    Output:
    1

    Input:
    float x = sqrt(-1.0f);
    
    Function call:
    isnormal(x);    
    
    Output:
    0

    Input:
    float x = 0.0f;
    
    Function call:
    isnormal(x);    
    
    Output:
    0

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

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

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

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

    float x = sqrt(-1.0f);

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

    x = sqrt(2);

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

    return 0;
}

Output

isnormal(0.0f))      : 0
isnormal(0.0f/0.0f)  : 0
isnormal(0.0f/1.0f)  : 0
isnormal(1.0f)       : 1
isnormal(sqrt(-1.0f)): 0
-nan is not normal value.
1.41421 is normal value.

Reference: C++ isnormal() function



Comments and Discussions!

Load comments ↻





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