Reference · Math
math.copysign
signature
math.copysign(num, num) -> floatWhat 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 floatEdge cases
-0.0carries a sign:math.copysign(5, -0.0)is-5.0, whilemath.copysign(5, 0)is5.0.- Infinity takes a sign like any number:
math.copysign(inf, -1)is-inf. - NaN as the magnitude stays NaN; NaN as the sign counts as positive on this platform, as its sign bit happens to be.
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.0Errors
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 strChaining
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.0See also
Thread
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