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
(/ 7 2.0) ; 3.5

WARNING: 2. is still a int. 2.0 is a 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.

Mod, Remainder

(% 7 4) ; 3

Power; Exponential

(expt 2 3) ; 8

(info "(elisp) Numbers")

Lisp Basics

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Lisp Basics

Basics

Lisp Data Structure

Function

Lisp Symbol

Lisp Misc

Working with Elisp