Skip to main content

signum

signum Function

Syntax:

signum number → signed-prototype

Arguments and Values:

number—a number .

signed-prototype—a number .

Description:

signum determines a numerical value that indicates whether number is negative, zero, or positive.

For a rational, signum returns one of -1, 0, or 1 according to whether number is negative, zero, or positive. For a float, the result is a float of the same format whose value is minus one, zero, or one. For a complex number z, (signum z) is a complex number of the same phase but with unit magnitude, unless z is a complex zero, in which case the result is z.

For rational arguments, signum is a rational function, but it may be irrational for complex arguments.

If number is a float, the result is a float. If number is a rational, the result is a rational. If number is a complex float, the result is a complex float. If number is a complex rational, the result is a complex , but it is implementation-dependent whether that result is a complex rational or a complex float.

Examples:

(signum 0)0 
(signum 99)1
(signum 4/5)1
(signum -99/100)-1
(signum 0.0)0.0
(signum #c(0 33)) → #C(0.0 1.0)
(signum #c(7.5 10.0)) → #C(0.6 0.8)
(signum #c(0.0 -14.7)) → #C(0.0 -1.0)
(eql (signum -0.0) -0.0) → true


See Also:

Section 12.1.3.3 (Rule of Float Substitutability)

Notes:

(signum x) (if (zerop x) x (/ x (abs x)))

Expanded Reference: signum

Sign of integer and rational numbers

For rationals, signum returns -1, 0, or 1.

(signum 99)
=> 1
(signum -42)
=> -1
(signum 0)
=> 0
(signum 4/5)
=> 1
(signum -99/100)
=> -1

Sign of floating-point numbers

For floats, signum returns a float of the same format: -1.0, 0.0, or 1.0.

(signum 3.14)
=> 1.0
(signum -2.5)
=> -1.0
(signum 0.0)
=> 0.0
(signum 1.0d0)
=> 1.0d0

Negative zero

signum preserves negative zero.

(eql (signum -0.0) -0.0)
=> T

Complex numbers

For complex numbers, signum returns a complex number with the same phase but unit magnitude. A complex zero returns itself.

(signum #c(0 33))
=> #C(0.0 1.0)
(signum #c(0.0 -14.7))
=> #C(0.0 -1.0)
(signum #c(7.5 10.0))
=> #C(0.6 0.8)
(signum #c(3 4))
=> #C(0.6 0.8)

Relationship to abs

signum satisfies the identity: (* (signum x) (abs x)) equals x for all numbers.

(let ((x -42))
(= (* (signum x) (abs x)) x))
=> T

(let ((x 3.5))
(= (* (signum x) (abs x)) x))
=> T