Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is copysign() in C?

Kainat Asif

The copysign() function is used to copy the sign of a number to the magnitude of another number and is a part of the math.h header file in C.

Syntax

The declaration of the copysign() function is shown below:

double copysign (double a, double b);

The copysign() function takes a and b as parameters and returns a number with the same magnitude as a and the same sign as b.

Code

Consider the code snippet below, which shows the implementation of the copysign() function:

#include <stdio.h>
#include <math.h>

int main() {

  double a = 1.55;
  double b = -2.33;

  double x = copysign(a, b);

  printf("copysign ( %f, %f ) = ( %lf ) \n", a, b, x);
  
  return 0;
}

Explanation

The copysign() function is used in line 9 to compute a number with the same magnitude as a (created in line 6) and the same sign as b(created in line 7).

RELATED TAGS

c

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring