The signbit
function in C determines whether a floating-point number is negative.
The signbit of a negative number is set as
1
This function checks if the given number’s signbit is set.
The signbit
function can be accessed via the math.h
library.
#include <math.h>
The signbit
function only takes one parameter: the floating-point number that needs to be ascertained as negative.
The syntax for the function call is:
int returned_val = signbit(x);
The function will return 0
if the floating-point parameter is non-negative (i.e., the signbit is not set). Otherwise, the number is negative and a non-zero number is returned.
#include <stdio.h>#include <math.h>int main() {double num1 = 1.2;double num2 = -1.2;// signbit returns 0printf("num1 is %s\n", signbit(num1)? "negative" : "not negative");// signbit returns non-zero numberprintf("num2 is %s\n", signbit(num2)? "negative" : "not negative");return 0;}
Free Resources