# 3.6 数学变换

Emacs Lisp 提供了传统的四种算术运算（加法、减法、乘法和除法），以及取余和取模函数，以及加减 1 的函数。 除了%，这些函数中的每一个都接受整数和浮点参数, 如果任何参数是浮点数，则返回一个浮点数。

Function：1+ number-or-maker 此函数返回数字或标记加1。例如，

(setq foo 4) ⇒ 4 (1+ foo) ⇒ 5 此函数与 C 运算符++不同——它不增加变量。它只是计算一个总和。因此，如果我们继续，

foo ⇒ 4 如果要增加变量，则必须使用setq，如下所示：

(setq foo (1+ foo)) ⇒ 5

Function：1- number-or-maker 此函数返回数字或标记减 1。

Function：+ \&rest numbers-or-makers 此函数将其参数相加。当没有给定参数时，+ 返回 0。

(+) ⇒ 0 (+ 1) ⇒ 1 (+ 1 2 3 4) ⇒ 10

Function：- \&optional number-or-maker \&rest more-numbers-or-makers 该-函数有两个目的：求反和减法。当-有单个参数时，返回值是参数的负数。当有多个参数时，从number-or-marker中累计减去每个more-numbers-or-markers。如果没有参数，则结果为 0。

(- 10 1 2 3 4) ⇒ 0 (- 10) ⇒ -10 (-) ⇒ 0 Function：\* \&rest numbers-or-makers 此函数将其参数相乘，并返回乘积。当没有给定参数时，\* 返回 1。

(*) ⇒ 1 (* 1) ⇒ 1 (\* 1 2 3 4) ⇒ 24

函数：/ number \&rest divisors 与一个或多个除数，该函数 依次用 divisor 除 number 并且返回商。由于没有除数，此函数返回 1 / number。每个参数可以是数字或标记。

如果所有参数都是整数，则结果是一个整数，在每次除法后将商向零舍入。

(/ 6 2) ⇒ 3 (/ 5 2) ⇒ 2 (/ 5.0 2) ⇒ 2.5 (/ 5 2.0) ⇒ 2.5 (/ 5.0 2.0) ⇒ 2.5 (/ 4.0) ⇒ 0.25 (/ 4) ⇒ 0 (/ 25 3 2) ⇒ 4 (/ -17 6) ⇒ -2 如果将一个整数除以整数 0，Emacs 会抛出 arith-error 错误 （请参阅 Errors）。非零数除以浮点数零会产生正无穷大或负无穷大（请参阅浮点基础知识）。

Function：% dividend divisor 该函数返回相除后的余数。参数必须是整数或标记。

对于任意两个整数 dividend 和 divisor,

(+ (% dividend divisor) (\* (/ dividend divisor) divisor)) 如果 divisor 非零，则结果总是等于 dividend。

(% 9 4) ⇒ 1 (% -9 4) ⇒ -1 (% 9 -4) ⇒ 1 (% -9 -4) ⇒ -1 Function：MOD dividend divisor 此函数取余；

与 不同%，mod允许浮点参数；它将商向下舍入（向负无穷大）为整数，并使用该商计算余数。

如果 divisor 为零， 则在两个参数都是整数时 mod 抛出 arith-error错误，其他情况下返回 NaN。

(mod 9 4) ⇒ 1 (mod -9 4) ⇒ 3 (mod 9 -4) ⇒ -3 (mod -9 -4) ⇒ -1 (mod 5.5 2.5) ⇒ .5 对于任意两个数 dividend 和 divisor，

(+ (mod dividend divisor) (\* (floor dividend divisor) divisor)) 总是等于dividend。如果任一参数是浮点数，则会有舍入误差影响；如果除数为 0，则会抛出 arith-error 错误。有关floor，请参阅数字转换。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/shu-zi/shu-xue-bian-huan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
