Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ cmath copysign() function is used to return a number with magnitude of first argument and sign of second argument.

Syntax

double copysign (double x, double y);
float copysign (float x, float y);
long double copysign (long double x, long double y);
// additional overloads 
double copysign (Type1 x, Type2 y);                                 

Parameters

x Specify a value providing the magnitude of the result.
y Specify a value providing the sign of the result.

Return Value

Returns a number with magnitude of first argument and sign of second argument.

Example:

In the below example, copysign() function is used to return a number with magnitude of first argument and sign of second argument.

#include <iostream>
#include <cmath>

using namespace std;
 
int main (){
  cout<<copysign(-324.1, 4)<<"\n";
  cout<<copysign(500, -21)<<"\n";
  cout<<copysign(-40.2, -15)<<"\n";
  return 0;
}

The output of the above code will be:

324.1
-500
-40.2

❮ C++ <cmath> Library