copysign(x, y)
Returns x with sign of y. Data type of return value is float. print(math.copysign(3, -2)) # -3.0
print(math.copysign(-3, -2)) # -3.0
print(math.copysign(-3, 2)) # 3.0
print(math.copysign(3, 2)) # 3.0
Switching signs:
import math
result = math.copysign(5, -3)
print(result) # Output: -5.0
Ensuring positive values:
result = math.copysign(-7, 4)
print(result) # Output: 7.0
Handling zero:
result = math.copysign(0, -2)
print(result) # Output: -0.0
Finance:import math
loss = math.copysign(5000, -1) # Apply a negative sign to represent loss
print(loss) # Output: -5000
Physics:velocity = math.copysign(20, -1) # Negative velocity means moving in the opposite direction
print(velocity) # Output: -20
import math
print(math.copysign(3, -5)) # Output: -3.0
print(math.copysign(-4, 2)) # Output: 4.0
print(math.copysign(0, -1)) # Output: -0.0
print(math.copysign(0, 1)) # Output: 0.0
values = [10, -20, 15, -25]
adjusted = [math.copysign(val, -1) for val in values]
print(adjusted)
Output
[-10.0, -20.0, -15.0, -25.0]
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.