Reference · Math

math.copysign

signature
math.copysign(num, num) -> float

What it does

The first number's magnitude with the second number's sign, as a float, as Python's math.copysign: math.copysign(3, -1) is -3.0. Only the sign of the second number counts, not its size.

Examples

print(math.copysign(3, -1), math.copysign(-3, 1), math.copysign(-3, -100))print(math.copysign(2.5, 0), type_of(math.copysign(1, 1)))
output
-3.0 3.0 -3.02.5 float

Edge cases

print(math.copysign(5, -0.0), math.copysign(5, 0), math.copysign(0.0, -1))print(math.copysign(float("inf"), -1), math.copysign(1, float("nan")))
output
-5.0 5.0 -0.0-inf 1.0

Errors

An argument that is not a number is a runtime error, not an error value: both arguments must be numbers.

this one fails
print(math.copysign(3, "-"))
output
line 1: `math.copysign` expects a number as argument 2, got str

Chaining

In a pipeline the magnitude comes first, so x | math.copysign(s) gives x the sign of s. Rounding a value away from zero to a step:

print(2.5 | math.copysign(-1))let away = (x) => math.copysign(math.ceil(math.fabs(x)), x)print(away(2.1), away(-2.1))
output
-2.53.0 -3.0

See also

math.fabs, abs, math.ceil

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On math.copysign(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo