copysign() math function in python by R4R Team

- copysign() is a python math function which are able to change the sign of the numeric values.

Syntax-

math.copysign(num1,num2)

-Here copysign() takes two argument both are number, it can be positive or negative both.
-It return the float num1 with the sign of num2.

Example-

num1 is 4
num2 is -n
copysign(num1,num2) will return -4

num1 is -4
num2 is n
copysign(num1,num2) will return 4


program-

import math
a=4
b=-1
n=math.copysign(a,b)
print(n)

print(math.copysign(3,-2))
print(math.copysign(-3,-2))
print(math.copysign(-3,2))
print(math.copysign(3,2))


output-

-4.0
-3.0
-3.0
3.0
3.0


-In this program, we import the math library by import math, then we have a two number 4 and -1 , copysign(4,-1) will return -4.0 because of num2 is negative number.

What if we try to put 0 at num1 and num2.

program-

import math
print(math.copysign(3,0))
print(math.copysign(0,-2))


output-

3.0
-0.0


-It will give no issue and work correctly and copysign(3,0) will return 3.0 that means 0 is also consider on positive number side.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!