copysign()
函数copysign()函数是cmath标头的库函数,用于获取带有第二个参数的符号的第一个参数的值,它接受两个参数(x,y)并以y的符号返回x的大小。
copysign()
函数语法:
copysign(x, y);
参数: x,y –是以y为符号获得x大小的数字。
返回值: float / double / long double-根据给定的类型,它返回带有符号y的数字(x)。
示例
Input: float x = 2.5; float y = -2; Function call: copysign(x, y); Output: -2.5
copysign()
函数示例//示例 // copysign()功能 #include <iostream> #include <cmath> using namespace std; // main()部分 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; }
输出结果
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
参考:C ++copysign()
函数