Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is copysignf in C?

Sheza Munir

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.

Visual representation of the copysignf function
Visual representation of the copysignf function

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 copysignf function provides a way to manipulate the sign of NaN.

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 numbers
    printf("copysignf(NAN,-3.0)        = %f\n", copysignf(NAN,-3.0));
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring