How to use copysign() function to get a value with the magnitude of x and the sign of y in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	cout << "copysign( 10.0, -1.0) = " << copysign(10.0, -1.0) << endl;
	cout << "copysign(-10.0, -1.0) = " << copysign(-10.0, -1.0) << endl;
	cout << "copysign(-10.0,  1.0) = " << copysign(-10.0, 1.0) << endl;
	cout << "copysign(  1.0, +2.0) = " << copysign(1.0, +2.0) << endl;
	cout << "copysign(  1.0, -2.0) = " << copysign(1.0, -2.0) << endl;

	return 0;
}


/*
run:

copysign( 10.0, -1.0) = -10
copysign(-10.0, -1.0) = -10
copysign(-10.0,  1.0) = 10
copysign(  1.0, +2.0) = 1
copysign(  1.0, -2.0) = -1

*/

 



answered Mar 14, 2016 by avibootz
...