Emacs Lisp: Arithmetic

By Xah Lee. Date: . Last updated: .
;; addition
(+ 4 5 1) ; 10
;; Subtraction
(- 9 2) ; 7
(- 9 2 3) ; 4
;; Multiplication
(* 2 3) ; 6
(* 2 3 2) ; 12
;; Division

;; WARNING: 7 divided by 2 returns 3, not 3.5,
;; because the division function return a int when both operands are int.

;; integer part of quotient
(/ 7 2) ; 3

;; WARNING: 2. is still a int. 2.0 is a float.
(/ 7 2.) ; 3

;; int divide by float return float
(/ 7 2.0) ; 3.5
;; test if a number is integer or float

(integerp 2) ; t
(integerp 2.) ; t
(integerp 2.0) ; nil

(floatp 2) ; nil
(floatp 2.) ; nil
(floatp 2.0) ; t

Note: Function names that end with a “p” often means it return either true or false. (The “p” stands for “predicate”) t means true; nil means false.

;; modular arithmetic, remainder
(% 7 4) ; 3
;; Power; Exponential
(expt 2 3) ; 8

Numbers (ELISP Manual)

Emacs Lisp Numbers