1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<h3 class="section">Arithmetic Operations</h3> <p>Emacs Lisp provides the traditional four arithmetic operations (addition, subtraction, multiplication, and division), as well as remainder and modulus functions, and functions to add or subtract 1. Except for <code>%</code>, each of these functions accepts both integer and floating-point arguments, and returns a floating-point number if any argument is floating point. </p> <dl> <dt id="1+">Function: <strong>1+</strong> <em>number-or-marker</em>
</dt> <dd>
<p>This function returns <var>number-or-marker</var> plus 1. For example, </p> <div class="example"> <pre class="example">(setq foo 4)
⇒ 4
(1+ foo)
⇒ 5
</pre>
</div> <p>This function is not analogous to the C operator <code>++</code>—it does not increment a variable. It just computes a sum. Thus, if we continue, </p> <div class="example"> <pre class="example">foo
⇒ 4
</pre>
</div> <p>If you want to increment the variable, you must use <code>setq</code>, like this: </p> <div class="example"> <pre class="example">(setq foo (1+ foo))
⇒ 5
</pre>
</div> </dd>
</dl> <dl> <dt id="1-">Function: <strong>1-</strong> <em>number-or-marker</em>
</dt> <dd><p>This function returns <var>number-or-marker</var> minus 1. </p></dd>
</dl> <dl> <dt id="+">Function: <strong>+</strong> <em>&rest numbers-or-markers</em>
</dt> <dd>
<p>This function adds its arguments together. When given no arguments, <code>+</code> returns 0. </p> <div class="example"> <pre class="example">(+)
⇒ 0
(+ 1)
⇒ 1
(+ 1 2 3 4)
⇒ 10
</pre>
</div> </dd>
</dl> <dl> <dt id="-">Function: <strong>-</strong> <em>&optional number-or-marker &rest more-numbers-or-markers</em>
</dt> <dd>
<p>The <code>-</code> function serves two purposes: negation and subtraction. When <code>-</code> has a single argument, the value is the negative of the argument. When there are multiple arguments, <code>-</code> subtracts each of the <var>more-numbers-or-markers</var> from <var>number-or-marker</var>, cumulatively. If there are no arguments, the result is 0. </p> <div class="example"> <pre class="example">(- 10 1 2 3 4)
⇒ 0
(- 10)
⇒ -10
(-)
⇒ 0
</pre>
</div> </dd>
</dl> <dl> <dt id="*">Function: <strong>*</strong> <em>&rest numbers-or-markers</em>
</dt> <dd>
<p>This function multiplies its arguments together, and returns the product. When given no arguments, <code>*</code> returns 1. </p> <div class="example"> <pre class="example">(*)
⇒ 1
(* 1)
⇒ 1
(* 1 2 3 4)
⇒ 24
</pre>
</div> </dd>
</dl> <dl> <dt id="/">Function: <strong>/</strong> <em>number &rest divisors</em>
</dt> <dd>
<p>With one or more <var>divisors</var>, this function divides <var>number</var> by each divisor in <var>divisors</var> in turn, and returns the quotient. With no <var>divisors</var>, this function returns 1/<var>number</var>, i.e., the multiplicative inverse of <var>number</var>. Each argument may be a number or a marker. </p> <p>If all the arguments are integers, the result is an integer, obtained by rounding the quotient towards zero after each division. </p> <div class="example"> <pre class="example">(/ 6 2)
⇒ 3
</pre>
<pre class="example">(/ 5 2)
⇒ 2
</pre>
<pre class="example">(/ 5.0 2)
⇒ 2.5
</pre>
<pre class="example">(/ 5 2.0)
⇒ 2.5
</pre>
<pre class="example">(/ 5.0 2.0)
⇒ 2.5
</pre>
<pre class="example">(/ 4.0)
⇒ 0.25
</pre>
<pre class="example">(/ 4)
⇒ 0
</pre>
<pre class="example">(/ 25 3 2)
⇒ 4
</pre>
<pre class="example">(/ -17 6)
⇒ -2
</pre>
</div> <p>If you divide an integer by the integer 0, Emacs signals an <code>arith-error</code> error (see <a href="errors">Errors</a>). Floating-point division of a nonzero number by zero yields either positive or negative infinity (see <a href="float-basics">Float Basics</a>). </p>
</dd>
</dl> <dl> <dt id="%">Function: <strong>%</strong> <em>dividend divisor</em>
</dt> <dd>
<p>This function returns the integer remainder after division of <var>dividend</var> by <var>divisor</var>. The arguments must be integers or markers. </p> <p>For any two integers <var>dividend</var> and <var>divisor</var>, </p> <div class="example"> <pre class="example">(+ (% <var>dividend</var> <var>divisor</var>)
(* (/ <var>dividend</var> <var>divisor</var>) <var>divisor</var>))
</pre>
</div> <p>always equals <var>dividend</var> if <var>divisor</var> is nonzero. </p> <div class="example"> <pre class="example">(% 9 4)
⇒ 1
(% -9 4)
⇒ -1
(% 9 -4)
⇒ 1
(% -9 -4)
⇒ -1
</pre>
</div> </dd>
</dl> <dl> <dt id="mod">Function: <strong>mod</strong> <em>dividend divisor</em>
</dt> <dd>
<p>This function returns the value of <var>dividend</var> modulo <var>divisor</var>; in other words, the remainder after division of <var>dividend</var> by <var>divisor</var>, but with the same sign as <var>divisor</var>. The arguments must be numbers or markers. </p> <p>Unlike <code>%</code>, <code>mod</code> permits floating-point arguments; it rounds the quotient downward (towards minus infinity) to an integer, and uses that quotient to compute the remainder. </p> <p>If <var>divisor</var> is zero, <code>mod</code> signals an <code>arith-error</code> error if both arguments are integers, and returns a NaN otherwise. </p> <div class="example"> <pre class="example">(mod 9 4)
⇒ 1
</pre>
<pre class="example">(mod -9 4)
⇒ 3
</pre>
<pre class="example">(mod 9 -4)
⇒ -3
</pre>
<pre class="example">(mod -9 -4)
⇒ -1
</pre>
<pre class="example">(mod 5.5 2.5)
⇒ .5
</pre>
</div> <p>For any two numbers <var>dividend</var> and <var>divisor</var>, </p> <div class="example"> <pre class="example">(+ (mod <var>dividend</var> <var>divisor</var>)
(* (floor <var>dividend</var> <var>divisor</var>) <var>divisor</var>))
</pre>
</div> <p>always equals <var>dividend</var>, subject to rounding error if either argument is floating point and to an <code>arith-error</code> if <var>dividend</var> is an integer and <var>divisor</var> is 0. For <code>floor</code>, see <a href="numeric-conversions">Numeric Conversions</a>. </p>
</dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Arithmetic-Operations.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Arithmetic-Operations.html</a>
</p>
</div>
|