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 <stdio.h>     
#include <math.h>
 
int main(int argc, char **argv)
{
	printf("copysign( 10.0, -1.0) = %.1f\n", copysign( 10.0, -1.0));
	printf("copysign(-10.0, -1.0) = %.1f\n", copysign(-10.0, -1.0));
	printf("copysign(-10.0,  1.0) = %.1f\n", copysign(-10.0,  1.0));
	printf("copysign(  1.0, +2.0) = %.1f\n", copysign(  1.0, +2.0));
    printf("copysign(  1.0, -2.0) = %.1f\n", copysign(  1.0, -2.0));
  
    return 0;
}

/*
run:
  
copysign( 10.0, -1.0) = -10.0
copysign(-10.0, -1.0) = -10.0
copysign(-10.0,  1.0) = 10.0
copysign(  1.0, +2.0) = 1.0
copysign(  1.0, -2.0) = -1.0

*/

 



answered Mar 14, 2016 by avibootz
...