Python math.copysign() Method

Python math.copysign() method is used to return a float consisting of the value of the first parameter and the sign of the second parameter.

Visual Representation

Visual Representation of Python math.copysign() Method

Syntax

math.copysign(x, y)

Parameters

x(required): The value whose magnitude(absolute value) is used in the result

y(required): The value whose sign is used in the result.

Return Value

Returns a float value.

It returns a TypeError, if either x or y is not a number.

Example: How to Use math.copysign() Method

Visual Representation of an Example Using the math.copysign() Method

import math

print(math.copysign(8, 4))
print(math.copysign(8, -4))
print(math.copysign(-8, 4))
print(math.copysign(-8, -4))
print(math.copysign(10, -2.8)) 

Output

8.0
-8.0
8.0
-8.0
-10.0

That’s it.

Related posts

Python math.ldexp()

Python math.atanh()

Python math.lgamma()

Python math.acosh()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.