copysign
C ++ copysign()函数 (C++ copysign() function)
copysign() function is a library function of cmath header, it is used to get the first argument's value with the sign of the second argument, it accepts two arguments (x, y) and returns magnitude of x with the sign of y.
copysign()函数是cmath标头的库函数,用于获取带有第二个参数的符号的第一个参数的值,它接受两个参数( x,y )并以y的符号返回x的大小。
Syntax of copysign() function:
copysign()函数的语法:
copysign(x, y);
Parameter(s): x, y – are the numbers to get the magnitude of x with the sign of y.
参数(S):X,Y -是得到的x与y的符号幅度的数字。
Return value: float/double/long double – based on the given type, it returns the number (x) having the sign of y.
返回值: float / double / long double-根据给定的类型,它返回带有符号y的数字( x )。
Example:
例:
Input:
float x = 2.5;
float y = -2;
Function call:
copysign(x, y);
Output:
-2.5
C ++代码演示copysign()函数的示例 (C++ code to demonstrate the example of copysign() function)
// C++ code to demonstrate the example of
// copysign() function
#include <iostream>
#include <cmath>
using namespace std;
// main() section
int main()
{
float x;
float y;
x = 2.5;
y = -2;
cout<<"copysign("<<x<<","<<y<<"): "<<copysign(x,y)<<endl;
x = 2.5;
y = 2;
cout<<"copysign("<<x<<","<<y<<"): "<<copysign(x,y)<<endl;
x = 2;
y = -2;
cout<<"copysign("<<x<<","<<y<<"): "<<copysign(x,y)<<endl;
x = 2;
y = 2;
cout<<"copysign("<<x<<","<<y<<"): "<<copysign(x,y)<<endl;
x = -2.5;
y = -2.5;
cout<<"copysign("<<x<<","<<y<<"): "<<copysign(x,y)<<endl;
return 0;
}
Output
输出量
copysign(2.5,-2): -2.5
copysign(2.5,2): 2.5
copysign(2,-2): -2
copysign(2,2): 2
copysign(-2.5,-2.5): -2.5
Reference: C++ copysign() function
翻译自: https://www.includehelp.com/cpp-tutorial/copysign-function-with-example.aspx
copysign