copysign() Function

The copysign() function returns a value that retains the magnitude of one operand while adopting the sign of the other. This function is useful in numerical computations where you need to control the sign of a result without modifying its absolute value.


Syntax of copysign()

</>
Copy
double copysign(double x, double y);
</>
Copy
float copysignf(float x, float y);
</>
Copy
long double copysignl(long double x, long double y);

Parameters

ParameterDescription
xValue whose magnitude is used in the resulting value.
yValue whose sign is used in the resulting value.

Return Value

The function returns a value that has the magnitude of x and the sign of y.

The copysign() function is particularly useful when the sign of a numerical result needs to be explicitly controlled in computations without affecting the magnitude. This makes it ideal for applications involving complex numerical algorithms where sign manipulation is required.


Examples for copysign()

Example 1: Basic Usage to Assign a Negative Sign

This example demonstrates how copysign() is used to assign a negative sign to a positive magnitude.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double magnitude = 5.0;
    double signSource = -3.0;
    
    double result = copysign(magnitude, signSource);
    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The variable magnitude is set to 5.0, which provides the magnitude for the result.
  2. The variable signSource is set to -3.0, providing the negative sign.
  3. The function copysign() returns -5.0, combining the magnitude of 5.0 with the sign of -3.0.

Program Output:

Result: -5.000000

Example 2: Applying copysign() on Floating-Point Values

This example illustrates the use of copysignf() with float values to ensure the sign is transferred while the magnitude remains unchanged.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    float a = 7.5f;
    float b = -2.0f;
    
    float result = copysignf(a, b);
    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The variable a is initialized with a positive float value of 7.5f.
  2. The variable b is initialized with a negative float value of -2.0f.
  3. The function copysignf() returns -7.5f, preserving the magnitude of a while adopting the sign of b.

Program Output:

Result: -7.500000

Example 3: Demonstrating copysignl() with Long Double Values

This example demonstrates the use of copysignl() with long double values, showing how the function transfers the sign correctly while preserving the magnitude.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    long double m = 12.34L;
    long double s = -0.0L;
    
    long double result = copysignl(m, s);
    printf("Result: %Lf\n", result);
    return 0;
}

Explanation:

  1. The variable m is assigned a long double value of 12.34L, which sets the magnitude.
  2. The variable s is assigned -0.0L to provide a negative sign.
  3. The function copysignl() returns -12.34L, merging the magnitude of m with the sign of s.

Program Output:

Result: -12.340000