www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_263.html   search  
 
Buy the book!


Programming in Emacs Lisp

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

The % remainder function

To understand (% 1 length), we need to understand %. According to its documentation (which I just found by typing C-h f % RET), the % function returns the remainder of its first argument divided by its second argument. For example, the remainder of 5 divided by 2 is 1. (2 goes into 5 twice with a remainder of 1.)

What surprises people who don't often do arithmetic is that a smaller number can be divided by a larger number and have a remainder. In the example we just used, 5 was divided by 2. We can reverse that and ask, what is the result of dividing 2 by 5? If you can use fractions, the answer is obviously 2/5 or .4; but if, as here, you can only use whole numbers, the result has to be something different. Clearly, 5 can go into 2 zero times, but what of the remainder? To see what the answer is, consider a case that has to be familiar from childhood:

By considering the cases as parallel, we can see that

and so on.

So, in this code, if the value of length is 5, then the result of evaluating

 
(% 1 5)

is 1. (I just checked this by placing the cursor after the expression and typing C-x C-e. Indeed, 1 is printed in the echo area.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003