當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。