ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.