What is copysignf in C?
The copysignf function returns the value formed by combining the magnitude of the first parameter and the sign of the second parameter.
The copysignf function is a variation of the copysign function. Thef in the variation refers to the float datatype of the parameter and return value.
Library
The copysignf function can be accessed via the math.h library.
Syntax
float copysignf(float x, float y)
Parameters
The copysignf function takes two parameters of the float datatype.
Return value
If no errors occur, the copysignf function returns a value of float datatype. This value has the magnitude of the first parameter and the sign of the second parameter.
If the first parameter is NaN (not a number), then the return value is NaN with the sign of the second parameter.
If the second parameter is -0, a negative value is returned (if a signed zero is supported in arithmetic operations).
The
copysignffunction provides a way to manipulate the sign ofNaN.
Code
The following code shows the return values associated to different kinds of parameters:
#include <stdio.h>#include <math.h>int main(){printf("copysignf(10,+4.0) = %f\n", copysignf(10,+4.0));printf("copysignf(20,-8.0) = %f\n", copysignf(20,-8.0));printf("copysignf(INFINITY,-2.0) = %f\n", copysignf(INFINITY,-2.0)); //%f is the string formatter for float numbersprintf("copysignf(NAN,-3.0) = %f\n", copysignf(NAN,-3.0));}
Free Resources