Name

copysign

Synopsis

Makes the sign of a number match that of another number

#include <math.h>
doublecopysign( double x, double y );
float copysignf( float x, float y );
long double copysignl( long double x, long double y );

The copysign() function returns a value with the magnitude of its first argument and the sign of its second argument.

Example

/* Test for signed zero values */
double x =copysign(0.0, -1.0);
double y = copysign(0.0, +1.0);

  printf( "x is %+.1f; y is %+.1f.\n", x, y);
  printf( "%+.1f is %sequal to %+.1f.\n", x, ( x == y ) ? "" : "not " , y )

This code produces the following output:

x is -0.0; y is +0.0.
-0.0 is equal to +0.0.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.