×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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.

Sample Input and Output

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

Example

// 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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.