8.2 Numerical Functions

These functions perform various arithmetic operations on numbers.

Function: cl-gcd &rest integers

This function returns the Greatest Common Divisor of the arguments. For one argument, it returns the absolute value of that argument. For zero arguments, it returns zero.

Function: cl-lcm &rest integers

This function returns the Least Common Multiple of the arguments. For one argument, it returns the absolute value of that argument. For zero arguments, it returns one.

Function: cl-isqrt integer

This function computes the “integer square root” of its integer argument, i.e., the greatest integer less than or equal to the true square root of the argument.

Function: cl-floor number &optional divisor

With one argument, cl-floor returns a list of two numbers: The argument rounded down (toward minus infinity) to an integer, and the “remainder” which would have to be added back to the first return value to yield the argument again. If the argument is an integer x, the result is always the list (x 0). If the argument is a floating-point number, the first result is a Lisp integer and the second is a Lisp float between 0 (inclusive) and 1 (exclusive).

With two arguments, cl-floor divides number by divisor, and returns the floor of the quotient and the corresponding remainder as a list of two numbers. If (cl-floor x y) returns (q r), then q*y + r = x, with r between 0 (inclusive) and r (exclusive). Also, note that (cl-floor x) is exactly equivalent to (cl-floor x 1).

This function is entirely compatible with Common Lisp’s floor function, except that it returns the two results in a list since Emacs Lisp does not support multiple-valued functions.

Function: cl-ceiling number &optional divisor

This function implements the Common Lisp ceiling function, which is analogous to floor except that it rounds the argument or quotient of the arguments up toward plus infinity. The remainder will be between 0 and minus r.

Function: cl-truncate number &optional divisor

This function implements the Common Lisp truncate function, which is analogous to floor except that it rounds the argument or quotient of the arguments toward zero. Thus it is equivalent to cl-floor if the argument or quotient is positive, or to cl-ceiling otherwise. The remainder has the same sign as number.

Function: cl-round number &optional divisor

This function implements the Common Lisp round function, which is analogous to floor except that it rounds the argument or quotient of the arguments to the nearest integer. In the case of a tie (the argument or quotient is exactly halfway between two integers), it rounds to the even integer.

Function: cl-mod number divisor

This function returns the same value as the second return value of cl-floor.

Function: cl-rem number divisor

This function returns the same value as the second return value of cl-truncate.

Function: cl-parse-integer string &key start end radix junk-allowed

This function implements the Common Lisp parse-integer function. It parses an integer in the specified radix from the substring of string between start and end. Any leading and trailing whitespace chars are ignored. The function signals an error if the substring between start and end cannot be parsed as an integer, unless junk-allowed is non-nil.