Home » C++ programming language

copysign() function with example in C++

C++ copysign() function: Here, we are going to learn about the copysign() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 28, 2019

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.

Syntax of copysign() function:

    copysign(x, y);

Parameter(s): x, y – are the numbers to get the magnitude of x with the sign of y.

Return value: float/double/long double – based on the given type, it returns the number (x) having the sign of y.

Example:

    Input:
    float x =  2.5;
    float y = -2;
    
    Function call:
    copysign(x, y);    
    
    Output:
    -2.5

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.