Namespaces
Variants
Views
Actions

std::copysign, std::copysignf, std::copysignl

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
Nearest integer floating point operations
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Floating point manipulation functions
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
copysign
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 
Defined in header <cmath>
float       copysign ( float mag, float sgn );
float       copysignf( float mag, float sgn );
(1) (since C++11)
double      copysign ( double mag, double sgn );
(2) (since C++11)
long double copysign ( long double mag, long double sgn );
long double copysignl( long double mag, long double sgn );
(3) (since C++11)
Promoted    copysign ( Arithmetic1 mag, Arithmetic2 sgn );
(4) (since C++11)
1-3) Composes a floating point value with the magnitude of mag and the sign of sgn.
4) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by (1-3). If any argument has integral type, it is cast to double. If any argument is long double, then the return type Promoted is also long double, otherwise the return type is always double.

Contents

[edit] Parameters

mag, sgn - floating point values

[edit] Return value

If no errors occur, the floating point value with the magnitude of mag and the sign of sgn is returned.

If mag is NaN, then NaN with the sign of sgn is returned.

If sgn is -0, the result is only negative if the implementation supports the signed zero consistently in arithmetic operations.

[edit] Error handling

This function is not subject to any errors specified in math_errhandling.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

[edit] Notes

std::copysign is the only portable way to manipulate the sign of a NaN value (to examine the sign of a NaN, signbit may also be used)

[edit] Example

#include <iostream>
#include <cmath>
 
int main(void)
{
    std::cout << std::showpos
              << "copysign(1.0,+2.0) = " << std::copysign(1.0,+2.0) << '\n'
              << "copysign(1.0,-2.0) = " << std::copysign(1.0,-2.0) << '\n'
              << "copysign(inf,-2.0) = " << std::copysign(INFINITY,-2.0) << '\n'
              << "copysign(NaN,-2.0) = " << std::copysign(NAN,-2.0) << '\n';
}

Output:

copysign(1.0,+2.0) = +1
copysign(1.0,-2.0) = -1
copysign(inf,-2.0) = -inf
copysign(NaN,-2.0) = -nan

[edit] See also

absolute value of a floating point value (|x|)
(function) [edit]
(C++11)
checks if the given number is negative
(function) [edit]
C documentation for copysign