zerop
zerop Function
Syntax:
zerop number → generalized-boolean
Pronunciation:
[ z—e( )r—o( )p—e ]
Arguments and Values:
number—a number .
generalized-boolean—a generalized boolean.
Description:
Returns true if number is zero (integer , float, or complex ); otherwise, returns false.
Regardless of whether an implementation provides distinct representations for positive and negative floating-point zeros, (zerop -0.0) always returns true.
Examples:
(zerop 0) → true
(zerop 1) → false
(zerop -0.0) → true
(zerop 0/100) → true
(zerop #c(0 0.0)) → true
Exceptional Situations:
Should signal an error of type type-error if number is not a number .
Notes:
(zerop number) ≡ (= number 0)
Expanded Reference: zerop
Testing for zero
zerop returns true if the number is zero, regardless of its numeric type.
(zerop 0)
=> T
(zerop 1)
=> NIL
(zerop -1)
=> NIL
With floating-point numbers
zerop recognizes both positive and negative floating-point zero.
(zerop 0.0)
=> T
(zerop -0.0)
=> T
(zerop 0.0d0)
=> T
(zerop 0.001)
=> NIL
With rationals
Zero ratios are also recognized as zero.
(zerop 0/100)
=> T
(zerop 0/1)
=> T
(zerop 1/1000000)
=> NIL
With complex numbers
A complex number is zero only if both its real and imaginary parts are zero.
(zerop #c(0 0))
=> T
(zerop #c(0 0.0))
=> T
(zerop #c(0 1))
=> NIL
Practical use: safe division guard
zerop is commonly used to guard against division by zero.
(defun safe-divide (a b)
(if (zerop b)
(values nil "division by zero")
(/ a b)))
(safe-divide 10 3)
=> 10/3
(safe-divide 10 0)
=> NIL
=> "division by zero"