math.copysign()

Published Aug 17, 2024
Contribute to Docs

The math.copysign() function in Python takes two numeric values and returns a float with the magnitude (absolute value) of the first value and the sign of the second value.

Syntax

math.copysign(x, y)
  • x: A numeric value (int or float) whose magnitude is used in the result.
  • y: A numeric value (int or float) whose sign is used in the result.

Example

In the example below, the math.copysign() function is used to return 10 with the same sign as -5:

import math
print(math.copysign(10, -5))

The above code gives the following output:

-10.0

Codebyte Example

The following codebyte example demonstrates how the math.copysign() function works:

Code
Output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy