signbit() Function with Example in C++

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

C++ signbit() function

signbit() function is a library function of cmath header. It is used to check the sign of the given value. It accepts a parameter (float, double or long double) and returns 1 if the given value is negative; 0, otherwise.

Syntax of signbit() function:

In C99, it has been implemented as a macro,

    signbit(x)

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

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

Parameter(s):

  • x – represents the value to check its sign.

Return value:

It returns 1 if x is negative; 0, otherwise.

Example:

    Input:
    double x = 10.0;
    
    Function call:
    signbit(x);
    
    Output:
    0

    Input:
    double x = -10.0;
    
    Function call:
    signbit(x);
    
    Output:
    1

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

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

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

// main() section
int main()
{
    double x = 0.0;

    x = 10.0;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = -10.0;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = 10.10;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = -10.10;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = 0.0;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = -10.0 / 2.5;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = 10.0 / -2.5;
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    x = sqrt(-1);
    cout << "signbit(" << x << "): " << signbit(x) << endl;

    return 0;
}

Output

signbit(10): 0
signbit(-10): 1
signbit(10.1): 0
signbit(-10.1): 1
signbit(0): 0
signbit(-4): 1
signbit(-4): 1
signbit(-nan): 1

Reference: C++ signbit() function



Comments and Discussions!

Load comments ↻





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