Open In App

copysign() function in C++

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

copysign(x, y) function returns the value with a magnitude of x and the sign of y. 

Examples:

Input : copysign(6, -2)
Output : -6

Input : copysign(-6, 2)
Output : 6

Syntax:

copysign(x, y);

Parameters:

x : Value with the magnitude 
y : Value with the sign 

Returns :

Returns the value with a magnitude of x
and the sign of y.
Return type follows type casting i.e., 
if If one element is float and second 
element int then it returns float. 

Time Complexity: O(1)

Auxiliary Space: O(1)

Below is the implementation of the above: 

CPP




// C++ program to return copysign value
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    cout << "Magnitude = 6 Sign = -2 " << endl;
    cout << "Copysign(6, -2) = " << copysign(6, -2) << endl;
 
    cout << endl;
 
    cout << "Magnitude = -6 Sign = 2 " << endl;
    cout << "Copysign(-6, 2) = " << copysign(-6, 2);
 
    return 0;
}


Output

Magnitude = 6 Sign = -2 
Copysign(6, -2) = -6

Magnitude = -6 Sign = 2 
Copysign(-6, 2) = 6

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads