copysign() Function in C
The copysign() function in C is part of the standard math library <math.h> and is used to create a floating-point number that combines the magnitude of one number with the sign of another. It copies the sign from one floating-point number and applies it to the magnitude of another floating-point number.
In this article, we will learn about the copysign() function in C and
Syntax of copysign() in C
double copysign(double x, double y);
Parameter
The copysign() function in C takes the following parameters:
- x: The value whose magnitude is to be used.
- y: The value whose sign is to be copied.
Return Value
The copysign() function returns a value that has the same magnitude as x but the sign of y.
Variants of copysign() in C
Apart from the standard copysign() function, there are two more variants of the copysign() function, based on the type of floating-point numbers:
copysignf() for float
float copysignf(float x, float y);
copysignl() for long double
long double copysignl(long double x, long double y);
Note: The operation performed by copysign is at the bit level, making it both efficient and reliable.
Examples of copysign() Function in C Language
Example 1: Program to demonstrate how to use the copysign() function
// C Program to demonstrate the copysign() function
#include <math.h>
#include <stdio.h>
int main() {
double x = 10.5, y = -5.5;
double result = copysign(x, y);
// Print the result
printf("The value with magnitude %.2lf and sign %.2lf is: %.2lf\n", x, y, result);
return 0;
}
Output
The value with magnitude 10.50 and sign -5.50 is: -10.50
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2: Program to demonstrate how to use the variants of copysign()
// C program to demonstrate the copysign() function for
// different data types
#include <math.h>
#include <stdio.h>
int main()
{
double x1 = 10.5, y1 = -5.5;
float x2 = 20.5f, y2 = -6.5f;
long double x3 = 30.5l, y3 = -7.5l;
// copysign function for double
double result1 = copysign(x1, y1);
// copysignf function for float
float result2 = copysignf(x2, y2);
// copysignl function for long double
long double result3 = copysignl(x3, y3);
// Print the results
printf("The value with magnitude %.2lf and sign %.2lf "
"is: %.2lf\n",
x1, y1, result1);
printf("The value with magnitude %.2f and sign %.2f "
"is: %.2f\n",
x2, y2, result2);
printf("The value with magnitude %.2Lf and sign %.2Lf "
"is: %.2Lf\n",
x3, y3, result3);
return 0;
}
Output
The value with magnitude 10.50 and sign -5.50 is: -10.50 The value with magnitude 20.50 and sign -6.50 is: -20.50 The value with magnitude 30.50 and sign -7.50 is: -30.50
Time Complexity: O(1)
Auxiliary Space: O(1)
Practical Applications of copysign() Function
The copysign() function is particularly useful in various scenarios, such as:
- When performing complex calculations, ensuring the correct sign of intermediate results can be crucial.
- In computer graphics and physical simulations, vectors and forces often need to be manipulated with specific signs.
- Copying signs can be important when dealing with waveforms and other signals in signal processing.