当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ copysign()用法及代码示例


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 <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; 
}

输出:

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

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


相关用法


注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 copysign() function in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。