summaryrefslogtreecommitdiff
path: root/devdocs/elisp/numeric-conversions.html
blob: 4680b19dc933c5adb3120c4132cad0be71f37d52 (plain)
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
 <h3 class="section">Numeric Conversions</h3>    <p>To convert an integer to floating point, use the function <code>float</code>. </p> <dl> <dt id="float">Function: <strong>float</strong> <em>number</em>
</dt> <dd><p>This returns <var>number</var> converted to floating point. If <var>number</var> is already floating point, <code>float</code> returns it unchanged. </p></dd>
</dl> <p>There are four functions to convert floating-point numbers to integers; they differ in how they round. All accept an argument <var>number</var> and an optional argument <var>divisor</var>. Both arguments may be integers or floating-point numbers. <var>divisor</var> may also be <code>nil</code>. If <var>divisor</var> is <code>nil</code> or omitted, these functions convert <var>number</var> to an integer, or return it unchanged if it already is an integer. If <var>divisor</var> is non-<code>nil</code>, they divide <var>number</var> by <var>divisor</var> and convert the result to an integer. If <var>divisor</var> is zero (whether integer or floating point), Emacs signals an <code>arith-error</code> error. </p> <dl> <dt id="truncate">Function: <strong>truncate</strong> <em>number &amp;optional divisor</em>
</dt> <dd>
<p>This returns <var>number</var>, converted to an integer by rounding towards zero. </p> <div class="example"> <pre class="example">(truncate 1.2)
     ⇒ 1
(truncate 1.7)
     ⇒ 1
(truncate -1.2)
     ⇒ -1
(truncate -1.7)
     ⇒ -1
</pre>
</div> </dd>
</dl> <dl> <dt id="floor">Function: <strong>floor</strong> <em>number &amp;optional divisor</em>
</dt> <dd>
<p>This returns <var>number</var>, converted to an integer by rounding downward (towards negative infinity). </p> <p>If <var>divisor</var> is specified, this uses the kind of division operation that corresponds to <code>mod</code>, rounding downward. </p> <div class="example"> <pre class="example">(floor 1.2)
     ⇒ 1
(floor 1.7)
     ⇒ 1
(floor -1.2)
     ⇒ -2
(floor -1.7)
     ⇒ -2
(floor 5.99 3)
     ⇒ 1
</pre>
</div> </dd>
</dl> <dl> <dt id="ceiling">Function: <strong>ceiling</strong> <em>number &amp;optional divisor</em>
</dt> <dd>
<p>This returns <var>number</var>, converted to an integer by rounding upward (towards positive infinity). </p> <div class="example"> <pre class="example">(ceiling 1.2)
     ⇒ 2
(ceiling 1.7)
     ⇒ 2
(ceiling -1.2)
     ⇒ -1
(ceiling -1.7)
     ⇒ -1
</pre>
</div> </dd>
</dl> <dl> <dt id="round">Function: <strong>round</strong> <em>number &amp;optional divisor</em>
</dt> <dd>
<p>This returns <var>number</var>, converted to an integer by rounding towards the nearest integer. Rounding a value equidistant between two integers returns the even integer. </p> <div class="example"> <pre class="example">(round 1.2)
     ⇒ 1
(round 1.7)
     ⇒ 2
(round -1.2)
     ⇒ -1
(round -1.7)
     ⇒ -2
</pre>
</div> </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/Numeric-Conversions.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Numeric-Conversions.html</a>
  </p>
</div>