📜  C++中的copysign()函数

📅  最后修改于: 2021-05-30 12:04:14             🧑  作者: Mango

copysign(x,y)函数返回值为x且值为y的值。

例子:

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

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

句法:

copysign(x, y);

参数:

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

返回值:

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. 

下面是上述的实现:

// C++ program to return copysign value
#include      
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;
}

输出:

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

Magnitude = -6  Sign = 2
Copysign(-6, 2) = 6
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”