std::copysign, std::copysignf, std::copysignl
| Defined in header <cmath>
|
||
| float copysign ( float x, float y ); float copysignf( float x, float y ); |
(1) | (since C++11) |
| double copysign ( double x, double y ); |
(2) | (since C++11) |
| long double copysign ( long double x, long double y ); long double copysignl( long double x, long double y ); |
(3) | (since C++11) |
| Promoted copysign ( Arithmetic1 x, Arithmetic2 y ); |
(4) | (since C++11) |
x and the sign of y. Promoted is also long double, otherwise the return type is always double.Contents |
[edit] Parameters
| x, y | - | floating point values |
[edit] Return value
If no errors occur, the floating point value with the magnitude of x and the sign of y is returned.
If x is NaN, then NaN with the sign of y is returned.
If y 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),
- The returned value is exact (FE_INEXACT is never raised) and independent of the current rounding mode.
[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 << "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
| (C++11)(C++11) |
absolute value of a floating point value (|x|) (function) |
| (C++11) |
checks if the given number is negative (function) |
| C documentation for copysign
| |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
