copysign_C ++中带示例的copysign()函数

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

参考: C ++ copysign()函数

翻译自: https://www.includehelp.com/cpp-tutorial/copysign-function-with-example.aspx

copysign

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值