Python | math.copysign() function

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.copysign(a, b) function return a float with the magnitude (absolute value) of a but the sign of b.

Syntax: math.copysign(a, b)

Parameter:
a, b: numeric values.

Returns:  Return absolute value of a but the sign of b.

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the working of copysign()
    
# importing "math" for mathematical operations 
import math 
    
a = 5.2
b = -25
    
# returning the copysign 
print ("The copysign of 5.2 and -25 is : ", end ="") 
print (math.copysign(a, b))

chevron_right


Output:



The copysign of 5.2 and -25 is : -5.2

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the working of factorial()
  
# importing "math" for mathematical operations 
import math 
  
# returning the copysign
print (math.copysign(-13, 25.5))
print (math.copysign(2.87, -15))

chevron_right


Output:

13.0
-2.87


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.