summaryrefslogtreecommitdiff
path: root/devdocs/elisp/arithmetic-operations.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/arithmetic-operations.html
new repository
Diffstat (limited to 'devdocs/elisp/arithmetic-operations.html')
-rw-r--r--devdocs/elisp/arithmetic-operations.html120
1 files changed, 120 insertions, 0 deletions
diff --git a/devdocs/elisp/arithmetic-operations.html b/devdocs/elisp/arithmetic-operations.html
new file mode 100644
index 00000000..12fd2276
--- /dev/null
+++ b/devdocs/elisp/arithmetic-operations.html
@@ -0,0 +1,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>&amp;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>&amp;optional number-or-marker &amp;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>&amp;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 &amp;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 &copy; 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>